<?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 Deepak Mohnani on Medium]]></title>
        <description><![CDATA[Stories by Deepak Mohnani on Medium]]></description>
        <link>https://medium.com/@saymedgm?source=rss-95826a04d477------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Rft41UqvrnzgkpdC</url>
            <title>Stories by Deepak Mohnani on Medium</title>
            <link>https://medium.com/@saymedgm?source=rss-95826a04d477------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:17:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saymedgm/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[How Redis Does the Impossible: Handling Millions of Connections with a Single Thread]]></title>
            <link>https://medium.com/@saymedgm/how-redis-does-the-impossible-handling-millions-of-connections-with-a-single-thread-aeb43bd97ece?source=rss-95826a04d477------2</link>
            <guid isPermaLink="false">https://medium.com/p/aeb43bd97ece</guid>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Deepak Mohnani]]></dc:creator>
            <pubDate>Sat, 13 Jun 2026 09:50:05 GMT</pubDate>
            <atom:updated>2026-06-13T09:50:05.487Z</atom:updated>
            <content:encoded><![CDATA[<p>When software engineers talk about scaling applications to handle thousands of concurrent clients, the immediate and instinctive answer is almost always: <strong>“Use more threads.”</strong> Yet, some of the most performant, high-throughput technologies in our modern stack — like Redis and Node.js — defy this instinct. Redis famously operates on a <strong>single main thread</strong>, yet it easily handles over 100,000 requests per second.</p><p>How can a single-threaded architecture support massive concurrency without blocking? The answer doesn’t lie in complex application code, but rather in a powerful mechanism managed by the operating system kernel: <strong>I/O Multiplexing</strong> and the <strong>Event Loop</strong>.</p><p>In this article, we’ll dive deep into the internals of system-level I/O, expose the limitations of traditional multi-threading, and break down exactly how Redis leverages kernel system calls to achieve blistering speeds on a single thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/0*T4wA9I9Ee7TNS6tP" /></figure><h4><strong>The Traditional Approach : Thread per client</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kbbiWNwoeZp1k4bQ-JKXOw.png" /></figure><p>The most intuitive way to support concurrent clients is to spin up a new thread every time a client establishes a socket connection.</p><p>Since thread can be scheduled concurrently across multiple CPU cores, this approach ensures that if one client gets blocked waiting for data, the operating system can switch to another thread.</p><h4>Why thread per client method fails to scale ?</h4><p>While this works for few dozen connections it introduces several architectural pain points as your application scales :</p><ol><li><strong>Thread Safety and Synchronization overhead</strong> : If your thread needs to access shared data structures (like Redis Dictonary) simple operations like in memory increment are no longer atomic. You need to introduce explicit locking mechanism like mutexes and semaphores in order to protect your critical sections.</li><li><strong>The Lock Waiting paradox</strong> : Even though threads are there to run. concurrently extensively critically sections force threads to wait which destroys the concurrency benefits and slows down code execution</li><li><strong>Resource Exaustion</strong> : Threads are expensive and each of them occupies significant memory of stack space and having massive of them results in CPU overhead due to continuous context switching.</li></ol><h4>What is exactly a event loop ?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rkJtXzvKr2Cta-SClxEe_A.png" /></figure><p>To bypass multithreading we use AsynchTronus I/O. This is where event loop comes to picture.</p><p>Many of us confuse event loop withy a secondary background thread but that is not the case and we also cannot bound heavy CPU calculations it is designed to handle I/O operations.</p><p>So event loop is basically a is a single threaded infinite loop in user space that utilizes a low level system call interface provided directly by operating system kernel.</p><h4>Mechanics of Network I/O</h4><p>To understand how can a single thread monitor multiple connection lets see how data packet flows from physical wire to application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LsRPt8-FZt86TzIBzHMBfA.png" /></figure><p>The Arrival : The client sends data across socket connections and data arrives in NIC (Network interface card).</p><p>The Interrupt : The hardware card triggers a CPU interrupt. The kernel pauses low priority tasks in order to read incoming packet and place it to <strong>kernel buffer.</strong></p><p>The User-Space Sendoff : Under normal synchronous operation, the application process must be scheduled on the CPU to explicitly issue a read() system call which copies data from isolated kernel space to user application space memory.</p><p>Because of this specific buffer layer, the operating system kernel always knows exactly which network web sockets have data waiting to read long before the application process asks for it. This is how asynchronous I/O works.</p><h4>The Pillars of I/O Multiplexing : EPoll, Kqueue and IOCP</h4><p>Instead of the application blindly checking every single to seen which socket is ready it asks operating systems kernel to do the monitoring. <br>Depending on the operating system kernel to do the monitoring.</p><p>Depending on the operating system the kernel exposes different system calls for the same.</p><ol><li>Linux : epoll</li><li>macOS : KQueue</li><li>Windows : iocp</li></ol><p>In UNIX environments everything is treated as a file. Socket, disks and pipes are all represented by an integer known as FD (File descriptors)</p><h4>Deep Dive : How Redis used epoll</h4><p>To handle concurrent clients redis server interacts with epoll via three primary system calls.</p><ol><li><strong>epoll_create() </strong>: When redis boots up, it issues this call to initialize and <strong><em>epoll</em></strong> instance. Think of this as creating a specialist watch-list inside the kernel dedicated to our process.</li><li><strong>epoll_ctl()</strong> : Whenever a client connects to the redis server the server accepts the connection and gets new socket file descriptor and uses <strong><em>epoll_ctl </em></strong>to register that descriptor with the kernel watchlist. So basically redis basically asks kernel to Monitor the File descriptor and tell me when the file becomes readable.</li><li><strong>epoll_wait() </strong>: Whenever redis invokes this method this is a blocking call but a major twist in this is instead of blocking on a single-client connection, <strong>it blocks on the watch list simultaneously</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08FApJ9xLnETizKJmPS4qw.png" /></figure><pre>// A simplified model of redis event loop<br><br>while(true){<br>   // Block at least one registered FDs has data ready in kernel buffer <br>   int ready_fds = epoll_wait(epoll_fd, events, MAX_EVENT, -1);<br>   for(int i = 0; i &lt; ready_fds; i++){<br>      int file_descriptor = events[i].data.file_descriptor;<br>      if(file_descriptor == server_socket_file_descriptor){<br>        // Handling new file descriptor <br>        int client_file_descriptor = accept_new_connection(file_descriptor);<br>        epoll_ctl(epoll_file_descriptor, EPOLL_CTL_ADD, client_file_descriptor, &amp;event);<br>      }else{<br>        // read and execute redis commands <br>        process_client_command(file_descriptor);<br>      }<br>   }<br>}</pre><h4>How the above code is efficient ?</h4><p>If a server has 10000 idle clients connected, <strong>epoll_wait()</strong> does not consume CPU cycles scanning them. If only 3 clients send data, <strong>epoll_wait() </strong>instantly returns thsoe 3 active file descriptors. The single thread wakes up and handles 3 requests sequentially wihout any lock contention, and goes back to sleep.</p><p>With <strong>epoll_wait()</strong> the redis thread completely stops executing and consumes zero power while CPU is waiting it hands over the monitoring job to operating system kernel which handles it efficiently at hardware level using network interrupts.</p><p><strong>epoll</strong> uses and event driven architecture. When data arrives on network card the operating system directly puts that connection in to the ready list. When redis calls the epoll_wait() the kernel does not search through thousands of idle connections. it instantly hands redis the prebuilt list of active connections. This makes it a constant time operation O(1) time. Whether you have 10 or 10000 client connected waking up the worker takes same fraction of milliseconds.</p><h4><strong>Conclusion :</strong></h4><p>Redis proves that you do not always need massive multi-threaded synchronization machinery to build highly scalable systems. By dropping the baggage of thread-safety context switches and shifting the burden of I/O readiness monitoring onto the OS kernel via epoll, a single thread can seamlessly handle thousands of concurrent network connections</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aeb43bd97ece" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>