<?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 Joel on Medium]]></title>
        <description><![CDATA[Stories by Joel on Medium]]></description>
        <link>https://medium.com/@joeljames?source=rss-2e1958317a0c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1Y-8Vzjl2SImHedM.jpeg</url>
            <title>Stories by Joel on Medium</title>
            <link>https://medium.com/@joeljames?source=rss-2e1958317a0c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:17:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joeljames/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[Kafka Key Concepts with Producer Consumer -Java Example]]></title>
            <link>https://medium.com/@joeljames/kafka-key-concepts-with-producer-consumer-java-example-7106a862056?source=rss-2e1958317a0c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7106a862056</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[pub-sub]]></category>
            <dc:creator><![CDATA[Joel]]></dc:creator>
            <pubDate>Tue, 21 Apr 2020 01:21:27 GMT</pubDate>
            <atom:updated>2020-04-21T01:25:58.965Z</atom:updated>
            <content:encoded><![CDATA[<p>Kafka is a high throughput distributed messaging system. Often time, large enterprises having large numbers of services which require data to operate on. In a microservice architecture, when an event happens within a service, other dependent services has to be notified about it to update their state or to perform an action.</p><p>As enterprise scales, data volume increases, and moving high volume of data between services in a scalable fashion becomes challenging. Kafka is a viable tool to consider in such scenario to move data between services and datastores.</p><h4>Kafka Topics</h4><p>Topics are named feed which span over an entire cluster of brokers. Behind each topic, the Kafka cluster maintains one or more physical log files. Messages are appended to a time ordered sequential stream, when producers send messages to a topic. Messages are stored in a binary format and are immutable. All the messages sent to a topic are retained by default for 7 days. The retention period is configurable and can be configured per topic. Consumers are responsible for reading messages and processing them from the topic. They use the message offset to maintain the last read position when consuming messages from a common topic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*GoRlq7O8qMNui6tvnq30cg.png" /><figcaption><a href="https://kafka.apache.org/intro">https://kafka.apache.org/intro</a></figcaption></figure><h4>Kafka Partitions and Brokers</h4><p>A Kafka topic can be divided into one or more partitions, and the number of partitions per topic is configurable. Partitions allow parallelism across topics.</p><p>Each Broker holds a list of partitions. Out of that, one partition will be the leader. All read and write to a topic goes through the leader, and the leader coordinates updating the replicas. If a leader fails, a replica in the list is assigned as the new leader.</p><h4>Replication Factor</h4><p>In order to be fault tolerant, Kafka supports replicating data across multiple servers. Replication can be configured per topic. Let’s say, we specify a replication factor of 3 when creating a topic. Kafka will copy the data across 3 servers so that if one fails, you will still have two copies of your data sitting on 2 different servers.</p><h4>Installing Kafka</h4><p>Make sure you have Scala installed since Kafka is mostly written in Scala. Get the latest Kafka binary from <a href="https://kafka.apache.org/downloads">here</a>. Un-pack it and copy the contents over to the system bin directory eg: /usr/local/bin/kafka. The unpacked contents will have the configs directory which contains the default configs, and bin directory which contains scripts required to get Kafka up and running.</p><h4>Starting Kafka with 3 Brokers and 3 Partitions on Local Machine</h4><p>Start the ZooKeeper server before starting the Kafka server</p><pre>bin/zookeeper-server-start.sh config/zookeeper.properties</pre><p>Since, we are going to start up 3 brokers, we will need 3 separate conf/server.properties. Create 3 separate files and copy the contents of conf/server.properties into those files. In order to avoid server start-up conflicts, change the log.dirs, broker.id and listeners properties in those files to be unique. Now lets start 3 brokers in 3 separate terminals.</p><pre>bin/kafka-server-start.sh config/server-{0}.properties</pre><p>Create a topic with 3 replications and 3 partitions:</p><pre>bin/kafka-topics.sh --create --topic my-topic --zookeeper localhost:2181 --replication-factor 3 --partitions 3</pre><p>Now, the server is up and running, and ready to receive messages for the define topic. Run the command below to check details of the topic:</p><pre>bin/kafka-topics.sh --describe —topic my-topic --zookeeper localhost:2181</pre><h4>Kafka Producer</h4><p>It is responsible for publishing messages to Kafka topics. Clone <a href="https://github.com/joeljames/kafka-getting-started/tree/master/src/main/java">this</a> repository to get the producer application.</p><p>Below example demonstrates producer application which will publish 100 messages to the topic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8700b64c9f6165bb5edd97f4f03c2991/href">https://medium.com/media/8700b64c9f6165bb5edd97f4f03c2991/href</a></iframe><p>At first, we defined the required Kafka producer properties.</p><ol><li>bootstrap.servers: Your broker addresses. Producer uses this to determine the cluster membership, partitions, leaders, etc.</li></ol><p>2. key and value serializers: Class used for message key and value serialization. This is used to optimize the size of the message for network transmission, storage, and compression.</p><p>Now, we instantiate the KafkaProducer, and create a ProducerRecord with:</p><ol><li>topic: Kafka topic which the producer record will be sent to.</li><li>key: This argument is optional, and should only be specified if you want all the messages to go to a single partition. Use this if your application requires message ordering.</li><li>message: The message you want to send.</li></ol><p>When the send method is called on the producer, the producer will first reach out to the cluster to discover cluster membership, using the bootstrap.servers property. It will get a metadata response back about the topics, partitions, and managing brokers on the cluster. It will keep this metadata up to date through out its life cycle.</p><p>Next, the data will pass through the serializer and the serialized message will be sent to the partitioner. The partitioner uses the below strategies to decide which partition the data should be sent to:</p><ol><li>Direct: Sends message directly to the defined partition.</li><li>Round-Robin: Sends message in a round robin fashion across partitions.</li><li>Key Mod-hash: Holds a map of key to partition, and all messages with the specified key goes to the defined partition.</li></ol><p>4. Custom: The custom defined strategy.</p><p>Next, the data will reach the record accumulator where the records will be batched for a configured time period. The batched data is then dispatched to the broker. If the broker responds with an error, the message sending is retried. Retry wait period can be configured in the producer properties.</p><p>Producer can also set the level of acknowledgement it should receive from the broker:</p><ol><li>0: Fire and Forget: Fastest, but not reliable</li><li>1: Only leader broker to acknowledge</li><li>2: All in sync replicas should confirm. The slowest performance</li></ol><h4>Kafka Consumer</h4><p>It is responsible for consuming messages from a topic and processing it. You can also have a group of consumers reading from unique partitions of a given topic. The group as a whole consumes all messages from the entire topic. Clone <a href="https://github.com/joeljames/kafka-getting-started/tree/master/src/main/java">this</a> repository to get the consumer application.</p><p>Below example demonstrates a consumer application where a group of consumers consumes messages from the defined topic.</p><p>It uses the Java Executors to spans 3 threads. Each thread starts a consumer process which polls message from the defined topic. The 3 consumers are a part of the consumer group (see consumer property: group.id), and will be polling messages from the same topic, but 3 different partitions. The consumer consumes the messages and print the thread name, topic, partition, offset, key, and value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8b508691842f518120b95b3c3c5ac09/href">https://medium.com/media/c8b508691842f518120b95b3c3c5ac09/href</a></iframe><p>We defined the required Kafka consumer properties.</p><ol><li>bootstrap.servers: Your broker addresses.</li><li>key and value deserializer: Class used for deserializing message key and value. This should match the serialization from the producer.</li><li>group.id: Unique string that identifies the consumer group.</li><li>enable.auto.commit: The consumer will auto-commit the largest offset it knows of from the polling.</li></ol><p>Poll method on the consumer is single threaded. When it is called, the fetcher connects to the broker, and keeps the tcp connection open until timeout. Fetcher buffers the messages, deserializes it, and makes it available for consumption. Consumer keeps track of the offset to determine what it has read. Kafka stores the committed offset in a topic called __consumer_offsets.</p><p>Offset commit behavior is configurable. By default, the consumer will commit the largest offset it knows from polling. Optionally, you can move the offset after processing the record batch by calling the commitSync or commitAsync call on the consumer.</p><h4>Try it out</h4><p>The java code for producer and consumer application can be found <a href="https://github.com/joeljames/kafka-getting-started">here</a>. Clone the repository and try out the producer consumer application.</p><p>By now, you should have your Kafka cluster up and running locally (refer to starting Kafka in the above section). Now, start your consumer application (KafkaConsumerWithThreads.java). This will start up 3 KafkaConsumer on three different Java threads, polling messages from the defined topic.</p><p>Now, start your producer application (KafkaProducerApp). The producer will start publishing messages, and your consumer application will start printing those messages. Since the topic has been created with 3 partitions, you will see each consumer thread has been assigned a partition, and read from the assigned partition.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7106a862056" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Way to Monitor Celery Queues in Production Apps — LocalMed Engineering]]></title>
            <link>https://medium.com/localmed-engineering/simple-way-to-monitor-celery-queues-in-production-apps-localmed-engineering-af21e79df78a?source=rss-2e1958317a0c------2</link>
            <guid isPermaLink="false">https://medium.com/p/af21e79df78a</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Joel]]></dc:creator>
            <pubDate>Fri, 11 Mar 2016 21:25:41 GMT</pubDate>
            <atom:updated>2016-08-12T14:19:18.537Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="http://docs.celeryproject.org/en/latest/index.html">Celery is a Python Library</a> that provides simple and efficient background processing for Python applications. While Celery works great at processing background jobs, sometimes things go wrong — thus, it is important to monitor Celery queue in a production environment. This post will demonstrate an easy way to monitor the Celery job queue, allowing your team to take timely action when the jobs get stuck or breakdown.</p><p>One way to monitor the health of Celery queue is to monitor the amount of jobs waiting in the queue. However, the number of queued jobs can vary depending on the traffic to the application. It is up to you and your team to determine the number of jobs that can be queued at any time and if that number is exceeded you will know Celery is in bad shape.</p><p>The basic idea of how to do this is to create a route that will check if the queue length is within your max limit, and constantly monitor the route using a monitoring service. The main objective of the route is to alert the team when there is something wrong with the the background jobs.</p><h4>Monitoring Celery queue in Django Application</h4><p>Let’s say our queue can have a maximum of 10 waiting jobs, any number greater than 10 in the queue would indicate a problem with background job processing. To assess our situation we’ll have a route that monitors the Celery queue and return an HTML page with the job summary as well as a couple of status codes — <em>200 OK</em> if the number is less than our maximum queue and <em>503 Unavailable</em> if the service has exceeded our cap.</p><p>For this example we’ll be creating a health app written in Django and we’ll use Redis as our <a href="http://docs.celeryproject.org/en/latest/getting-started/brokers/"><em>Celery broker</em></a>. You can run “python manage.py startapp health” to create the new health app. Once we have created the new app, lets add the default settings for the health app.</p><p>app/settings.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d44fda4015d017247f7186dfdb350c42/href">https://medium.com/media/d44fda4015d017247f7186dfdb350c42/href</a></iframe><p>The settings set the max queue size limit at line 10, and the queue names which are to be monitored a lines 17–20. Now let’s add the health app views.</p><p>health/views.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9cc8eabb4d5176064a61d2b76c69853/href">https://medium.com/media/b9cc8eabb4d5176064a61d2b76c69853/href</a></iframe><p>This creates a view that connects to the Redis server that will check to see if monitoring queues are within the range. Once connected to the Redis server, it checks the length of the queue by calling Redis <a href="http://redis.io/commands/LLEN"><em>llen</em></a> with the queue name.</p><p>In Redis, queue keys only exists when there are tasks in them, so if a key does not exist it simply means there are no messages in that queue. This is because in Redis a list with no elements in it is automatically removed, and hence it won’t show up in the keys command output, and llen for that list returns 0.</p><p>Now let’s define the template which will display the queue size summary.</p><p>templates/health/status_queue.djhtml</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/511bc602bb6ed19b7143647308a9611f/href">https://medium.com/media/511bc602bb6ed19b7143647308a9611f/href</a></iframe><p>This template prints the queue summary with each queue name and queue size.</p><p>For our last step, let’s define the route which will display the view when the url is accessed.</p><p>health/urls.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb8c3bccc2221dcd8487b55d327ab324/href">https://medium.com/media/cb8c3bccc2221dcd8487b55d327ab324/href</a></iframe><p>And that’s all there is to it. We now have a Django application which connects to a Redis server to monitor our Celery queue. Of course, it’s up to you and your team, as well as your needs and your application’s needs for which settings you should use. This is a great first step.</p><h4>Configuring the Monitoring Service</h4><p>The configuration of the monitoring service will vary depending on the monitoring service you are using. The basic is the same: we want our monitor to alert our team if the HTTP response status is 503 when the route we have just implemented is accessed. We use <a href="https://newrelic.com/">NewRelic</a> monitoring service which periodically pings the queue route we defined above. If NewRelic gets HTTP status code of 503 from the health app then an alert is sent to our <a href="https://slack.com/">Slack</a> channel, and our team is alerted. There are also a few other services for monitoring the routes such as <a href="http://uptimerobot.com/">UptimeRobot</a> and <a href="http://uptimerobot.com/">pingdom</a>.</p><p>If you are using NewRelic to monitor then the settings would look something like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*97Fkvr4ssq3xnL7IS-roxA.png" /><figcaption>NewRelic monitoring service screenshot</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af21e79df78a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/localmed-engineering/simple-way-to-monitor-celery-queues-in-production-apps-localmed-engineering-af21e79df78a">Simple Way to Monitor Celery Queues in Production Apps — LocalMed Engineering</a> was originally published in <a href="https://medium.com/localmed-engineering">LocalMed Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>