<?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 Elia Rohana on Medium]]></title>
        <description><![CDATA[Stories by Elia Rohana on Medium]]></description>
        <link>https://medium.com/@rohaelia?source=rss-b3961f119426------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Z4NzZpExRVbHFGsSGgsT-A.jpeg</url>
            <title>Stories by Elia Rohana on Medium</title>
            <link>https://medium.com/@rohaelia?source=rss-b3961f119426------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 06:28:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rohaelia/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[Scheduling Millions Of Messages With Kafka & Debezium]]></title>
            <link>https://medium.com/yotpoengineering/scheduling-millions-of-messages-with-kafka-debezium-6d1a105160c?source=rss-b3961f119426------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d1a105160c</guid>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[messaging]]></category>
            <category><![CDATA[scheduling]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Elia Rohana]]></dc:creator>
            <pubDate>Sun, 19 Dec 2021 12:03:44 GMT</pubDate>
            <atom:updated>2021-12-21T17:00:49.198Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I5aiXzA2WZ4Ei06R0OORjg.png" /></figure><p>Implementing a high scale distributed system isn’t easy, because traditional scheduling on top of the database doesn’t scale. Moreover, it gets even harder when working with microservices architecture, as you inherit all the distributed system problems, such as <strong>data inconsistency</strong>, <strong>dual writes,</strong> <strong>and</strong> <strong>domain boundary </strong>issues.</p><p>In this article, I’ll share my journey on how we at Yotpo implemented a highly scalable and reliable scheduled-messages solution for millions of messages per minute, with Apache Kafka and Debezium CDC.</p><p>In the next few chapters, I’ll deep dive into choosing Debezium CDC to schedule messages to Apache Kafka.</p><h3>Motivation</h3><p>As part of our business at Yotpo, we need to send emails to the end users. Not only that, it can be at a future date and at a scale of millions per day, at peak time it can even get to millions in minutes. So the challenge was scheduling emails to be sent at this scale.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aWvzZph2XRcMa9_-xr9swA.png" /></figure><h3>My journey through traditional scheduling solutions</h3><p>Given the above requirements, I started researching the traditional solutions for scheduling email requests and quickly found that they don’t fit the requirements. How do we store all those emails, and schedule them on the exact delivery date, at this scale — ensuring all emails are executed on the given delivery date? <br>Let’s take a closer look at the journey and the issues I tackled along the way.</p><h4>RabbitMQ — Delayed Message Exchange</h4><p>At first we thought, this is exactly what we need! But soon after reading the plugin README, we found it doesn’t scale.</p><p>Quoted from <a href="https://github.com/rabbitmq/rabbitmq-delayed-message-exchange#limitations">RabbitMQ plugin</a></p><blockquote>“<em>Current design of this plugin doesn’t really fit scenarios with a high number of delayed messages (e.g. 100s of thousands or millions). See </em><a href="https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/issues/72"><em>#72</em></a><em> for details.</em>”</blockquote><p>Moreover, this solution meant that we wouldn’t save any messages in our systems, creating a lack of visibility and no way to monitor and have analytics on this data.</p><h4>Quartz Job Scheduler</h4><p>Using Quartz as a scheduler and job execution tool will not scale for tens of thousands and looks complicated, especially when you have short tasks/jobs.</p><p>Quote from Quartz documentation</p><blockquote>“<em>The clustering feature works best for scaling out</em><strong><em> long-running</em></strong><em> and/or cpu-intensive jobs (distributing the work-load over multiple nodes). If you need to scale out to support thousands of short-running (e.g 1 second) jobs, consider partitioning the set of jobs by using multiple distinct schedulers (including multiple clustered schedulers for HA). The scheduler makes use of a cluster-wide lock, a pattern that degrades performance as you add more nodes (when going beyond about three nodes — depending upon your database’s capabilities, etc.).</em>”</blockquote><h4>Database as Delayed Message Store for Apache Kafka messages</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KeljH7a163tiFuyQnPDY0Q.png" /></figure><ol><li>Insert scheduled messages to the database</li><li>When a scheduler timer starts, it needs to query the database on the undelivered entries and obtain a lock on those entries, to make sure the entries will only be produced once</li><li>Produce to Kafka</li><li>Update and commit to the database</li><li>Consumer service consumes the messages and sends the emails</li></ol><p><strong>Drawbacks</strong></p><ol><li><strong>Dual write problem </strong>— we are trying to write to two different systems, i.e. Database and Kafka, which leads to data inconsistency</li><li><strong>2-phase commit</strong> is not an option because it’s not scalable</li></ol><h3>Debezium CDC to the rescue</h3><blockquote><em>“Debezium is an open source distributed platform for change data capture. Start it up, point it at your databases, and your apps can start responding to all of the inserts, updates, and deletes that other apps commit to your databases. Debezium is durable and fast, so your apps can respond quickly and never miss an event, even when things go wrong.”</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPYikSo6lxmNjqqNLaWsnQ.png" /></figure><h3>Utilizing Debezium to stream scheduled messages from Database to Kafka</h3><p>We use Apache Kafka as our streaming platform and for communication between microservices. As you may know, Apache Kafka is a highly scalable distributed platform capable of processing a large number of messages, but it <strong>doesn’t provide </strong>a solution for delayed messages. So we had to find a solution to storing all delayed messages until they’re ready for processing (i.e. scheduling).</p><p>Since Debezium is widely used at Yotpo as an infrastructure for Change Data Capture, why not use it to solve problems other than plain CDC? <strong>Let me explain how we did it…</strong></p><p>If we store the messages in a database, we only need to stream those messages back to Kafka when they’re ready for processing. Since this is what Debezium excels at, and more importantly at high scale — we found it to be the perfect fit!</p><p>Let’s take a closer look at the <strong>high level architecture.</strong></p><h3>High Level Architecture (HLA)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qFDvkfIz5xksBvX23BZ_kA.png" /></figure><ol><li>HTTP server will insert the messages into the DB with the delivery time</li><li>A separate application will serve as a simple cron scheduler that updates the delayed message when the delivery time is &lt; NOW and the update <strong>is READY</strong> — flagged as true to be ignored in the next cron cycle</li><li>Debezium will capture those updated entries (only those specific updates, more on that later) via CDC, and stream those messages to Apache Kafka ready for consumption by the execution consumer</li></ol><h3>Deep dive into Debezium connector</h3><h4>The Debezium connector</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4cbe54300d21b35036d6f6c2a52c486/href">https://medium.com/media/b4cbe54300d21b35036d6f6c2a52c486/href</a></iframe><p>The connector is configured on the <strong>delayed_messages</strong> table (lines 19), and will only respond to an update operation that has <strong>is_ready=false,</strong> changed to <strong>is_ready=true</strong>.</p><h4>Let’s have a closer look at the transformers section</h4><ol><li><a href="https://debezium.io/documentation/reference/configuration/event-flattening.html"><strong>ExtractNewRecordState</strong></a> — is used for message flattening</li><li><a href="https://debezium.io/documentation/reference/configuration/topic-routing.html"><strong>Reroute</strong></a> — changes the default Debezium Kafka topic to “email.execution”</li><li><a href="https://debezium.io/documentation/reference/configuration/filtering.html"><strong>Filter</strong></a><strong> </strong>— filters messages with update operation, that changes its is_ready flag from false to true. That ensures that the Debezium connector produces a message for this specific update operation only, and ignores any other operations</li></ol><h4>The execution consumer</h4><p>A simple Kafka consumer that consumes an email message from the “email.execution” topic, and simply sends the emails. Furthermore, you need to optimize the consumer configuration and the Kafka topic partitions based on your scale.</p><h4><strong>Benefits</strong></h4><ul><li>Separation of concerns between scheduling and execution that increase scalability</li><li>Scheduler execution doesn’t affect database performance</li><li>Data consistency — no dual write issues</li><li>Fault tolerance</li><li>Flexibility</li><li>Store whatever data you need in the DB</li><li>Query this data for analytics/management purposes</li><li><strong>Gain Apache Kafka benefits</strong></li><li>Horizontal scale</li><li>Message ordering</li><li>Batch consumption to increase performance</li><li>Streaming capabilities, and more…</li></ul><h4><strong>Drawbacks</strong></h4><ul><li>Complexity — we have 2 additional infrastructures that need to be monitored and maintained</li></ul><h4><strong>Results</strong></h4><p>The system is running on production for a while, at its peak, 50K emails are scheduled at the same time, the results below are as follows:</p><ul><li>scheduler query took 1.25 seconds</li><li>~250ms of latency until debezium has captured all 50k emails from the Database and produced to Kafka topic —this was collected from the logs of the scheduler application and the email consumer application</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*7OBnUQgE56XYO1vuW9_4Mw.png" /><figcaption>scheduler update query execution time</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*J04a3vHfIuUxV1lJg9dDng.png" /><figcaption>consumer lag spike</figcaption></figure><p>as you can see the scheduling is very fast, and according to the results, we can get around 2.4 million messages per minute! and that’s by using default configurations both for Debezium and Mysql.</p><h3>Future improvement</h3><p>It is possible to improve the solution by optimizing some of the sub-components of the system, for instance, our <strong>Database Storage. </strong>By creating an archiving process for old messages, or by creating partitions by days to clean up old partitions.</p><p>Additional optimizations can also be done on the <strong>Scheduler application, </strong>by optimizing the update query to handle millions of messages at once.</p><p>Lastly, this solution can be open source as it is a general delayed message mechanism for Apache Kafka.</p><h3>Wrapping up</h3><p>In this article, we explored how to utilize the <strong>Debezium Change Data Capture</strong> to solve architecture challenges. I believe the CDC pattern is very powerful, especially when you combine Kafka and Debezium (Kafka Connect), and will be embraced by more companies in the future. <br>We used it to solve our <strong>delayed messages</strong> problem, but there are many other patterns over and above CDC, such as Outbox pattern, and distributed cache eviction.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d1a105160c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yotpoengineering/scheduling-millions-of-messages-with-kafka-debezium-6d1a105160c">Scheduling Millions Of Messages With Kafka &amp; Debezium</a> was originally published in <a href="https://medium.com/yotpoengineering">Yotpo Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>