<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nurullah Samed Savaş on Medium]]></title>
        <description><![CDATA[Stories by Nurullah Samed Savaş on Medium]]></description>
        <link>https://medium.com/@nurullahsamed?source=rss-ec3752870617------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ogONjbuALy_z3cpJbtR1UA.jpeg</url>
            <title>Stories by Nurullah Samed Savaş on Medium</title>
            <link>https://medium.com/@nurullahsamed?source=rss-ec3752870617------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:05:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nurullahsamed/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Did We Implement More Maintainable And Extendable System Using A State Machine In Go?]]></title>
            <link>https://medium.com/trendyol-tech/how-did-we-implement-more-maintainable-and-extendable-system-using-a-state-machine-in-go-de4900f0805e?source=rss-ec3752870617------2</link>
            <guid isPermaLink="false">https://medium.com/p/de4900f0805e</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[state-machine]]></category>
            <dc:creator><![CDATA[Nurullah Samed Savaş]]></dc:creator>
            <pubDate>Thu, 14 Dec 2023 07:15:37 GMT</pubDate>
            <atom:updated>2023-12-14T07:15:37.956Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction:</strong></h3><p>State machines are essential tools in software development for managing complex systems and workflows. They help you model and control the state transitions of your application, making it easier to understand and maintain. In this article, we will talk about why we as Trendyol Translation Team needed to create state machines and how we implemented it.</p><h3><strong>Problem:</strong></h3><p>Before we dive into the implementation, let’s discuss why we needed to create state machines. As the translation team at Trendyol, we strive to provide solutions for various translation needs of multiple teams within organization, and each team has different translation process requirements. For example, in the workflow you see below, a translation request is initially created in the “Draft” state. In this state, the text to be translated undergoes linguistic acceptance checks. If it passes the check, it proceeds to the “SourceReady” state; if it doesn’t pass, it goes to “SourceRejected.” After the “SourceReady” state, the source text is translated, and it progresses to the “Completed” state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*xbH-PQwutRGIXuVSWgPNFg.png" /></figure><p>We have many different workflows like this. Some stakeholders may not require linguistic acceptance checks, while others may want an intermediate state after machine translation, where human review takes place before progressing to the final “Completed” state.</p><p>When considering managing all of these workflows within the code using if and switch statements, the cost of updating, maintaining, and testing these flows would be very high. To make our system more easily changeable, extensible, and maintainable, we decided to create state machines for each workflows we have.</p><h3><strong>Implementation:</strong></h3><p>The github.com/qmuntal/stateless library is a popular Go library for working with state machines. It provides a powerful and flexible way to define and manage state transitions.</p><p><strong>Defining States and Transitions:</strong></p><p>With the library, we can start defining our state machine. In this example, let’s consider to implement workflow we talked about in previous topic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e41388320e04230390485e2464d944a8/href">https://medium.com/media/e41388320e04230390485e2464d944a8/href</a></iframe><p>In the above code:</p><ul><li>We create a new state machine with the initial state “Draft.”</li><li>We define transitions from “Draft” to “SourceRejected” and to “SourceReady.”</li><li>We define transitions from “SourceReady” to “Completed”</li></ul><p>We will demonstrate how to fire these transitions using triggers in the next steps.</p><p><strong>Handling State Transitions with triggers:</strong></p><p>To manage state transitions, we implemented a workflow service as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5267890fa3f664818ca3359fa5f272c5/href">https://medium.com/media/5267890fa3f664818ca3359fa5f272c5/href</a></iframe><p>With the NewService function, we create a service containing an array that holds constructor functions for all existing workflow types in our system. When needed for a specific workflow, the LoadWorkflow method creates a workflow instance to be used.</p><p>When creating a workflow instance, we need to provide two functions. The stateAccessor function is used to read the current state of the entity from the database, and the stateMutator function is responsible for updating the state of this entity using the current state and trigger information.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa7274ffa0d9dbbbce3671c86c543293/href">https://medium.com/media/aa7274ffa0d9dbbbce3671c86c543293/href</a></iframe><p>To trigger a transition, we use the TriggerTransition function. In this function, we first prepare the stateAccessor and stateMutator functions that will be triggered during the fire process. In some cases, we may need to update different fields on the entity in addition to the state information. Therefore, in the stateMutator function, we use the TransitionRequest struct. For example, in our scenario, when triggering the &#39;translate&#39; transition, we need to update not only the state from &#39;SourceReady&#39; to &#39;Completed&#39; but also the translation information. Hence, we use this structure for such requirements.</p><p>Later on, we create the workflow instance to which the relevant transition belongs using the LoadWorkflow function.</p><p>Since we cannot directly pass inputs to the stateAccessor and stateMutator functions, we facilitate data exchange through the context. We add the entity&#39;s id information and the translationRequest struct to the context for later retrieval in the stateAccessor and stateMutator functions.</p><p>Finally, we trigger the relevant transition using the FireCtx function. If the associated transition is valid, our state information is updated. However, if, for instance, we attempt to trigger the &#39;linguisticOK&#39; transition for an entity with the &#39;Completed&#39; state, this operation will result in an error since such a transition is not defined.</p><p>Additionally, we provide an extra endpoint that returns the workflow information to the front end in the following format. With this endpoint, the front end can have information about which triggers can be fired in which states for the relevant workflow. This way, when a new workflow is added to the system or an existing workflow changes, new trigger buttons can be easily added/removed to/from the front end without making any code changes.</p><p><em>GET /workflows/:workflowID</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a43b9fa71844f39b69b7e95d8458db6f/href">https://medium.com/media/a43b9fa71844f39b69b7e95d8458db6f/href</a></iframe><h3>Conclusion:</h3><p>In this article, we explored how to implement a state machine in Go using the github.com/qmuntal/stateless library. We defined states and transitions to our state machine. State machines are incredibly useful for managing complex workflows in your applications, making your code more readable and maintainable.</p><p>State machines provide a clear and intuitive representation of the behavior of a system. It doesn’t allow illegal transitions. It makes the system more stable and less faulty. The states and transitions are explicitly defined, making it easier for developers and stakeholders to understand how the system works.</p><p>Also state machines are commonly used in the design of user interfaces, where the interface transitions between different states based on user input . This helps in creating responsive and intuitive user experiences.</p><blockquote><em>If you want to be part of a team that tries new technologies and want to experience a new challenge every day, join us.</em></blockquote><p><a href="https://jobs.lever.co/trendyol?source=post_page-----f02fed163130--------------------------------">Trendyol jobs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de4900f0805e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/how-did-we-implement-more-maintainable-and-extendable-system-using-a-state-machine-in-go-de4900f0805e">How Did We Implement More Maintainable And Extendable System Using A State Machine In Go?</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sharing NLP Models among Gunicorn Workers: Reducing Memory Usage and Boosting Performance]]></title>
            <link>https://medium.com/trendyol-tech/sharing-large-language-models-among-gunicorn-workers-reducing-memory-usage-and-boosting-18c0efd8e942?source=rss-ec3752870617------2</link>
            <guid isPermaLink="false">https://medium.com/p/18c0efd8e942</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[gunicorn]]></category>
            <dc:creator><![CDATA[Nurullah Samed Savaş]]></dc:creator>
            <pubDate>Mon, 25 Sep 2023 17:08:18 GMT</pubDate>
            <atom:updated>2023-10-05T13:55:29.238Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Sharing NLP Models among Gunicorn Workers: Reducing Memory Usage and Boosting Performance</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*zuaJqiH4N0ojJF_B6KRpdw.png" /></figure><p><strong><em>Introduction</em></strong></p><p>As the Trendyol Translation team, we are developing processes to meet the translation needs of different teams within our company.</p><p>In some cases, before the translation process, we need to perform various text analyses on the source text. One of these analyses is the segmentation. In the segmentation step, we segment long texts to be translated into sentences and ensure that the translation is done sentence by sentence.</p><p>To meet this need, we have a Flask application written in Python. In many Python applications, the utilization of natural language processing (NLP) models is common for performing text analysis. However, especially when working with NLP models which can consume a significant amount of memory, the performance of our applications can be negatively impacted due to excessive memory usage.</p><p><strong><em>Our Problem: Excessive Memory Usage Without Preloading</em></strong></p><p>In our application, we need to load an specialized NLP model into memory as shown below in order to perform segmentation using the Spacy library.</p><pre># main.py<br>import spacy<br>from flask import Flask<br>app = Flask(__name__)<br># Load SpaCy model<br>nlp = spacy.load(“en_core_web_sm”)<br><br><br>@app.route(“/”)<br>def segmentation():<br> # Use the model for text processing<br> text = “Hello, this is a SpaCy example.”<br> doc = nlp(text)</pre><p>At first, our application used a single gunicorn worker, and we were scaling our application only with Kubernetes replicas. However, this meant a separate resource cost for each replica. To maximize efficiency for each replica, we decided to increase the number of workers. Gunicorn will, by default, start up as many workers as we specify in the configuration.</p><pre># gunicorn_config.py<br>workers = 4 # worker count</pre><p>This way, we allow each replica to handle a higher number of concurrent connections. However, when we increased the number of workers, we noticed a significant increase in the application’s memory usage.</p><p>The reason for this was that the ‘spacy.load’ part in the above code run for each worker. This means that nlp model being loaded into memory for each worker.</p><p>This problem becomes particularly pronounced when dealing with NLP models, which can consume a significant amount of memory. The redundant loading of these models for each worker not only wastes resources but also impacts the application’s scalability and responsiveness.</p><p><strong><em>Solution: Preloading the Language Model</em></strong></p><p>The solution is a simple configuration change. If we set the ‘preload_app’ configuration to ‘True’ as shown below, the application code is imported into the master process before the worker processes are forked. After the application instance is loaded into the master process, the worker processes are created as copies of this main gunicorn process and all of them share the same memory space.<br>This means that the ‘nlp’ variable in the example code above is loaded only once and shared by all the workers.</p><pre># gunicorn_config.py<br>workers = 4 # worker count<br>preload_app = True</pre><p>Preloading ensures that the model is available for all workers. This approach is particularly beneficial for applications using NLP models, improving memory efficiency and response times.</p><p><strong><em>Result</em></strong></p><p>Before this parameter change, our application, running with 4 workers, had a memory usage of approximately 1.9GB, but after the change, with the same number of workers, our memory usage decreased to 572MB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*iPXvERYhIQcwqUvppEh69g.png" /><figcaption>Memory usage with 4 workers before parameter change</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*Elr4CaeoiwuhH5_r_CUh7g.png" /><figcaption>Memory usage with 4 workers after parameter change</figcaption></figure><p>This solution makes your application more scalable and resource-efficient, especially in cases involving substantial NLP models.</p><p>If you are also using Gunicorn for resource-intensive services like our example, you can achieve significant cost efficiency with this parameter. The more extensive your application’s memory usage, the greater the benefits you will gain from this change.</p><blockquote><em>If you want to be part of a team that tries new technologies and want to experience a new challenge every day, join us.</em></blockquote><p><a href="https://jobs.lever.co/trendyol?source=post_page-----f02fed163130--------------------------------">Trendyol jobs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18c0efd8e942" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/sharing-large-language-models-among-gunicorn-workers-reducing-memory-usage-and-boosting-18c0efd8e942">Sharing NLP Models among Gunicorn Workers: Reducing Memory Usage and Boosting Performance</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>