<?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 Łukasz Antoniak on Medium]]></title>
        <description><![CDATA[Stories by Łukasz Antoniak on Medium]]></description>
        <link>https://medium.com/@lukasz.antoniak?source=rss-521084ca187------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hv-1uUHN2lr-55Iq.jpg</url>
            <title>Stories by Łukasz Antoniak on Medium</title>
            <link>https://medium.com/@lukasz.antoniak?source=rss-521084ca187------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:57:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lukasz.antoniak/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[Apache Kafka leaves the Zoo]]></title>
            <link>https://medium.com/@lukasz.antoniak/apache-kafka-leaves-the-zoo-bef529ba82b7?source=rss-521084ca187------2</link>
            <guid isPermaLink="false">https://medium.com/p/bef529ba82b7</guid>
            <category><![CDATA[zookeeper]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[distributed-computing]]></category>
            <category><![CDATA[consensus]]></category>
            <dc:creator><![CDATA[Łukasz Antoniak]]></dc:creator>
            <pubDate>Sat, 02 Feb 2019 18:07:18 GMT</pubDate>
            <atom:updated>2019-03-27T12:52:50.258Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ieqK84Nz4peXcrssM9qyhw.png" /></figure><p>Apache Kafka has become de facto the standard system for brokering messages in highly available environments. The promise of linear scalability and fault tolerance implied the need of distributed architecture, which typically involves coordination between nodes. Creators of Kafka leveraged Apache ZooKeeper to guarantee strong consistency of cluster state, its configuration and discovery of peers. ZooKeeper dependency confuses newcomers and makes Kafka deployment more complex. Hereby essay describes the process of replacing ZooKeeper with Atomix, a reactive Java framework for building fault-tolerant distributed systems.</p><p><strong>Strong consistency, required</strong></p><p>Modern distributed systems often distinguish between data that requires strong consistency guarantees, and information that may become coherent at some point in the future. Apache Kafka defines different protocol for handling updates of cluster-level metadata (e.g. cluster state, topic configuration, ISR list) and separate mechanism for replicating topic partitions between brokers. Cluster metadata is modified infrequently, its size does not exceed few megabytes, but every node needs to have a consistent view at any point in time. Updates to meta-information shall not appear on common read or write request path, not to degrade performance. On the contrary, Kafka records stored in topics are constantly appended, partition size can exceed gigabytes, but only subset of nodes manage given topic-partition.</p><p>For the remaining part of the article, let us focus on the sub-system that will manage cluster metadata in strongly consistent way. In terms of <a href="https://en.wikipedia.org/wiki/CAP_theorem">CAP theorem</a>, the module needs to fall under “CP” category. This means that we trade off availability in favor of consistency and tolerance of network partitioning. It seems better for the system to become shortly unavailable, instead of running into split-brain situation, which usually causes data corruption. The solution requires quorum of nodes (n/2 +1) to be reachable to operate correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-I-2LIuweuxeys5rALbLJQ.png" /><figcaption>Network partitioning in Apache Kafka cluster</figcaption></figure><p><strong>Few words on ZooKeeper</strong></p><p>Creators of Apache Kafka decided to leverage Apache ZooKeeper as a cluster coordination service. ZooKeeper provides configuration management capabilities, distributed synchronization, group membership and leader election receipts. Its data model represents a set of nodes (called “znodes”) organized in hierarchical tree-like data structure. Every node can have mix of different characteristics — persistent, ephemeral or sequential — and contain arbitrary payload stored as binary byte array. Apache Kafka persists data in human-readable JSON format. If you have not done already, download official ZooKeeper distribution which provides CLI interface to interact with the service. Below example presents configuration of Kafka topic, its number of partitions and assignment to brokers:</p><pre>[zk: localhost:2181(CONNECTED) 0] get /brokers/topics/my-topic<br><strong>{&quot;version&quot;:1,&quot;partitions&quot;:{&quot;1&quot;:[0,1],&quot;0&quot;:[1,0]}}<br></strong>cZxid = 0x32<br>ctime = Wed Jan 30 22:11:49 PST 2019<br>mZxid = 0x32<br>mtime = Wed Jan 30 22:11:49 PST 2019<br>pZxid = 0x34<br>cversion = 1<br>dataVersion = 0<br>aclVersion = 0<br><strong>ephemeralOwner = 0x0<br></strong>dataLength = 48<br>numChildren = 1</pre><p>Nodes of ephemeral type disappear once client session (Kafka broker) that owns given znode disconnects from the cluster. ZooKeeper provides the mechanism of one-time watches that enables clients to receive notifications once znode representing certain path, or any of its children, is created, updated or removed. Apache Kafka implements service discovery using ephemeral data and watches. Every broker subscribes to child notifications of <em>/brokers/ids</em> to receive an event whenever peers leave or join the cluster. Exactly one of Kafka instances within the cluster acts as controller who manages state of partitions, replicas and performs administrative tasks. Every broker is eligible to become controller and watches for removal of ephemeral <em>/controller</em> node. Upon notification of key deletion, server initiates leader election process. The first engine that successfully creates new znode wins the election.</p><pre>[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids<br>[0, 1]</pre><pre>[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/0<br><strong>{&quot;listener_security_protocol_map&quot;:{&quot;PLAINTEXT&quot;:&quot;PLAINTEXT&quot;},&quot;endpoints&quot;:[&quot;PLAINTEXT://192.168.1.8:9092&quot;],&quot;jmx_port&quot;:-1,&quot;host&quot;:&quot;192.168.1.8&quot;,&quot;timestamp&quot;:&quot;1548914820968&quot;,&quot;port&quot;:9092,&quot;version&quot;:4}<br></strong>cZxid = 0x18<br>ctime = Wed Jan 30 22:07:01 PST 2019<br>mZxid = 0x18<br>mtime = Wed Jan 30 22:07:01 PST 2019<br>pZxid = 0x18<br>cversion = 0<br>dataVersion = 0<br>aclVersion = 0<br><strong>ephemeralOwner = 0x1000005c2fd0000<br></strong>dataLength = 192<br>numChildren = 0</pre><pre>[zk: localhost:2181(CONNECTED) 2] get /controller<br><strong>{&quot;version&quot;:1,&quot;brokerid&quot;:0,&quot;timestamp&quot;:&quot;1548914821132&quot;}<br></strong>cZxid = 0x1a<br>ctime = Wed Jan 30 22:07:01 PST 2019<br>mZxid = 0x1a<br>mtime = Wed Jan 30 22:07:01 PST 2019<br>pZxid = 0x1a<br>cversion = 0<br>dataVersion = 0<br>aclVersion = 0<br><strong>ephemeralOwner = 0x1000005c2fd0000<br></strong>dataLength = 54<br>numChildren = 0</pre><p>Sequential znodes guarantee unique names by automatically assigning a sequence number suffix. Apache Kafka uses various nodes to broadcast cluster state changes, e.g. <em>/isr_change_notification</em> for updating ISR list of topic-partitions. Sequential nodes guarantee entry ordering and form a foundation of ZooKeeper <a href="https://zookeeper.apache.org/doc/current/recipes.html#sc_recipes_Queues">queue recipe</a>. Example value of notification about ISR change captured while running Kafka inside IDE:</p><pre>/isr_change_notification/isr_change_0000000015</pre><pre>{<br>   &quot;version&quot;: 1,<br>   &quot;partitions&quot;: [<br>      { &quot;topic&quot;: &quot;__consumer_offsets&quot;, &quot;partition&quot;: 28 },<br>      { &quot;topic&quot;: &quot;__consumer_offsets&quot;, &quot;partition&quot;: 22 }<br>   ]<br>}</pre><p>Kafka brokers heavily depend on connectivity with ZooKeeper ensemble. If the link breaks, users may experience messages lost, but luckily only with relaxed setup of acks property on producer side (different than all). I recommend reading an article series by Jack Vanlightly, who performed extensive <a href="https://jack-vanlightly.com/blog/2018/9/14/how-to-lose-messages-on-a-kafka-cluster-part1">Kafka resilience testing</a>.</p><p>To summarize, Apache ZooKeeper is an excellent tool for managing data in strongly consistent fusion. It has been used by number of big data Apache projects in Hadoop ecosystem (e.g. Hadoop MapReduce, HBase, Solr, Neo4J, Mesos). But in 2019, we may have other cool options…</p><p><strong>Welcome Atomix, Swiss knife for distributed systems</strong></p><p><a href="https://atomix.io/">Atomix</a> is an event-driven Java framework which solves problems commonly encountered by developers of distributed systems. It provides distributed version of well known Java primitives such as maps, locks and counters which are backed by one of distributed protocols — Raft, Gossip or Primary-Backup. Library ships building blocks that enable implementation of cluster management, group membership, leader election, distributed concurrency control, partitioning, and replication.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1150734664cd39bc5e3fca0ecd40df31/href">https://medium.com/media/1150734664cd39bc5e3fca0ecd40df31/href</a></iframe><p>We require strong consistency guarantees from ZooKeeper replacement, so the natural choice points us to Raft protocol for storing Kafka metadata. Atomix implements enhanced <a href="https://www.cockroachlabs.com/blog/scaling-raft">Multi-Raft</a> algorithm that partitions key set among configurable number of independent consensus groups, and coalesces all heartbeats of single node to minimize network traffic. Each group has its own Raft leader. ZooKeeper maintains only one leader who broadcasts updates to all replicas. Detailed explanation of ZAB (ZooKeeper Atomic Broadcast) and Raft algorithms is beyond the scope of hereby article, but both protocols have very much in common. In fact the major difference between basic Raft version and ZAB is that the later synchronizes followers with newly elected leader before normal operation phase, whereas Raft simplifies algorithm states and performs mentioned activity as part of normal operation mode.</p><p>Curious readers may wonder why to replace ZooKeeper with Atomix? Best answer is the simplicity of deployment and architecture. Kafka would no longer rely on external service, which simplifies Kubernetes manifests. For small and medium size cluster, every node could have identical characteristics (broker and Raft node), there would be no split between brokers and ZooKeeper servers while thinking about Kafka infrastructure. Large deployments may choose only a subset of hosts to participate as Raft nodes in consensus groups due to performance reasons. The larger the cluster, the longer it takes for quorum (n/2 + 1) of nodes to acknowledge write requests.</p><p><strong>And so they code…</strong></p><p>Making extensive changes to code base of big projects such as Apache Kafka, can be a challenging activity. Luckily interaction with ZooKeeper is mostly handled by two Scala classes — kafka.zk.KafkaZkClient and kafka.zookeeper.ZooKeeperClient. KafkaZkClient provides over 100 logical methods such as setting controller epoch, triggering update of partition leader and ISR list, or discovering all reachable brokers in the cluster. On the other hand, ZooKeeperClientis responsible for purely technical aspects — sending requests to ZooKeeper, notifying whenever watch has been executed, reconnection with the ensemble upon network disruption etc. Following the pragmatic approach, I have decided to reimplement ZooKeeperClient and leverage Atomix framework. Of course, proposed decision has its cons. From design perspective it is far from being perfect. ZooKeeper JAR remains on the classpath as Atomix response codes and exceptions have to be translated to ZK domain. The main advantage is the ease of maintenance while pulling changes from upstream Kafka Git repository.</p><p>I have decided to store Kafka metadata in io.atomix.core.map.AtomicMap whose key corresponds to path of the znode and value represents binary data. Support of additional ZooKeeper features required introduction of few helper data structures:</p><ul><li>io.atomix.core.map.AtomicCounter to generate unique identifiers of client session that will be further assigned as owner of ephemeral data.</li><li>io.atomix.core.map.AtomicCounterMap to produce sequence numbers for sequential nodes.</li><li>io.atomix.core.multimap.AtomicMultimap to maintain mapping of all ephemeral paths actively owned by given broker.</li><li>io.atomix.core.lock.AtomicLock guarantees exclusive write access to AtomicMap and AtomicMultimap during the process of stale ephemeral data deletion.</li></ul><p>Atomix distributed map API provides the ability to conditionally modify value of given key using optimistic locking, which resolves the problem of concurrent updates. The biggest challenge was the implementation of ephemeral data as Atomix does not ship hereby feature out-of-the-box. Having the choice of setting Time-To-Live on AtomicMap keys and periodically refresh those before expiration, or manually removing stale data based on notifications from Atomix membership protocol, I have decided to continue with the second option. Implementation details can be viewed on my GitHub repository — <a href="https://github.com/lukasz-antoniak/kafka">https://github.com/lukasz-antoniak/kafka</a>.</p><p><strong>Demo time!</strong></p><p>The easiest way to play with “Kafka-Atomix” distribution, is to download source code from <a href="https://github.com/lukasz-antoniak/kafka">Git repository</a> and navigate to <em>config/kafka-atomix</em> folder. You will find a simple Docker Compose manifest that provisions three node Kafka cluster. Script assumes that you have build the image locally from source, so just replace all occurrences of image: &#39;kafka-atomix:latest&#39; with image: &#39;lantonia/kafka-atomix:latest&#39; in <em>docker-compose.yml</em> to pull ready to use binaries from Docker Hub. Execute $ docker-compose up to begin the journey…</p><p>First let us create “test” topic, send and receive some messages.</p><pre>$ docker exec -it kafka0 /bin/bash</pre><pre># ./bin/kafka-topics.sh --create --zookeeper atomix:///volume/kafka/config/atomix-admin.conf --replication-factor 2 --partitions 2 --topic test</pre><pre># ./bin/kafka-console-producer.sh --broker-list kafka0:9092 --topic test --request-required-acks -1</pre><pre># ./bin/kafka-console-consumer.sh --bootstrap-server kafka0:9092 --topic test --from-beginning</pre><p>You may inspect the current state of the cluster by viewing active brokers and elected controller.</p><pre># ./bin/kafka-cluster.sh --atomix /volume/kafka/config/atomix-admin.conf --describe --keys /brokers/ids,/controller --include-children</pre><pre>10 /brokers/ids {&quot;data&quot;:&quot;[null]&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>52 <strong>/brokers/ids/0 </strong>{&quot;data&quot;:&quot;{\&quot;listener_security_protocol_map\&quot;:{\&quot;PLAINTEXT\&quot;:\&quot;PLAINTEXT\&quot;},\&quot;endpoints\&quot;:[\&quot;PLAINTEXT://kafka0:9092\&quot;],\&quot;jmx_port\&quot;:-1,\&quot;host\&quot;:\&quot;kafka0\&quot;,\&quot;timestamp\&quot;:\&quot;1549061407166\&quot;,\&quot;port\&quot;:9092,\&quot;version\&quot;:4}&quot;,&quot;version&quot;:0,&quot;owner&quot;:2}<br>30 <strong>/brokers/ids/1 </strong>{&quot;data&quot;:&quot;{\&quot;listener_security_protocol_map\&quot;:{\&quot;PLAINTEXT\&quot;:\&quot;PLAINTEXT\&quot;},\&quot;endpoints\&quot;:[\&quot;PLAINTEXT://kafka1:9092\&quot;],\&quot;jmx_port\&quot;:-1,\&quot;host\&quot;:\&quot;kafka1\&quot;,\&quot;timestamp\&quot;:\&quot;1549061406309\&quot;,\&quot;port\&quot;:9092,\&quot;version\&quot;:4}&quot;,&quot;version&quot;:0,&quot;owner&quot;:1}<br>51 <strong>/brokers/ids/2 </strong>{&quot;data&quot;:&quot;{\&quot;listener_security_protocol_map\&quot;:{\&quot;PLAINTEXT\&quot;:\&quot;PLAINTEXT\&quot;},\&quot;endpoints\&quot;:[\&quot;PLAINTEXT://kafka2:9092\&quot;],\&quot;jmx_port\&quot;:-1,\&quot;host\&quot;:\&quot;kafka2\&quot;,\&quot;timestamp\&quot;:\&quot;1549061407431\&quot;,\&quot;port\&quot;:9092,\&quot;version\&quot;:4}&quot;,&quot;version&quot;:0,&quot;owner&quot;:3}<br>37 <strong>/controller </strong>{&quot;data&quot;:&quot;{\&quot;version\&quot;:1,<strong>\&quot;brokerid\&quot;:1</strong>,\&quot;timestamp\&quot;:\&quot;1549061406676\&quot;}&quot;,&quot;version&quot;:0,&quot;owner&quot;:1}</pre><p>Login to the server currently acting as controller and simulate network partitioning.</p><pre>$ docker exec -it kafka1 /bin/bash</pre><pre># iptables -I INPUT -j DROP</pre><pre># iptables -I OUTPUT -j DROP</pre><p>Open command prompt of other two brokers. Inspect the state of the cluster. You should notice only two active instances and re-election of the controller. In-sync replica set of our “test” topic has also shrinked.</p><pre>$ docker exec -it kafka0 /bin/bash</pre><pre># ./bin/kafka-cluster.sh --atomix /volume/kafka/config/atomix-admin.conf --describe --keys /brokers/ids,/controller --include-children</pre><pre>10 /brokers/ids {&quot;data&quot;:&quot;[null]&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>52 <strong>/brokers/ids/0 </strong>{&quot;data&quot;:&quot;{\&quot;listener_security_protocol_map\&quot;:{\&quot;PLAINTEXT\&quot;:\&quot;PLAINTEXT\&quot;},\&quot;endpoints\&quot;:[\&quot;PLAINTEXT://kafka0:9092\&quot;],\&quot;jmx_port\&quot;:-1,\&quot;host\&quot;:\&quot;kafka0\&quot;,\&quot;timestamp\&quot;:\&quot;1549061407166\&quot;,\&quot;port\&quot;:9092,\&quot;version\&quot;:4}&quot;,&quot;version&quot;:0,&quot;owner&quot;:2}<br>51 <strong>/brokers/ids/2 </strong>{&quot;data&quot;:&quot;{\&quot;listener_security_protocol_map\&quot;:{\&quot;PLAINTEXT\&quot;:\&quot;PLAINTEXT\&quot;},\&quot;endpoints\&quot;:[\&quot;PLAINTEXT://kafka2:9092\&quot;],\&quot;jmx_port\&quot;:-1,\&quot;host\&quot;:\&quot;kafka2\&quot;,\&quot;timestamp\&quot;:\&quot;1549061407431\&quot;,\&quot;port\&quot;:9092,\&quot;version\&quot;:4}&quot;,&quot;version&quot;:0,&quot;owner&quot;:3}<br>219 <strong>/controller</strong> {&quot;data&quot;:&quot;{\&quot;version\&quot;:1,<strong>\&quot;brokerid\&quot;:0</strong>,\&quot;timestamp\&quot;:\&quot;1549061587798\&quot;}&quot;,&quot;version&quot;:0,&quot;owner&quot;:2}</pre><pre># ./bin/kafka-cluster.sh --atomix /volume/kafka/config/atomix-admin.conf --describe --keys /brokers/topics/test --include-children</pre><pre>76 <strong>/brokers/topics/test </strong>{&quot;data&quot;:&quot;{\&quot;version\&quot;:1,\&quot;partitions\&quot;:{\&quot;1\&quot;:[1,2],\&quot;0\&quot;:[0,1]}}&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>93 /brokers/topics/test/partitions {&quot;data&quot;:&quot;[null]&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>80 /brokers/topics/test/partitions/0 {&quot;data&quot;:&quot;[null]&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>213 <strong>/brokers/topics/test/partitions/0/state</strong> {&quot;data&quot;:&quot;{\&quot;controller_epoch\&quot;:1,\&quot;leader\&quot;:0,\&quot;version\&quot;:1,\&quot;leader_epoch\&quot;:0,<strong>\&quot;isr\&quot;:[0]</strong>}&quot;,&quot;version&quot;:1,&quot;owner&quot;:0}<br>97 /brokers/topics/test/partitions/1 {&quot;data&quot;:&quot;[null]&quot;,&quot;version&quot;:0,&quot;owner&quot;:0}<br>241 <strong>/brokers/topics/test/partitions/1/state</strong> {&quot;data&quot;:&quot;{\&quot;controller_epoch\&quot;:2,\&quot;leader\&quot;:2,\&quot;version\&quot;:1,\&quot;leader_epoch\&quot;:1,<strong>\&quot;isr\&quot;:[2]</strong>}&quot;,&quot;version&quot;:1,&quot;owner&quot;:0}</pre><p>Let us send few more messages. Finally restore network on partitioned container. You should see that broker successfully rejoins the cluster. ISR list of “test” topic expands.</p><pre>$ docker exec -it kafka1 /bin/bash</pre><pre># iptables -D INPUT 1</pre><pre># iptables -D OUTPUT 1</pre><p>To complete demo, shutdown Kafka cluster by invoking $ docker-compose down --volumes.</p><p><strong>Summary</strong></p><p>Hope you liked the demo! Well, I cannot yet recommend decommissioning your reliable Apache Kafka cluster and provisioning Kafka-Atomix distribution. More resilience tests need to be performed. I am looking forward to checkout <a href="https://github.com/worstcase/blockade">Blockade</a> which introduces randomized hardware failures into the cluster. Next step is to support Kafka 2.x, because current modifications apply only to branch 1.1. Kafka 2.x takes advantage of ZooKeeper multi() operation, which will most probably translate to Atomix transactions (two-phase commit protocol).</p><p>Are there any other attempts to replace ZooKeeper in Apache Kafka? Yes. <a href="https://banzaicloud.com/blog/kafka-on-etcd">Banzai Cloud</a> maintains a version that depends on <a href="https://coreos.com/etcd">etcd</a> (<a href="https://cwiki.apache.org/confluence/display/KAFKA/KIP-273+-+Kafka+to+support+using+ETCD+beside+Zookeeper">KIP-273</a>), whereas <a href="https://github.com/travisjeffery/jocko">Jocko</a> projects aims to completely rewrite Kafka in Go using Raft for consensus and Serf for service discovery.</p><p>Feel free to checkout project source code available on <a href="https://github.com/lukasz-antoniak/kafka">GitHub</a> and play with sample three node “dockerized” environment! Hopefully the project will make Kafka cluster even easier to deploy and maintain.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bef529ba82b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>