<?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 Chandan Singh on Medium]]></title>
        <description><![CDATA[Stories by Chandan Singh on Medium]]></description>
        <link>https://medium.com/@chandansingh_64286?source=rss-7b5aae4850f0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3-rgj1zITth9hw3z.</url>
            <title>Stories by Chandan Singh on Medium</title>
            <link>https://medium.com/@chandansingh_64286?source=rss-7b5aae4850f0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:22:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chandansingh_64286/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[Concurrency with LMAX Disruptor]]></title>
            <link>https://medium.com/@chandansingh_64286/concurrency-with-lmax-disruptor-40bcef990f15?source=rss-7b5aae4850f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/40bcef990f15</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[disruptors]]></category>
            <dc:creator><![CDATA[Chandan Singh]]></dc:creator>
            <pubDate>Tue, 05 Feb 2019 06:40:22 GMT</pubDate>
            <atom:updated>2019-02-05T06:40:22.719Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is a Disruptor?</h3><p>Disruptor is an open source Java library written by LMAX. It is a concurrent programming framework for the processing of a large number of transactions, with low-latency (and without the complexities of concurrent code). The performance optimization is achieved by a software design that exploits the efficiency of underlying hardware.</p><h3>Mechanical Sympathy</h3><p>Let’s start with the core concept of <a href="https://www.infoq.com/presentations/mechanical-sympathy">mechanical sympathy</a> — that is all about understanding how the underlying hardware operates and programming in a way that best works with that hardware.</p><p>For example, let’s see how CPU and memory organization can impact software performance. The CPU has several layers of cache between it and main memory. When the CPU is performing an operation, it first looks in L1 for the data, then L2, then L3, and finally, the main memory. The further it has to go, the longer the operation will take.</p><p>If the same operation is performed on a piece of data multiple times (for example, a loop counter), it makes sense to load that data into a place very close to the CPU.</p><h3>Why not Queues</h3><p>Queue implementations tend to have write contention on the head, tail, and size variables. Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched.</p><p>To deal with the write contention, a queue often uses locks, which can cause a context switch to the kernel. When this happens the processor involved is likely to lose the data in its caches.</p><p>To get the best caching behavior, the design should have only one core writing to any memory location (multiple readers are fine, as processors often use special high-speed links between their caches). Queues fail the one-writer principle.</p><p>If two separate threads are writing to two different values, each core invalidates the cache line of the other (data is transferred between main memory and cache in blocks of fixed size, called cache lines). That is a write-contention between the two threads even though they’re writing to two different variables. This is called false sharing, because every time the head is accessed, the tail gets accessed too, and vice versa.</p><h3>How the Disruptor Works</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/0*dUYOSfub22Kt63E1.jpg" /></figure><p>Disruptor has an array based circular data structure (ring buffer). It is an array that has a pointer to next available slot. It is filled with pre-allocated transfer objects. Producers and consumers perform writing and reading of data to the ring without locking or contention.</p><p>In a Disruptor, all events are published to all consumers (multicast), for parallel consumption through separate downstream queues. Due to parallel processing by consumers, it is necessary to coordinate dependencies between the consumers (dependency graph).</p><p>Producers and consumers have a sequence counter to indicate which slot in the buffer it is currently working on. Each producer/consumer can write its own sequence counter but can read other’s sequence counters. The producers and consumers read the counters to ensure the slot it wants to write in is available without any locks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40bcef990f15" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>