<?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 Swarnim Singhal on Medium]]></title>
        <description><![CDATA[Stories by Swarnim Singhal on Medium]]></description>
        <link>https://medium.com/@swarnimsinghal?source=rss-24112aa002ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*oiPBM0de6aEJnNiS.jpg</url>
            <title>Stories by Swarnim Singhal on Medium</title>
            <link>https://medium.com/@swarnimsinghal?source=rss-24112aa002ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 19:53:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@swarnimsinghal/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[Implementing Cassandra’s Gossip Protocol : Part 2]]></title>
            <link>https://medium.com/@swarnimsinghal/implementing-cassandras-gossip-protocol-part-2-56332db9177?source=rss-24112aa002ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/56332db9177</guid>
            <dc:creator><![CDATA[Swarnim Singhal]]></dc:creator>
            <pubDate>Wed, 02 Sep 2020 15:09:45 GMT</pubDate>
            <atom:updated>2020-09-02T15:09:45.020Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing Cassandra’s Gossip Protocol : Part 2</h3><h4>Implementing the three messages of Gossip round</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*OXI-T_OJvg574klugF8wUw.jpeg" /></figure><p>In the <a href="https://medium.com/@swarnimsinghal/implementing-cassandras-gossip-protocol-part-1-b9fd161e5f49">first part</a> of the blog series, I covered the various code constructs used to facilitate transfer of knowledge as gossip messages in Cassandra. The content covered in the first part was generic to some extent in the sense that it could be applied to any distributed system, whereas this article will cover concepts specific to Cassandra, (obviously) by extending the understanding built in the previous blog. So, please I highly recommend to read the first part once before you dive further.</p><p>Let’s start with the below image taken, which was also used in the first blog. As mentioned there, gossip in Cassandra is very similar to the TCP three-way handshake.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*c_dOeNwWk-e-nW7QWHMUtQ.png" /></figure><p>This part primarily focuses on what information in contained in each of the mentioned three messages and how nodes in the cluster use <em>handlers to update and exchange information</em> to make the magic happen.</p><blockquote>I will be using examples along with code to explain each of the steps. We will call node-X the initiator and node-Y the peer as node-X initiates the gossip round with its friend node-Y.</blockquote><p>Before going into the details of each of the message, it would be nice to get accustomed to a very simple code construct “<strong>GossipDigest</strong>” which consists of endpoint address, generation number and maximum version that has been seen for the endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e27c11920ca42f2e42550dede1b1a9b5/href">https://medium.com/media/e27c11920ca42f2e42550dede1b1a9b5/href</a></iframe><h4><strong>1. GossipDigestSynMessage</strong></h4><p>Node starting gossip exchange sends GossipDigestSynMessage, which includes a list of gossip digests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c08c8273dbbfb4d52e3ea5b37bb3de72/href">https://medium.com/media/c08c8273dbbfb4d52e3ea5b37bb3de72/href</a></iframe><blockquote>We have discussed in the first part that there is a Message Class which has generic payload, <strong>GossipDigestSyn</strong> is the payload in this case.</blockquote><blockquote>Another important thing I mentioned that each message has an associated verb which is mapped to a VerbHandler, <strong>GossipDigestSynVerbHandler</strong> is the handler in this case.</blockquote><pre>On receipt of the <strong>GossipDigestSyn </strong>message, peer node does the following : <br>1. Get the sender’s node address (X in this case). This is used to send the <strong>Ack</strong> response.<br>2. Extract out the gossipDigestList from the syn message’s.<br>3. Construct ACK response which has following two pieces of information<br>   a. deltaEpStateMap (EndpointState map)for nodes<br>     i.  which X doesn’t know about<br>     ii. where Y has newer information than X.<br>   b. List of gossip digest for nodes<br>     i.  which Y doesn&#39;t know about<br>     ii. which have a lower version in Y than X.<br>4. Send back the ACK response</pre><pre><strong>Note:</strong> The <em>newness</em> of information is determined using maxVersion sent in the gossip digest. <em>Greater the value, newer is the info.</em></pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44248623144c2b90700b723ac7ac4a2c/href">https://medium.com/media/44248623144c2b90700b723ac7ac4a2c/href</a></iframe><p>An<strong> Example </strong>would better explain this<strong>. (</strong>Reference: each tuple is a GossipDigest (address, generation, version).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zgy2z8avIjggxcScUmCYPA.png" /></figure><h4>2. GossipDigestAckMessage</h4><p>This Ack gets sent out as a result of the receipt of a GossipDigestSynMessage by an endpoint. This is the 2 stage of the 3 way messaging in the Gossip protocol.</p><pre>On receipt of the <strong>GossipDigestAck </strong>message, initiator node does the following : <br>1. Get the sender’s node address (Y in this case). This is used to send the <strong>Ack2</strong> response.<br>2. Extract out the gossipDigestList and EndPointStateMap from the Ack message&#39;s payload.<br>3. Construct Ack2 response which contains<br>   a. deltaEpStateMap (EndpointState map)for nodes<br>     i.  which Y doesn’t know about<br>     ii. where X has newer information than Y.<br>4. Send back the Ack2</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d0a72256d5c02c109b604e6060831a7/href">https://medium.com/media/9d0a72256d5c02c109b604e6060831a7/href</a></iframe><p>3. <strong>GossipDigestAck2Message</strong></p><p>This ack gets sent out as a result of the receipt of a <em>GossipDigestAckMessage</em>. This the last stage of the 3 way messaging of the Gossip protocol. <strong><em>GossipDigestAck2VerbHandler</em></strong> is pretty straightforward, the peer node (Y) receives the message and simply updates local state with the information from EndPointStateMap.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b55f25e65370dde2e7e89b0d20e0c459/href">https://medium.com/media/b55f25e65370dde2e7e89b0d20e0c459/href</a></iframe><p>This completes the round of gossip and both peer and the initiator nodes (node-X and node-Y for us) have now exchanged and updated information for the nodes they know about. They will carry this on with other nodes in the cluster, hence bringing the blog’s first universal truth to its complete circle</p><blockquote>Who gossips to you, will gossip about you</blockquote><p>Luckily for Cassandra, we want this to happen, in the reality of our everyday lives, <strong>not so much :P</strong></p><h3>Note to the reader</h3><blockquote>All the verb handlers have <strong>Gossiper.instance.someMethod()</strong> call. I have tried to explain what each of these calls does and implementations are pretty straight-forward, since code mostly involves comparisons based on generation and versions and nothing more. That said, <strong><em>the implementation details for Gossiper entity will be covered in the next article.</em></strong></blockquote><p>I hope this reading will further strengthen the concepts laid out in the first part and help you get a firmer grasp on the topic.</p><p>Feel free to post comments, <em>happy gossiping</em> till then :P</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56332db9177" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Cassandra’s Gossip Protocol : Part 1]]></title>
            <link>https://medium.com/@swarnimsinghal/implementing-cassandras-gossip-protocol-part-1-b9fd161e5f49?source=rss-24112aa002ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9fd161e5f49</guid>
            <category><![CDATA[nosql]]></category>
            <category><![CDATA[nosql-database]]></category>
            <category><![CDATA[cassandra]]></category>
            <category><![CDATA[gossip-protocol]]></category>
            <dc:creator><![CDATA[Swarnim Singhal]]></dc:creator>
            <pubDate>Wed, 04 Sep 2019 13:59:24 GMT</pubDate>
            <atom:updated>2019-11-08T08:51:31.971Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing Cassandra’s Gossip Protocol : Part 1</h3><h4>Lets learn while gossiping !😛</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/0*6PowiMOCnZDle485.jpg" /></figure><p>Well, after chilling around for a week in my notice period, I decided to do something useful and started reading a few blogs. Throughout the past 2 years of my career as a software developer , I have heard “<strong><em>office gossips” </em></strong>on how great Cassandra is. <strong><em>(pun absolutely intended)</em></strong></p><p>Cassandra is one of the most popular NoSql distributed databases currently and after skimming through some usual online tutorials, I decided to learn more by diving into the source code itself. The code base is huge and so I had to pick a topic to dig deeper, Gossip protocol was my choice, primarily because it is not just specific to Cassandra but is an important tool f<a href="http://highscalability.com/blog/2011/11/14/using-gossip-protocols-for-failure-detection-monitoring-mess.html">or Failure Detection, Monitoring, Messaging And Other Good Things</a> in a distributed setup in general.(<a href="http://highscalability.com/blog/2011/11/14/using-gossip-protocols-for-failure-detection-monitoring-mess.html"><em>ref</em></a><em>)</em></p><p>The motivation behind this post is that, I, as a developer have always been intrigued on how the code for distributed systems is written and I am sure there are more inquisitive engineers like me, who have the skill set to understand the source code, but in general, never bother. My intention here is to demonstrate the implementation to some extent for such people who are interested in learning <em>what drives Cassandra behind the wheels</em>. This post is by no means a tutorial on Cassandra’s Gossip Protocol, there are already quite a few of them out there, <strong>rather, this takes a curious developer, who already has some understanding, a step further </strong>by taking a look at various constructs used in the code to achieve the required functionality.</p><p>If one does not know what gossip protocol does, I will strongly suggest to go through <a href="https://www.linkedin.com/pulse/gossip-protocol-inside-apache-cassandra-soham-saha">this</a> and <a href="https://cwiki.apache.org/confluence/display/CASSANDRA2/ArchitectureGossip">this</a>.</p><h3><strong>the, Why ?</strong></h3><p>When building a system on top of a set of wildly uncooperative and unruly computers you have knowledge problems: knowing when other nodes are dead; knowing when nodes become alive; getting information about other nodes so you can make local decisions, like knowing which node should handle a request based on a scheme for assigning nodes to a certain range of users; learning about new configuration data; agreeing on data values; and so on.</p><blockquote><strong>How do you solve these problems?</strong></blockquote><p><em>Some centralized database ? However that would be a single point of failure, so a big NO !</em></p><blockquote><strong>So what’s the super cool decentralised way to bring order to large clusters ?</strong></blockquote><blockquote><em>Yes, your answer is right ! Gossip protocol.</em></blockquote><blockquote><em>Note: Different distributed systems have different ways to achieve this ( Kafka uses Zookeeper, ElasticSearch uses Mesh Topology Communication, etc).</em></blockquote><h3>the, What ?</h3><p>Gossip is a <em>peer-to-peer communication</em> protocol in which nodes periodically exchange state information about themselves and about other nodes they know about. The gossip process in Cassandra runs every second and exchanges state messages with other nodes in the cluster. Each node independently will always select one to three peers to gossip with. <em>It will always select a live peer (if any) in the cluster, it will probabilistically pick a seed node from the cluster or maybe it will probabilistically select an unavailable node</em>.The nodes exchange information about themselves and about the other nodes that they have gossiped about, so all nodes quickly learn about all other nodes in the cluster.</p><h3>the, How ?</h3><h4>Lets Break it Down !</h4><p>The Gossip messaging is very similar to the TCP <strong><em>three-way handshake</em></strong>. With a regular broadcast protocol, there could only have been one message per round, and the data can be allowed to <em>gradually</em> spread through the cluster. But with the gossip protocol, having three messages for each round adds a degree of <em>anti-entropy</em>. This process allows obtaining “convergence” of data shared between the two interacting nodes much faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/0*6XMTCt--PNA5eGcL" /></figure><h4>Implementation Overview</h4><p>I will split this into 2 parts :</p><ul><li><em>what exactly is exchanged as part of each ms</em>g : we will cover this in the next part of the blog series. To begin with, simply consider each of the above 3 messages is a “payload”, encapsulating certain information.</li><li>the entities (in code) required to facilitate the transfer of information :this is what is covered in-depth below.</li></ul><p>Each node has a central singleton entity Gossiper. The method of initiating gossip is in the Gossiper.gossipTask.run method, which is a runnable and executed once per second by a scheduled task in the upper layer.</p><blockquote><strong>Gossiper.endpointStateMap&lt;InetAddress, EndPointState&gt;</strong> holds the state of the entire cluster known to the current node. The key is the IP address of the corresponding node, and the value is EndPointState information which looks something like below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*LC3cNUmI4ZPE1ycNhyZjmQ.png" /></figure><p>Below gives the description of each. (<a href="https://cwiki.apache.org/confluence/display/CASSANDRA2/ArchitectureGossip">reference link</a>). The code for each of these is covered in the “Code” section of this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J7F3U2ArYZeFbI8YendLKA.png" /><figcaption>source: <a href="https://cwiki.apache.org/confluence/display/CASSANDRA2/ArchitectureGossip">https://cwiki.apache.org/confluence/display/CASSANDRA2/ArchitectureGossip</a></figcaption></figure><ul><li>A generic <strong>Message</strong> class to represent messages being exchanged : Lets discuss this one in more detail</li></ul><blockquote><strong>Need for a Message Class</strong></blockquote><blockquote><em>One might think that we can have a </em>Message<em> base class with </em>GossipDigestSynMessage, GossipDigestAckMessage &amp; GossipDigestAck2Message<em> as child classes. However such inheritance hierarchy will introduce tight coupling and any change will have a waterfall effect on all the related classes. To solve this, we will use </em><strong><em>composition</em></strong><em>.</em></blockquote><blockquote><em>A </em>Message<em> object will have a generic </em><strong>payload</strong><em> (which can be any one of </em>GossipDigestSyn, GossipDigestAck<em>, etc or any object in general we wish to transfer </em>~ the power of composition<em>).</em></blockquote><blockquote><em>Also it will have a </em><strong><em>Header</em></strong><em> ( similar to a HTTP header). Payload will contain the actual object and the header will have metadata such as </em><strong><em>created_at, from, to</em></strong><em> and so on.</em></blockquote><blockquote><strong><em>Verb and VerbHandler</em></strong></blockquote><blockquote><em>Another important thing to note is that gossip is in some way a form of </em>event driven programming<em>, i.e. actions are to be performed in response to certain events. This can be further extended beyond just gossip to other features of Cassandra. To handle this generically, Cassandra has “</em><strong><em>verbs</em></strong><em>” and their respective “</em><strong><em>handlers</em></strong><em>”. On receipt of a message, a node analyses the associated verb and triggers the associated handler, passing the entire message object as an argument. The handlers will then extract the payload and carry on their execution.</em></blockquote><p>Considering all the above points, listing the additional classes we need :</p><ul><li><strong><em>Verb</em></strong> (enum) : Each verb will be mapped to its handler, in the enum declaration itself.</li><li><strong><em>Header</em></strong> will have Verb, from, to, created_at fields</li><li><strong><em>Message&lt;T&gt; </em></strong>(class). Two attributes — Header and payload.</li><li>As already mentioned, there will a central singleton entity<strong><em> Gossiper, </em></strong>(to be covered in part-2 and part-3)</li></ul><h3>Code</h3><p>Coming to the most important part and the main topic of the post, lets see how the code of the above entities look like</p><blockquote><strong>HeartbeatState</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36216d56bead1ac9544510ca28f7bc2e/href">https://medium.com/media/36216d56bead1ac9544510ca28f7bc2e/href</a></iframe><blockquote><strong>AppState</strong></blockquote><p>ApplicationState is actually a Map&lt;ApplicationState, VersionedValue&gt;, which stores a variety of application information and the version corresponding to the information. For instance application state for “load information” could be (5.2, 45), which means that node load is 5.2 at version 45. ApplicationState is an enumeration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4291b30f10b53e061ca2c674ec49d7d8/href">https://medium.com/media/4291b30f10b53e061ca2c674ec49d7d8/href</a></iframe><blockquote><strong>EndPointState</strong></blockquote><p>Includes <strong><em>all ApplicationStates</em></strong> and HeartBeatState for a node. EndPointState can include only one of each type of ApplicationState, so if EndPointState already includes, say, load information, new load information will overwrite the old one. ApplicationState version number guarantees that old value will not overwrite new one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac4d2b3628e62745a393b6e016d7977b/href">https://medium.com/media/ac4d2b3628e62745a393b6e016d7977b/href</a></iframe><p>VersionedValue is a simple class with version (int) and value (string) attributes (code link).</p><blockquote><strong>Verb, Header and Message</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/935b517de39e3c8acdfc2bc03440988c/href">https://medium.com/media/935b517de39e3c8acdfc2bc03440988c/href</a></iframe><p>Few Points to note</p><ul><li><strong>Header</strong> has InetAddressPort which is nothing but the ip-address and the port for a node as both of them in combination uniquely identify a node. <a href="https://gist.github.com/Swarnim-singhal/8e2f69ac2ddf0f8f82cd7fe49836ab5b">Code link</a> for InetAddressPort implementation.</li><li><strong>Verb</strong> has Handlers for the three enums listed, implementations for these will be covered in the next part of this post.</li></ul><h4>Note to the reader</h4><blockquote>* The above implementations are very close but not exactly similar to Cassandra’s source code. Cassandra implements custom <em>serializers</em> for almost all the above ( and the additional constructs we are going to discuss later). Here, I have taken a simple approach and just implemented the <strong>Serializable</strong> interface. Serialization is required as messages have to be passed from one node to another. Also I will omit certain advanced concepts such as shadow round, etc in the implementation going forward.</blockquote><blockquote>* There are a few <strong>buzz</strong> keywords such as “volatile”, “AtomicReference”, etc which a reader might not be aware of. These are meant to ensure thread-safety in concurrent programs. (Talking in details about these is perhaps a topic for another post, as for the usages in code, I copy-pasted them from the Cassandra&#39;s source code itself ).</blockquote><p>The next part of the post will cover the in-depth details as well as implementation of the payloads and the respective handlers for each of them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9fd161e5f49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>