<?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 Vineet Narayan on Medium]]></title>
        <description><![CDATA[Stories by Vineet Narayan on Medium]]></description>
        <link>https://medium.com/@vineetisone?source=rss-1fcc3db1da7e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cSDJ4qZsu8eM168D</url>
            <title>Stories by Vineet Narayan on Medium</title>
            <link>https://medium.com/@vineetisone?source=rss-1fcc3db1da7e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:38:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vineetisone/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[Delayed Kafka Producer]]></title>
            <link>https://medium.com/airteldigital/delayed-kafka-producer-d9564426192c?source=rss-1fcc3db1da7e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9564426192c</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[aerospike]]></category>
            <dc:creator><![CDATA[Vineet Narayan]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 20:42:59 GMT</pubDate>
            <atom:updated>2023-01-31T09:29:53.305Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*ECr9UMs-HomT5tnmzXEyYQ.png" /><figcaption>Kafka and Aerospike</figcaption></figure><p>We will focus on three things:-</p><ul><li>Why do we need such a mechanism</li><li>Solution for this problem</li><li>Other Application uses of this solution</li></ul><h3>Introduction</h3><p>We have a message but we don’t want to process the message right now rather after some time. Other messaging queue support delayed messages. Kafka does not natively support a delay in the message.</p><h3>Strategy</h3><p>Apart from Kafka, we have a database and cache layer in our system. We can leverage the functionality of the NO SQL database to store messages up to a certain duration. Aerospike, Redis, Mongo etc provide the functionality of storing messages with expiry. We can then attach a change notification listener available for these databases which pushes data to Kafka.</p><h3>Solution</h3><p><strong>Prerequisites</strong></p><p>Assume you have basic knowledge in:</p><ul><li>Kafka</li><li>Aerospike</li></ul><p>We will create a Delay Service to hold all the brains for giving delay and producing to the original topic. Delay service let you postpone the delivery of new messages to consumers for several seconds. If you send a message to the event_delayer_publisher topic, any messages that you send to the queue remain invisible to consumers for the duration of the delay period. Client integration would simply be producing the same message to the event_delayer_publisher along side meta-info of the original topic and the delay needed.</p><p>The high level would look like following:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7rzmixpvBxzPmGXhEFeQwA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-aBx55HLd1I1kjBvoLLDQ.png" /></figure><h3>Delay Service</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WVVGshosFI_ogVO4_dI3ng.png" /></figure><h4>Pre-processor Queue consumer</h4><p>The queue will accept the below message and process this message. Pre-processor will validate the request and save the message to the Aerospike in set kafka_message_cache. Id for the same message is saved to another set of Aerospike kafka_message_id, alongside expiry calculated based on current time and postTime. The idea is to expire the message in the kafka_message_id set and receive the same change notification in another consumer.</p><pre>Kafka client producer:-<br>Topic:- event_delayer_publisher<br>Message:-<br>{<br>    &quot;topic&quot;: &quot;topic_name&quot;, // original topic name<br>    &quot;key&quot;: &quot;key&quot;, // key to be used when producing to original topic after delay --optional<br>    &quot;value&quot;: {}, // message for the original topic<br>    &quot;headers&quot;: {}, // headers for the original topic --optional<br>    &quot;postTime&quot;: 12312031310, // epoch milli time when this message needs to produced<br>    &quot;failurePostCallbackUrl&quot;: &quot;&quot; // post webhook url for sending back message to client if system failed to produce/accept the message --optional<br>}<br><br>Delay Service Aerospike:-<br><br>set1:- kafka_message_cache<br>doc: {<br>  id=string,<br>  topic=string,<br>  key=string,<br>  value=object,<br>  headers=Map,<br>  postTime=long,<br>  failurePostCallbackUrl=string,<br>  tries=int<br>}<br><br>set2:- kafka_message_id<br>doc: {<br>  id=string<br>}<br></pre><h4>Aerospike:-</h4><p>Save the message in set1:kafka_message_cache without any expiry.<br>Save the message in set2:kafka_message_id with expiry equal to produce time.<br><em>Info</em>:- expiry in aerospike is in seconds, the id of both sets is same.<br>Change Notification:- Put change notification on set2.</p><h4>Aerospike-Kafka-Outbound</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fcaiz35_MiIzf4Ge8r5olA.png" /></figure><p>We will use Kafka outbound already distributed by Aerospike. Kafka outbound will listen to the change notification on set2 and then produce a message to Kafka.</p><h4>Post-processor Queue consumer</h4><p>The queue will accept messages from Kafka Outbound connector. The queue is going to receive writes and deletes on set2. The consumer is going to filter writes and only process delete requests. It will find the id from the expired message and use this id to fetch the message from set1. Now the message is ready to produce. It will read the JSON and convert it to Kafka value, header and topic and produce the message.</p><h4>Failure Handler</h4><p>Whenever retry on Aerospike saves or retry on Kafka production fails. The message will be sent back to the client using Rest API.</p><h4>Rescue Scheduler</h4><p>There might be a chance that the message might drop during this whole process. So, we need a short polling and a long polling scheduler to rescue those messages. To enable the scheduler you need to create a <strong>secondary index </strong>on the field postTime. Shorting polling will run every 10 seconds and poll for messages that should be produced between current-10s and current-2mins. Long polling will run every 30 mins and poll for messages that should be produced 2 hours and above ago.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tsr8aEbfFPb3V9EuVazBpg.png" /><figcaption>Delay Service Design</figcaption></figure><h3>Local Deployment:-</h3><p>I will be using ubuntu 20 for demo purpose:-</p><h3>Delay Service codebase</h3><p><a href="https://github.com/1vin33t1/java-utility">https://github.com/1vin33t1/java-utility</a></p><h3>Kafka</h3><p><a href="https://kafka.apache.org/quickstart">https://kafka.apache.org/quickstart</a></p><h3>Aerospike</h3><p>Aerospike Feature file:-</p><p>You can request a single server feature file from Aerospike using the below link. <a href="https://aerospike.com/get-started-aerospike-database/">https://aerospike.com/get-started-aerospike-database/</a></p><p>Open feature file and set asdb-change-notification to true</p><pre>asdb-change-notification         true</pre><p>Aerospike installation process:-</p><p><a href="https://aerospike.com/download/">https://aerospike.com/download/</a></p><pre># aerospike<br>wget -O aerospike.tgz &#39;https://www.aerospike.com/enterprise/download/server/latest/artifact/ubuntu20&#39;<br>mkdir aerospike-server-enterprise<br>tar -xvf aerospike.tgz --directory aerospike-server-enterprise<br>cd aerospike-server-enterprise<br>chmod +x asinstall<br>chmod +x dep-check<br>sudo ./asinstall</pre><p>save your feature file here:- /etc/aerospike/features.conf</p><p>Aerospike Config file:- /etc/aerospike/aerospike.conf</p><pre># Aerospike database configuration file for use with systemd.<br><br>service {<br>   proto-fd-max 15000<br>   feature-key-file /etc/aerospike/features.conf<br>}<br><br>logging {<br>   file /var/log/aerospike/aerospike.log {<br>      context any info<br>   }<br>}<br><br>network {<br>   service {<br>      address any<br>      port 3000<br>   }<br><br>   heartbeat {<br>      mode multicast<br>      multicast-group 239.1.99.222<br>      port 9918<br><br>      # To use unicast-mesh heartbeats, remove the 3 lines above, and see<br>      # aerospike_mesh.conf for alternative.<br><br>      interval 150<br>      timeout 10<br>   }<br><br>   fabric {<br>      port 3001<br>   }<br><br>   info {<br>      port 3003<br>   }<br>}<br><br>namespace test {<br>   replication-factor 1<br>   memory-size 3G<br>   default-ttl 3600<br>   nsup-period 3<br>   nsup-threads 3<br>   storage-engine device {<br>      file /opt/aerospike/data/test.dat<br>      filesize 5G<br>      data-in-memory true<br>   }<br>   set kafka_processed_id {<br>        stop-writes-count 100000<br>    }<br>}<br><br>xdr {<br>   dc kafkaDC {<br>      connector true<br>      node-address-port 127.0.0.1 8085<br>      namespace test {<br>         ship-only-specified-sets true<br>         ship-set kafka_message_id<br>         ship-nsup-deletes true<br>      }<br>   }<br>}</pre><pre>Information regarding nsup config:-<br>nsup-period 3 -&gt; namespace supervisor periodic run time<br>nsup-threads 3 -&gt; no. of thread for namespace supervisor<br>xdr -&gt; cross data replication<br>node-address-port 127.0.0.1 8085 -&gt; address of kafka outbound<br>namespace test -&gt; namespace you want to listen<br>ship-only-specified-sets -&gt; send change notification only for specified sets<br>ship-set -&gt; send notification of this set<br>ship-nsup-deletes -&gt; send notification also for set which are expired by nsup (namespace supervisor)</pre><pre># start aerospike <br>sudo systemctl start aerospike;</pre><h3>Aerospike-Kafka Outbound</h3><p>installation</p><pre># aerospike-kafka-outbound<br>wget &#39;https://download.aerospike.com/artifacts/enterprise/aerospike-kafka-outbound/5.0.1/aerospike-kafka-outbound-5.0.1.all.deb&#39;;<br>sudo dpkg -i aerospike-kafka-outbound-5.0.1.all.deb;<br>sudo systemctl enable aerospike-kafka-outbound;<br>sudo systemctl start aerospike-kafka-outbound;</pre><p>Kafka outbound config:- /etc/aerospike-kafka-outbound/aerospike-kafka-outbound.yml</p><pre># Change the configuration for your use case.<br>#<br># Refer to https://docs.aerospike.com/connect/kafka/from-asdb/configuring for details.<br><br># The connector&#39;s listening ports, TLS and network interface.<br>service:<br>  port: 8085<br><br># Format of the Kafka destination message.<br>format:<br>  mode: flat-json<br>  metadata-key: metadata<br><br># Aerospike record routing to a Kafka destination.<br>routing:<br>  mode: static<br>  destination: as_outbound_default_event<br>namespaces:<br>  test:<br>    format:<br>      mode: flat-json<br>      metadata-key: metadata<br>    sets:<br>      kafka_message_id:<br>        routing:<br>          mode: static<br>          destination: as_outbound_msgid_set<br><br># Kafka producer initialization properties.<br>producer-props:<br>  bootstrap.servers:<br>    - 127.0.0.1:9092<br>  acks: all<br>  retries: 2<br><br># The logging properties.<br>logging:<br>  file: /var/log/aerospike-kafka-outbound/aerospike-kafka-outbound.log</pre><h3>Setup Video</h3><p><a href="https://youtu.be/TpW5g2VAO5g">https://youtu.be/TpW5g2VAO5g</a></p><h3>Pros &amp; Cons</h3><p><strong>Pros:</strong>-</p><ul><li>Single delay service for multiple clusters of Kafka</li><li>No need for a separate topic for the delay</li><li>Dynamic delay for each message</li><li>Simple and robust client integration</li><li>Exposing REST-API for saving messages, can be used as a simple fallback for Kafka downtime</li><li>Delay can be up to 5 year</li></ul><p><strong>Cons:</strong>-</p><ul><li>Delays are not precise and have an error rate of 1–3 secs and sometimes 10 secs if rescued by the scheduler.</li><li>Aerospike space limitations can be mitigated using hybrid memory.</li><li>Aerospike is the single point of failure but it is resilient against network partition and the service is sending failures back to the client.</li><li>Messages are only virtually queued and do not guarantee FIFO</li></ul><h3>Production Experience in Airtel:-</h3><p>We have to migrate from RabbitMQ to Kafka and one of the requirements during migration is to have an in-house solution for delayed messages in Kafka using already available tools. I have been using the above design for the delayed production of messages in Airtel for 2 years. We produce around 10 million records per day in this service. As stated above message production is not precise and there will be an error rate of 1–3 secs. In a week, we see around 100 messages rescued by Scheduler mainly during high TPS. This service saved us many times when one of the Kafka brokers is down and this is used as a fallback to retry those messages. We have injected the delay service Rest API in Kafka failure callback to let it retry from the delay service. We have used it for retry mechanisms (for eg:- enquiry, fulfilment etc.), subscription, snooze notifications and simple Kafka fallback.</p><h3>How to Integrate your Kafka with Delay Queue in Airtel :-</h3><ul><li>You need to raise request for consumer and topic creation in your cluster for event_delayer_publisher topic.</li><li>After consumer and topic creation, you just need to send the message into event_delayer_publisher as per below contract. You have successfully integrated with Delay Queue.</li></ul><pre>Kafka client producer:-<br>Topic:- event_delayer_publisher<br>Message:-<br>{<br>    &quot;topic&quot;: &quot;topic_name&quot;, // original topic name where message needs to be produced<br>    &quot;key&quot;: &quot;key&quot;, // key to be used when producing to original topic after delay --optional<br>    &quot;value&quot;: {}, // message for the original topic<br>    &quot;headers&quot;: {}, // headers for the original topic --optional<br>    &quot;postTime&quot;: 12312031310, // epoch milli time when this message needs to produced<br>    &quot;failurePostCallbackUrl&quot;: &quot;&quot; // post webhook url for sending back message to client if system failed to produce/accept the message --optional<br>}</pre><ul><li>Kibana URL for both Pre-Prod and Prod are available.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9564426192c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/airteldigital/delayed-kafka-producer-d9564426192c">Delayed Kafka Producer</a> was originally published in <a href="https://medium.com/airteldigital">Airtel Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>