<?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 Mehmet Mehmetoglu on Medium]]></title>
        <description><![CDATA[Stories by Mehmet Mehmetoglu on Medium]]></description>
        <link>https://medium.com/@mehmet.moglu?source=rss-3f2e5ecdb453------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RvyEE-EQ1y6lY9Zg-UWFhA.jpeg</url>
            <title>Stories by Mehmet Mehmetoglu on Medium</title>
            <link>https://medium.com/@mehmet.moglu?source=rss-3f2e5ecdb453------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 16:07:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mehmet.moglu/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[From Idea to Implementation: The Journey of Building Our Kafka Event Scheduler Service]]></title>
            <link>https://medium.com/trendyol-tech/from-idea-to-implementation-the-journey-of-building-our-kafka-event-scheduler-service-d7bcd181208f?source=rss-3f2e5ecdb453------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7bcd181208f</guid>
            <category><![CDATA[couchbase]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[scheduling]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Mehmet Mehmetoglu]]></dc:creator>
            <pubDate>Tue, 15 Oct 2024 11:14:20 GMT</pubDate>
            <atom:updated>2024-10-15T11:14:20.474Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nPLOM6RssqsCY8RS" /><figcaption>Photo by <a href="https://unsplash.com/@lucian_alexe?utm_source=medium&amp;utm_medium=referral">Lucian Alexe</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Intro</h3><p>As the Seller Campaigns team, we needed to schedule Kafka events to meet domain-specific requirements, such as triggering these events during flash sale periods (e.g., 12:00–15:00) or upon campaign completion.</p><p>To address this, we explored various scheduling task methods, and as a result of all our research, we decided to develop a general scheduling service instead of creating a one-off solution for a domain-specific problem.</p><p>In this article, we will discuss the process of developing our Kafka event scheduler service.</p><h3>Scheduling Methods</h3><p>We explored the alternatives we could apply in our project for managing scheduled tasks:</p><ol><li><strong>Spring Scheduling: </strong>Our projects are built on Spring Boot, using Spring Scheduling to pull and process scheduled tasks from the database at short intervals.</li><li><strong>Couchbase Eventing and Timers: </strong>We use Couchbase in our projects, managing scheduling tasks through Couchbase by using Couchbase’s Eventing and Timers features.</li><li><strong>Third-Party Tools: </strong>Using third-party tools that provide scheduling task management.</li></ol><p>When we evaluated the options based on their efforts</p><h4>Spring Scheduling</h4><p>In this method, considering that the application is a distributed microservice, it is essential to implement a robust locking mechanism to ensure that each task is processed only once. Furthermore, when scaling or restarting the application, any previously locked jobs must seamlessly resume from their last processed state.</p><h4>Couchbase Eventing and Timers</h4><p>In this method, we could implement the scheduling structure using the eventing and timers features available in Couchbase, eliminating the need for complex scheduling logic within the application. Additionally, the timers in Couchbase could operate in a distributed and scalable manner without requiring extra effort and could resume from where they left off in case of restarts.</p><h4>Third-Party Scheduling Tools</h4><p>In this method, we could manage the scheduling structure end-to-end using a third-party tool, eliminating the need to develop our scheduling service. However, it required analyzing the third-party tool, a learning curve, adding a new dependency to the system, and integrating the project with it.</p><h4>Decision</h4><p>After analyzing various scheduling approaches, we chose to develop our scheduling service using Couchbase Eventing and Timers. This solution allowed us to build the desired scheduling structure most effectively, considering both impact and effort.</p><p>When evaluating other solutions, we found that using Spring scheduling required significantly more effort to set up the desired structure. Using a third-party tool involved new dependencies, potential risks, integration efforts, and a learning curve. Since we could resolve the issue with other methods, we considered this approach as a last resort.</p><h3>Scheduling Mechanism</h3><p>Before delving into the implementation details, let’s first explore the fundamental concepts of the scheduling mechanism.</p><h4>Couchbase Eventing</h4><p>Couchbase Eventing allows us to define JavaScript functions that are triggered by document changes, such as creation, updates, or deletions.</p><p>For more detail:</p><p><a href="https://docs.couchbase.com/server/current/eventing/eventing-overview.html">Run a Function on Data Change</a></p><h4>Couchbase Timers</h4><p>Couchbase Timers allows us to schedule the execution of JavaScript functions defined in Couchbase at specified dates.</p><p>For more detail:</p><p><a href="https://docs.couchbase.com/server/current/eventing/eventing-timers.html">Timers</a></p><p>By using these two features in Couchbase, when we create documents for scheduled Kafka events, we can listen for changes (create, update, delete) on these documents with the Eventing, and trigger functions at the desired dates using the Timer feature.</p><h3>Implementation Flow</h3><p>Let’s first take a high-level look at the definition and triggering flow of the scheduled Kafka events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RyHe7ick1FfAVKSqWNhxMA.png" /><figcaption>High-level Architecture</figcaption></figure><p>Applications that send scheduled Kafka events initiate the process by publishing a message to a Kafka topic. This message specifies the details of the future event, including the <strong>topic, key, payload</strong>, and the scheduled send time (<strong>timestamp</strong>).</p><p>The Scheduler Consumer(Kotlin + Spring Boot) listens to the Kafka topic and creates the scheduled event document in Couchbase, adding extra fields to track the event’s status and any possible errors. We will go into more detail later about the additional fields shown in red on the Couchbase document.</p><p>In Couchbase, scheduling operations (timer creation/cancellation) are performed based on the document’s information, and when the timers are triggered, a request is sent to the Scheduler API.</p><p>The Scheduler API then processes the necessary operations to send the Kafka message using the information received through the endpoint, thereby completing the flow.</p><p>Now, let’s examine the detailed process for scheduling operations in Couchbase.</p><h4>Managing Timers For Scheduled Events</h4><p>Once scheduled event documents are created in Couchbase, we need to create or cancel timers based on the information in the document to perform time-based operations. The timers will trigger a JavaScript function at the specified time, which subsequently sends a request to the Scheduler API to publish a Kafka message.</p><p>Let’s look at the flow here in a simplified manner, without exploring the business logic details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OzmRLywv7r3R4WEyU4kPog.png" /><figcaption>Scheduling Flow in Couchbase</figcaption></figure><p>When a document change occurs in Couchbase, the <strong>onUpdate</strong> function is triggered. Within this function, we check the <strong>active</strong> and <strong>status</strong> fields of the document to decide whether to create a new timer or cancel an existing one. Timers can be created by invoking the <strong>createTimer</strong> or <strong>cancelTimer</strong> functions in Couchbase.</p><p>For creating or updating a timer in Couchbase</p><pre>createTimer(callbackFunction, timerTime, uniqueTimerId, context);</pre><p>A timer is created here with a unique <strong>timerId(</strong>e.g., the document ID) and the other arguments that we specify. When the <strong>timerTime</strong> arrives, the <strong>callbackFunction</strong> is called with the <strong>context object</strong>. We can pass values to be used within the <strong>callbackFunction</strong> through the <strong>context object.</strong></p><p>To cancel the created timer</p><pre>cancelTimer(callbackFunction, uniqueTimerId);</pre><p>Using the previously assigned <strong>timerId</strong> and <strong>callbackFunction</strong>, we can cancel a timer that is scheduled to trigger in the future.</p><p>Detailed documentation for Couchbase timers:</p><p><a href="https://docs.couchbase.com/server/current/eventing/eventing-timers.html#the-timer-construct">Timers</a></p><p>Here’s a simplified version of our <strong>onUpdate</strong> function (not a complete representation and excludes logging for clarity):</p><pre>function OnUpdate(doc, meta) {<br>    // We check the document&#39;s active and status fields to decide whether to create, update, or cancel the timer<br>    // We will explain the purpose of these fields in detail later<br>    if (shouldCreateOrUpdateTimer(doc)) {<br>        const timerTime = new Date(doc.timestamp);<br><br>        const context = {<br>            id: meta.id,<br>            expiration: meta.expiration,<br>        };<br><br>        createTimer(timerCallback, timerTime, meta.id, context);<br>    } else if (shouldCancelTimer(doc)) {<br>        cancelTimer(timerCallback, meta.id));<br>    }<br>}</pre><p>At the specified time of the created timer, the <strong>timerCallback</strong> function is called. Within this function, we utilize Couchbase’s <strong>curl </strong>function to send a request to the Scheduler API endpoint, including the necessary payload for sending a Kafka message. The endpoint then sends the Kafka message using the provided payload.</p><p>The simplest version of the example <strong>timerCallback</strong> code (we’ll cover status tracking and error handling details later).</p><pre>function timerCallback(context) {<br>   // fetch the related document<br>   const doc = scheduler_event[context.id];<br><br>   const request = {<br>       path: &quot;/scheduler/trigger&quot;,<br>       body: {<br>           &quot;key&quot;: doc.key || null,<br>           &quot;topic&quot;: doc.topic,<br>           &quot;payload&quot;: doc.payload,<br>       }<br>   };<br>   curl(https://medium.com/&#39;POST&#39;,%20scheduler_api_url,%20request);<br>}</pre><p>Using this flow, we successfully sent scheduled Kafka events for the happy path scenario. However, we need to track the success of these events and implement retry scenarios in case of failures</p><h3>Consistency and Fault Tolerance: Status Tracking and Handling Failures</h3><p>To monitor the success of scheduled events, we maintain a <strong>status </strong>field in the document throughout the processing lifecycle. Furthermore, we need a structure capable of retrying operations in case of unexpected errors in the database, Kafka, or network.</p><p>To manage retries, we include <strong>retryCount</strong>, <strong>backoffInterval</strong>, and <strong>backoffMaxAttempts</strong> fields within the document. If an error occurs while trying to send an API request to send a Kafka message, we set a new retry timer based on the specified <strong>backoffInterval</strong> (e.g., 1 minute). After this interval, we retry the process again. This retry mechanism continues until the specified limit in <strong>backoffMaxAttempts</strong> (e.g., 3 attempts) is reached.</p><p>We can depict the status transitions and error conditions in a diagram, providing a visual overview without delving into the details of the business logic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7vFPXM3xCyEtsuhVQ3DuDQ.png" /><figcaption>After Adding Status Transitions, Scheduling Flow in Couchbase</figcaption></figure><p>After adding the status transitions, below is the <strong>onUpdate</strong> function (This does not fully reflect the actual implementation and excludes logging for clarity)</p><pre>function OnUpdate(doc, meta) {<br>   // We check the document&#39;s active and status fields to decide whether to create, update, or cancel the timer<br>   // For the create timer case, we update the document&#39;s status field to ACTIVE<br>   // For the cancel timer case, we update the document&#39;s status field to CANCELLED<br>   if (shouldCreateOrUpdateTimer(doc)) {<br>       const timerTime = new Date(doc.timestamp);<br><br>       const context = {<br>           id: meta.id,<br>           expiration: meta.expiration,<br>       };<br><br>       createTimer(timerCallback, timerTime, meta.id, context);<br>       updateStatusActive(meta);<br>   } else if (shouldCancelTimer(doc)) {<br>       if (cancelTimer(timerCallback, meta.id)) {<br>           updateStatusCancelled(meta);<br>       }<br>   }<br>}</pre><p>After adding status transitions, error handling, and retry mechanisms, here is the final version of the <strong>timerCallback</strong> function. (This does not fully reflect the actual implementation, some expressions have been simplified into functions for clarity).</p><pre>function timerCallback(context) {<br>   // fetch the related document<br>   const doc = scheduler_event[context.id];<br><br>   try {<br>       const request = {<br>           path: &quot;/scheduler/trigger&quot;,<br>           body: {<br>               &quot;key&quot;: doc.key || null,<br>               &quot;topic&quot;: doc.topic,<br>               &quot;payload&quot;: doc.payload,<br>           }<br>       };<br>       const response = curl(https://medium.com/&#39;POST&#39;,%20scheduler_api_url,%20request);<br><br>       if (responseHasError(response)) {<br>           handleError(doc, context);<br>       } else {<br>           updateStatusCompleted(context);<br>       }<br>   } catch (e) {<br>       handleError(doc, context);<br>   }<br>}</pre><p>In the handleError function, we continue to create retry timers set for (<strong>now + backoffInterval</strong>), as long as the <strong>backoffMaxAttempts</strong> limit has not been exceeded.</p><pre>function handleError(doc, context) {<br>   if (doc.retryCount &gt;= doc.backoffMaxAttempts) {<br>       return;<br>   }<br><br>   updateStatusFailedAndIncreaseRetryCount(context);<br>   rescheduleTimer(doc, context);<br>}</pre><h3>Conclusion</h3><p>In this article, we initially described the need to send scheduled Kafka events, which stemmed from a specific business domain challenge. Rather than providing a solution tailored solely to this problem, we opted to develop a general scheduling service that benefits not only our team but also other teams across the organization.</p><p>Our general scheduling service enables us to manage various time-based requirements from a single service, such as sending Kafka events for flash discount periods and campaign end dates, as well as triggering archiving processes at specific times, and sending any time-based Kafka events.</p><p>Thanks for reading.</p><blockquote>We’re always looking for passionate and talented individuals to join our team. Learn more and apply from the links below.</blockquote><p><a href="https://jobs.lever.co/trendyol/">Trendyol jobs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7bcd181208f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/from-idea-to-implementation-the-journey-of-building-our-kafka-event-scheduler-service-d7bcd181208f">From Idea to Implementation: The Journey of Building Our Kafka Event Scheduler Service</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[Eliminating the Latency: Displaying Campaigns to Thousands of Sellers in Less Than 5 Minutes]]></title>
            <link>https://medium.com/trendyol-tech/eliminating-the-latency-displaying-campaigns-to-thousands-of-sellers-in-less-than-5-minutes-e4f9fa7b453d?source=rss-3f2e5ecdb453------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4f9fa7b453d</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <category><![CDATA[couchbase]]></category>
            <category><![CDATA[latency]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Mehmet Mehmetoglu]]></dc:creator>
            <pubDate>Fri, 16 Jun 2023 18:42:42 GMT</pubDate>
            <atom:updated>2023-06-16T18:42:42.126Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4xMUfTCt9E6Kw2jx4eZSBw.jpeg" /></figure><p>For Trendyol, an e-commerce platform that offers a marketplace model, Campaigns is one of the most important discount tools that help sellers to reach more customers and increase their sales by adding their products to the campaigns.</p><p>As Seller Campaigns team, our responsibility is to maintain the campaign management process which contains the creation of these campaigns, showing them to relevant sellers and providing necessary tools for them to add their products to the campaigns, and thus improve seller experience and engagement.</p><p>In this article, with my colleague <a href="https://medium.com/u/9ca15f49392c">Hasan Utku Uçar</a>, we will be discussing latency problems that we faced while displaying the campaigns to the sellers especially in big events like Black Friday and how we have decreased the latency of showing these campaigns.</p><h3>Problem</h3><p>A campaign can be shown to a seller if the seller has the relevant products that meet the campaign’s participation criterias like category, brand, price range, etc. In order to increase seller participation in campaigns, it is necessary to sort these campaigns based on the number of products or total stock quantity that can be added. For all of these, we have to calculate these values by filtering and using the seller’s product data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0QiQPoO6DK1ZaZ8JDsOEtg.png" /><figcaption>Mock example of our campaigns page</figcaption></figure><p>Initially, we were sending Kafka events for each seller and a campaign pair to calculate eligible product counts and their total quantities for deciding if the seller has eligible products or not. While processing these events, we were sending filter requests to the Product Team’s API and we were caching the eligible product counts to Couchbase.</p><p>However, for tens of thousands of sellers and hundreds of campaigns, it was taking a significant amount of time to process these events because we were sending millions of product filter requests. Because of that, we were experiencing delays in displaying new campaigns to the sellers. Delays could even reach hours during critical event periods such as Black Friday and 11.11. The reason for this was the time-consuming process of handling 15–20 million Kafka events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*BTtpRurtHyqN96CFkcUxmg.png" /><figcaption>Old structure for calculating eligible product counts</figcaption></figure><p>The main problem here was that our campaign data and the product team’s product data were separate, making it difficult to perform sorting and filtering operations based on data belonging to different teams. To solve this problem, we decided to keep a small part of the product data on our side because we had already tried all possible improvements that could be made while using the other team’s API, but they were ineffective.</p><h3>Transferring Product Data To Our Elasticsearch</h3><p>Since we have more than one and special kinds of filtering needs on the product data that the Product Team kept on Couchbase, we decided to transfer it to our Elasticsearch. And we used <a href="https://docs.couchbase.com/elasticsearch-connector/current/index.html">Couchbase to Elasticsearch (CBES) Connector</a> for this job. With this connector, all product documents and every change on these documents after the data are transferred have become written to Elasticsearch. Also, we kept only the fields that we needed to filter on the documents, and thus the index size was reduced.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_J685jdEr6V0kc3pvfAGSg.png" /><figcaption>New structure for calculating eligible product counts</figcaption></figure><h3>Elasticsearch Tuning</h3><p>After transferring the product data to our side, we started to send the product filter requests to our Elasticsearch, but as it was expected the same performance problems continued. Then we focused on tuning the Elasticsearch.</p><h4>Index Settings</h4><p>Initially, we checked the index settings, which are crucial for Elasticsearch query performance. Our index had approximately 800 million documents and a size of 1TB. Since our data was divided into shards on Elasticsearch, the size of the shards was a critical setting for both read and write performance. <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/size-your-shards.html">According to Elasticsearch documentation</a>, there are general practices for shard sizes, but there is no one-size-fits-all solution. <strong>It is recommended that shard sizes range from 10GB to 50GB for log clusters and average around 20–25GB for search clusters</strong>. To determine the shard size, the suggested strategy is to set an initial value and test query performance to find the optimal value.</p><p>According to these practices, the shard sizes we were using were not compatible with the recommended values. We rearranged the index settings for shard sizes to be around 20GB on average and performed a reindex. After that, we tested the new index and observed that the queries were performing more efficiently compared to the previous index.</p><h4>Increasing Cluster Resources</h4><p>Another important point is the balanced utilization of Elasticsearch resources. In our tests, the queries were working stable up to a certain TP value, However, at higher TP values, CPUs’ usages were reaching up to <strong>95–98% </strong>and the queries were starting to not return any response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*9Iu7_lcEw7ME3lFAjGd78Q.png" /><figcaption>Resource usage from our Kibana dashboard</figcaption></figure><p>So to fix this problem, we discussed it with the SRE team and they increased the number of cluster nodes and their resources.</p><h4>Using Aggregations</h4><p>During the critical campaign event periods, the number of queries we needed to send to Elasticsearch was around <strong>15–20M</strong> and we had to keep the TP value stable to maintain performance. And sending query requests one by one for each campaign and seller pair was an expensive process. The main cost here was due to the product related filters like category, brand, etc. that we were sending while querying. Because these filters were arrays and they contained too many elements in them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*RfpRWZIrsHo81csz5R3ZoQ.png" /><figcaption>Pseudo query to demonstrate our filter usage</figcaption></figure><p>For a campaign, the product filters used in the queries do not change except for the seller information. For this reason, we decided to put multiple seller information as filters and use the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html">aggregation feature</a> in order to obtain the eligible product count and stock quantity of multiple sellers in a single query.</p><p>In this way, <strong>we have enabled Elasticsearch to reduce the cost of document traversal and made it to calculate these values for more than one seller at once</strong>.<strong> </strong>For example, while 20 queries were sent for 20 sellers in the past, now only 1 query is being sent for 20 sellers thanks to the aggregation. After doing this, we reduced the response time per seller and throughput.</p><h4>Improving Cache Usage</h4><p>While researching aggregation queries, we discovered the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html#agg-caches"><em>preference parameter</em></a> that enhances cache usage.</p><blockquote>For faster responses, Elasticsearch caches the results of frequently run aggregations in the shard request cache. To get cached results, use the same preference string for each search.</blockquote><blockquote>Elasticsearch routes searches with the same preference string to the same shards. If the shards’ data doesn’t change between searches, the shards return cached aggregation results.</blockquote><p>The purpose of this parameter is to improve cache utilization by traversing nodes and shards in the same order for similar queries.</p><p>To use this feature, we created a hash for product filters’ values in the queries and used this hash as the preference parameter. As a result, although initial queries were running slowly, due to the constant preference hash value for the same filters, the subsequent queries became much more efficient by utilizing the cache.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*hp9GCPes6sSeGw1E5dLjfg.png" /><figcaption>Cache usage increases on heavy load</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n7UofPSQjuiAlJgepWKn3g.png" /><figcaption><em>Final query performance results </em>comparison<em> after the Elasticsearch improvements</em></figcaption></figure><h3>Consuming Kafka Messages in Batch</h3><p>After the improvements on the Elasticsearch side, we became capable of performing operations for multiple sellers in a single query. To achieve this, we also needed to process messages in batches on the Kafka side. We used the campaign id as the partition key value while producing events and thus collecting the same campaign events in the same partition.</p><p>On the consumer side, to ensure the efficient performance of batch consumers, it was crucial to adjust the number of messages fetched in a single poll which required modifying the <strong>max-poll-records</strong> configuration parameter. We adjusted this parameter to create a balanced TP for Elasticsearch in a single request.</p><h3>Couchbase Bulk Writes</h3><p>After all the optimizations, we resolved the performance issues during the data calculation process. However, this time, while writing the calculated data to Couchbase to cache the eligible product counts, we pushed the application and database resources to their limits due to high throughput and experienced performance problems during the writing operations. The reason for this was that we were individually writing millions of values to Couchbase, causing the <strong>ops/sec</strong> value to reach around our suggested maximum limit, which is approximately 50–60k, that our current database can handle. And we tried to find a different solution other than increasing the resources.</p><p>As a solution, since we already obtained multiple seller information in a single query, we decided to write all the seller data we obtained in the response in a single query. And we started to upsert these documents <a href="https://docs.couchbase.com/server/current/guides/insert.html#inserting-documents-in-bulk">in bulk using N1QL</a>. We used N1QL because bulk operations implemented in Java SDK and Spring Data Couchbase were actually executing multiple queries in parallel instead of single bulk queries</p><p>With this change, the writing operations became more efficient and we reduced the <strong>ops/sec</strong> value to around 5–10k.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p8WLb4LCiQPp6tDR4QotzQ.png" /><figcaption>Couchbase write performance comparison between single and bulk upsert</figcaption></figure><h3>Conclusion</h3><p>In order to display the campaigns to the sellers, we needed to process the eligible product calculation events for millions of potential products to be added in a timely manner. However, due to the previous structure, processing these events took a significant amount of time, resulting in sellers seeing their campaigns late. This calculation process was taking too much time, even hours, during the critical event periods.</p><p>Thanks to all these changes, we improved the campaigns’ visibility latency. Today, all of our sellers can see their newly created campaigns in less than 5 minutes instead of hours of waiting time during important event periods like Black Friday, 11.11, etc. Also, we used to have millions of messages waiting to be processed that create lag on our Kafka topic because of the high requests and high processing times, but now we do not have this problem anymore.</p><p>Thanks for reading.</p><blockquote>We’re always looking for passionate and talented individuals to join our team. Learn more and apply from the links below.</blockquote><p><a href="https://jobs.lever.co/trendyol/">Trendyol jobs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4f9fa7b453d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/eliminating-the-latency-displaying-campaigns-to-thousands-of-sellers-in-less-than-5-minutes-e4f9fa7b453d">Eliminating the Latency: Displaying Campaigns to Thousands of Sellers in Less Than 5 Minutes</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[Liquibase ile veritabanı şeması üzerinde versiyon kontrolü]]></title>
            <link>https://medium.com/@mehmet.moglu/liquibase-ile-veritaban%C4%B1-%C5%9Femas%C4%B1-%C3%BCzerinde-versiyon-kontrol%C3%BC-8a6b9e307668?source=rss-3f2e5ecdb453------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a6b9e307668</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[liquibase]]></category>
            <category><![CDATA[version-control-system]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Mehmet Mehmetoglu]]></dc:creator>
            <pubDate>Sat, 22 Feb 2020 10:44:13 GMT</pubDate>
            <atom:updated>2020-02-22T10:44:13.114Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-V7c7vtYTJLncMlnaAfOOQ.jpeg" /></figure><p><strong>Liquibase nedir ?</strong></p><p>Liquibase veritabanı şeması üzerindeki değişikliklerimizi yönetmemizi sağlayan açık kaynaklı bir Java kütüphanesidir. Veritabanı şeması üzerindeki değişikliklerin izlenmesini, yönetilmesini ve uygulanmasını sağlar. Kaba tabirle veritabanı üzerinde versiyon kontrol sistemi kurmamıza yardımcı olur.</p><p><strong>Neden böyle bir araca ihtiyaç duyarız ?</strong></p><p>Projedeki versiyon çeşitliliği arttığında ve bu versiyonlar farklı ortamlara dağıtıldığında veritabanı şemasının da kontrolü karmaşıklaşır ve zaman kaybına yol açar.</p><p>Örneğin, herhangi bir ortama ürünün yeni versiyonunu çıktığımızı ve veritabanı şemasının da değiştiğini düşünelim.</p><p>Manuel olarak veritabanı güncellenicekse versiyonlar arası şema değişikliklerini manuel olarak tespit etmek, ilgili sql dosyalarını kullanmak gerekir ve süreç oldukça karmaşık bir hal alır. Aynı zamanda olası bir hatada rollback yapmakta zorlaşır.</p><p>Hibernate hbm2ddl kullanıldığı durumda, değişiklikleri otomatik olarak üretip, uyguladığından hatalara yol açabilmektedir.</p><p>Bu nedenlerden ötürü veritabanı şeması üzerinde versiyon kontrolünü sağlamak amacıyla Liquibase kullanılabilir. <strong>Liquibase’in sunduğu avantajlar</strong></p><ul><li>Versiyon kontrollü veritabanı değişiklikleri</li><li>Deployment esnasında değişikliklerin otomatik uygulanması</li><li>Uygulama içine gömülme, build tool veya Jenkins ile kullanılabilme</li><li>Rollback yapmanın kolay olması</li><li>Değişiklikler branchler üzerinde saklandığı için git işlemleri</li></ul><h3><strong>Liquibase Genel Bakış</strong></h3><p>Liquibase, değişiklikleri changeLog dosyaları üzerinden takip eder. Veritabanı şeması üzerinde yapılacak değişiklikler changeLog dosyalarına yazılır ve Liquibase bu dosyaları kullanarak veritabanı şemasını günceller. changeLog dosyaları sql, xml, json veya yaml formatında yazılabilir.</p><p><strong>ChangeLog Dosyaları</strong></p><p>Veritabanı şeması üzerinde yapacağımız değişiklikleri içeren bu dosyalar “changeSet” ifadelerinden oluşur. Yapılan her değişiklik changeSet olarak belirtilir. Bir changeSet author ve id değerlerinden oluşur. Author ve id değerleri changeSetleri birbirinden ayırt etmek için kullanılır. Id ise item numarası, açıklama veya herhangi bir ifadeden oluşabilir.</p><p>Sql formatında bir changeLog dosya formatı aşağıdaki gibi yazılır. Liquibase sql commentlerini kullanarak changeSeti ayırt eder. Rollback ile yazılan kısımlar ise değişiklik geri alındığı zaman kullanılır. Değişiklikler sql formatında yazılacaksa rollback ifadesi de yazılmalıdır.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08e0c6e24ca9faf5681be72fb45c0ca0/href">https://medium.com/media/08e0c6e24ca9faf5681be72fb45c0ca0/href</a></iframe><p>Xml formatında changeLog dosyasına bakarsak root olarak databaseChangeLog ve içerisinde changeSet taglerinden oluşur.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8c52f6176fe7be5ac091c969cf3f350/href">https://medium.com/media/b8c52f6176fe7be5ac091c969cf3f350/href</a></iframe><p>Xml formatında bir changeSet ise aşağıdaki gibi yazılır.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/096a1964002f3ee10da2b5ebacdbeeab/href">https://medium.com/media/096a1964002f3ee10da2b5ebacdbeeab/href</a></iframe><blockquote>İki format arasındaki fark ise, xml formatında yazılan changeLoglar kullanıldığında, <strong>Liquibase bu dosyaları sql olarak üretir ve çalıştırır.</strong> Bu durumun sağladığı avantajlar</blockquote><ul><li>Bir changeLog dosyasını farklı veritabanları üzerinde uygulama (örn. Dev ortamında PostgreSQL, Test ortamında h2 gibi)</li><li>Rollback statement yazmaya gerek kalmaması(uç durumlar hariç)</li></ul><p>Ama yinede Liquibase’in yaptığı geliştirici <a href="https://www.liquibase.org/2020/01/top-10-findings-liquibase-survey.html"><strong>anketine</strong></a> göre:</p><p>Liquibase kullanıcıları çoğunluk olarak Sql formatı tercih etmektedir. Bu durumun xml formatında changeSet yazmanın ekstra zaman alması ve sql yazmanın daha pratik olmasından kaynaklandığını düşünüyorum.</p><p><strong>Veritabanı üzerinde yapılan değişiklikler</strong></p><p>Liquibase, veritabanı üzerinde yapılmış değişikleri izlemek için “DATABASECHANGELOG” isimli bir tablo kullanır. Liquibase şema üzerinde değişiklik yaptığı zaman bu tabloyu log tablosu olarak kullanır.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aglV9_5AhjxFwzlv9XenOA.png" /><figcaption>Örnek bir databasechangelog tablosu, id, author, filename, md5sum gibi birçok sütun barındırmaktadır</figcaption></figure><p>Yapılan değişiklikler bu tabloya yazılır ve bu şekilde tüm işlemler kayıt altına alınmış olur. Liquibase bu tabloyu update, rollback ve diğer işlemlerde referans alır. Örneğin bir changeSet ile iki defa update yapmaya çalışırsanız, ilk işlemde bu tabloya kayıt atılacağı için ikinci çalıştırmada Liquibase hiçbir işlem gerçekleştirmez. changeSet eşleme durumları id, author, filename, md5sum gibi değerler üzerinden yapılır.</p><p>Ayrıca Liquibase kendi içinde DATABASECHANGELOGLOCK isminde bir tablo daha kullanır. Bu tabloyu birden fazla Liquibase instance çalışma ihtimaline karşı lock tablosu olarak kullanır(concurrency amaçlı).</p><p><strong>Liquibase Konfigürasyonu</strong></p><p>Liquibase <a href="https://download.liquibase.org/">buradan</a> indirilebilir .</p><p><strong>Command Line, CLI Script, Maven Plugin, Gradle, Ant veya Spring Boot entegrasyonuyla kullanılabilir.</strong></p><p>Eğer konsol veya build tool aracılığıyla kullanılacaksa Liquibase.properties dosyasına veritabanı bağlantı bilgileri yazılmalıdır. Spring Boot projesi içinde kullanılacaksa konfigürasyon ihtiyacımız yoktur çünkü Spring’in ürettiği DataSource objesini Liquibase doğrudan kullanır. Örnek liquibase.properties dosyası(password encrypted olarak verilebilir).</p><pre>driver:com.mysql.cj.jdbc.Driver<br>url:jdbc:mysql://localhost:3306/employees<br>username:username<br>password:password<br>classpath:lib/mysql-connector-java-8.0.19.jar</pre><h3><strong>Liquibase üzerinde yapılan işlemler</strong></h3><ul><li>Update</li><li>Rollback</li><li>Diff</li><li>GenerateChangeLog(şema üretimi)</li></ul><p>Temel işlemler olarak sıralanabilir. Komut satırı üzerinden işlemleri basitçe gördükten sonra Spring Boot entegrasyonunu inceleyelim.</p><p><strong>Update</strong></p><p>Tek bir changeSet ile basit bir update</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5661ea97ba76258ab174d9bfa5f79c3/href">https://medium.com/media/d5661ea97ba76258ab174d9bfa5f79c3/href</a></iframe><p>Komut satırı ile Linux üzerinde Liquibase sh, Windows ortamında Liquibase bat üzerinden çalışabilir.</p><pre>liquibase --changeLogFile=create-table-offices.sql update</pre><p>İşlem başarıylıysa “Liquibase: Update has been successful” gibi bir mesaj alırız. Şimdi “DATABASECHANGELOG” tablomuza bakalım.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gy-vsJFvHg5SfzmSd5_5Dg.png" /></figure><p>Görüldüğü gibi yaptığımız işlem log tablosuna yazıldı. Daha sonra yapılacak update veya rollback işleminde bu tablo kullanılır.</p><p>Daha gerçekçi bir senaryo ile bakarsak, bir versiyonda çoklu değişiklikler bulunacağı için her versiyona ait bir changeLog dosyası ve bu dosyaların toplandığı bir master-changelog dosyası bulundurmak <a href="https://www.liquibase.org/bestpractices.html">liquibase best-practise </a>olarak görülmektedir.</p><p>Örnek olarak, db.changelog-master.xml dosyası</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ecba8554b65b68af3fd396a3d038295/href">https://medium.com/media/3ecba8554b65b68af3fd396a3d038295/href</a></iframe><p>Her bir versiyon dosyası, db.changelog-1.0.sql changeSetleri içerir veya changeSetler büyük olduğu durumda include seçeneği ile ayrı dosyalara ayrılabilir.</p><p>Ayrıca master change log dosyasına aşağıdaki gibi tag ifadeleri ekleyebiliriz. Bu ifade databasechangelog tablosuna versiyon belirtme amacıyla kayıt atar.</p><p>Örneğin, 1.0 tagini db.changelog-1.0.sql dosyasından önce yerleştirirsek bu tagleri rollback işlemi yaparken veritabanını spesifik bir versiyona doğru geri alma amacıyla kullanabiliriz.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01764e7785b2d438b3b56fc3b2ee798c/href">https://medium.com/media/01764e7785b2d438b3b56fc3b2ee798c/href</a></iframe><p>Update ile ilgili son olarak, Update işlemi gerçekleştirmeden updateSql komutuyla işlem için çalışacak sql scriptini görebiliriz. Bu özellik update öncesi kontrol amacıyla kullanılır.</p><pre>liquibase --changeLogFile=db.changelog-master.xml --outputFile=migration-master.sql updateSql</pre><p>İşlem sonucunda migration-master.sql dosyasına update durumunda çalışacak sql script yazılmış olur.</p><p><strong>Rollback</strong></p><p>Veritabanı üzerinde yapılmış değişiklikleri geri almak amacıyla kullanılır. Liquibase rollback için üç opsiyon sunar</p><ul><li>rollback&lt;Tag&gt; → belirliten tag’e geri dönme</li><li>rollbackToDate →belirtilen tarihe geri dönme</li><li>rollbackCount→ belirtilen değişiklik sayısı kadar geri alma</li></ul><p>Test amaçlı rollback öncesi yaptığım değişiklikler</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ob8E8SCqyIABnGq8FsG8Kg.png" /></figure><p>Örnek olarak version_1.1 tag’ine geri dönelim.</p><pre>liquibase --changeLogFile=db.changelog-master.xml rollback version_1.1</pre><p>db.changelog-master.xml dosyasını parametre geçmemizin nedeni yaptığımız değişiklikleri barındırması ve log tablosuyla eşleşme yapması içindir.</p><p>İşlem sonucu aldığım çıktıda, sondan geriye doğru değişikliklerin geri alındığı görülmektedir.</p><pre>Rolling Back Changeset:path-independent::tagDatabase-example3::moglu<br>Rolling Back Changeset:path-independent::insert-table-warehouse2::moglu<br>Rolling Back Changeset:path-independent::insert-table-warehouse::moglu<br>Rolling Back Changeset:path-independent::create-table-warehouse::moglu<br>Rolling Back Changeset:path-independent::tagDatabase-example2::moglu<br>Liquibase: Rollback has been successful.</pre><p>DATABASECHANGELOG tablosunu incelediğimiz zaman ise 1.1 tagi ve sonraki değişiklerin geri alındığını görebiliriz. version_1.1 tag’inin silinme sebebi versiyon belirtme amaçlı tag olmasından ötürüdür eğer normal bir şema değişikliği olsaydı dahil edilmeyecek ve ondan sonraki değişiklikler geri alınmış olacaktı.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4xiVcmInF9PqW1BBHf9beQ.png" /><figcaption>Rollback sonucu log tablosunun son hali</figcaption></figure><p>Rollback yapmak önemli bir işlem olduğu için, bu işleme kalkışmadan önce çalıştırılacak sql dosyasını görmek istersek rollbackSql komutunu kullanabiliriz.</p><pre>liquibase --changeLogFile=db.changelog-master.xml --outputFile=rollback-to-1.1.sql rollbackSql version_1.1</pre><p>output dosyasından kısa bir kesit</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/734bf008da07ac2da4e129f494dfab0a/href">https://medium.com/media/734bf008da07ac2da4e129f494dfab0a/href</a></iframe><p><strong>Diff Almak</strong></p><p>Liquibase iki veritabanı arasında diffişlemi gerçekleştirip sonucunu bir text dosyası yada sql gibi çıktı verebilmektedir.</p><p>Bunun için iki farklı komut</p><ul><li>diff →text dosyasına açıklama bazında diff yazılır.</li><li>diffChangeLog →istenilen formatta update scripti oluşturulur.</li></ul><p>İşlem öncesinde liquibase.properties dosyasına kendi veritabanınız ve karşılaştırmak istediğiniz veritabanı için bağlantı bilgilerini yazmak gerekir.</p><pre>driver:com.mysql.cj.jdbc.Driver<br>url:jdbc:mysql://localhost:3306/employees<br>username:username<br>password:password</pre><pre>referenceDriver: com.mysql.cj.jdbc.Driver<br>referenceUrl:jdbc:mysql://localhost:3306/employees<br>referenceUsername:username<br>referencePassword:password<br>classpath:lib/mysql-connector-java-8.0.19.jar</pre><p>Text formatında diff almak için;</p><pre>liquibase --outputFile=sample-diff.txt diff</pre><p>Sql formatında diff almak için(kendi veritabanınız için update scripti)</p><pre>liquibase --changeLogFile=sample-diff.<strong>mysql</strong>.sql diffChangeLog</pre><p><em>Sql formatında diff alırken dosya isminde kullandığınız veritabanını belirtmeniz gerekir.</em></p><p>Örnek text çıktısı</p><pre>Missing Table(s): <br>     offices<br>     warehouse<br>Unexpected Table(s): <br>     test2<br>Changed Table(s): NONE</pre><pre>Missing Column(s): <br>     employees.warehouse.FNAME<br>     employees.warehouse.ID<br>     employees.offices.active<br>     employees.offices.id<br>     employees.offices.name<br>     employees.offices.office_location<br>Unexpected Column(s): <br>     employees101.test2.FNAME<br>     employees101.test2.ID<br>...</pre><p>Örnek update scripti</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01edfdf6d6f5ae2caea8c4b118b55ee2/href">https://medium.com/media/01edfdf6d6f5ae2caea8c4b118b55ee2/href</a></iframe><p><strong>Şema üretmek</strong></p><p>Liquibase generateChangeLogkomutuyla mevcut veritabanının şemasını istenilen formatta üretebilir. Üretilen dosya bir başka şema üzerinde uygulanabilir ve değişilikler log tablosuna da yazılmış olur</p><pre>liquibase --changeLogFile=initial-schema.<strong>mysql</strong>.sql generateChangeLog</pre><p><em>Şema üretimi için de kullandığınız veritabanını dosya uzantısında belirtmelisiniz.</em></p><p>Çıkan dosyadan bir kesit</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecffce6ef7ec82412c8187622e593b9e/href">https://medium.com/media/ecffce6ef7ec82412c8187622e593b9e/href</a></iframe><h3>Liquibase Spring Boot Entegrasyonu</h3><p>Liquibase Spring Boot projesine kolaylıkla entegre edilebilir. Yapmamız gereken şey projeye <a href="https://mvnrepository.com/artifact/org.liquibase/liquibase-core">liquibase-core</a> maven dependency eklemektir.</p><p>İlgili dependency eklendikten sonra liquibase, application.properties gibi property dosyası üzerinden basitçe konfigüre edilebilir.</p><pre>spring.liquibase.enabled=true<br>spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml<br><br>logging.level.liquibase=INFO</pre><p>ChangeLog dosyalarını db/changelog altında tutabiliriz.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*iUvP1Enhdsx3W5ax4UgsdA.png" /></figure><p>Propertyler ayarlandıktan sonra projeyi ayağa kaldırdığımızda liquibase otomatik olarak çalışır, master change log dosyasını okur ve veritabanı şemasını günceller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w3_WKz9INX73DRFfo5Tpkg.png" /></figure><p>İşlemden sonra kontrol amaçlı DATABASECHANGELOG tablosuna bakılırsa yapılan değişilikler görülebilir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X-v1-ZH2VGiwTncgNDPNWQ.png" /></figure><h3>Sonuç</h3><p>Liquibase ile veritabanı şeması üzerinde yapılan değişiklikler üzerinde versiyon kontrolü yapılabilir. Bu sayede değişiklikler branchlerde tutulur, kolayca izlenir, versiyonlanabilir ve otomatik olarak uygulanabilir.</p><p>Takım üyeleri arasında da veritabanı değişiklikleri kolayca görülebildiğinden manuel güncellemeler ve versiyona göre şema karmaşıklıkları ortadan kalkmış olur.</p><p>Bu yazımda çok fazla detaya girmeden Liquibase ile veritabanı şeması üzerinde versiyon kontrolü hakkında bilgi vermeye çalıştım. Umarım faydalı bir yazı olmuştur.</p><p>İlgili kodları <a href="https://github.com/moglu/spring-liquibase-demo">github</a> hesabımda bulabilirsiniz.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a6b9e307668" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>