<?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:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[System Design Newsletter]]></title><description><![CDATA[I simplify complex system design concepts, delivering one well-researched article weekly to help you become a better software engineer.]]></description><link>https://read.systemdesignblueprint.com</link><image><url>https://substackcdn.com/image/fetch/$s_!oBIx!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4642e5a-0027-404b-ae07-0ca0e4960741_432x432.png</url><title>System Design Newsletter</title><link>https://read.systemdesignblueprint.com</link></image><generator>Substack</generator><lastBuildDate>Mon, 18 May 2026 04:17:05 GMT</lastBuildDate><atom:link href="https://read.systemdesignblueprint.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Aniket Singh]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[systemdesignnewsletter@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[systemdesignnewsletter@substack.com]]></itunes:email><itunes:name><![CDATA[Aniket Singh]]></itunes:name></itunes:owner><itunes:author><![CDATA[Aniket Singh]]></itunes:author><googleplay:owner><![CDATA[systemdesignnewsletter@substack.com]]></googleplay:owner><googleplay:email><![CDATA[systemdesignnewsletter@substack.com]]></googleplay:email><googleplay:author><![CDATA[Aniket Singh]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Scalable Systems Design: From zero to a Million Users]]></title><description><![CDATA[An engineer's guide to building scalable systems. Learn key techniques for handling millions of users with efficiency]]></description><link>https://read.systemdesignblueprint.com/p/build-systems-to-handle-millions-of-users</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/build-systems-to-handle-millions-of-users</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Thu, 14 Nov 2024 04:45:49 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/5613d8dc-a495-49f2-81a4-3a4b85013344_602x756.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Building systems that seamlessly scale from small, <strong>single-server setups</strong> to handling <strong>millions of users</strong> is an essential skill for senior engineers. In this guide, we&#8217;ll discuss the key concepts and architectural patterns that enable systems to scale efficiently, maintain high availability, and ensure robust performance at scale.</p><p>In this guide, we&#8217;ll walk through a <strong>framework for scaling systems</strong> from <strong>0 to 1M users</strong>. As we dive into the details, we&#8217;ll reference several key topics that I&#8217;ve previously covered in depth. Feel free to check these out:</p><ol><li><p>A crash course on Caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong>, <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-9-load-balancers-101">What are Load Balancers</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">What is Redis</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/introduction-to-kafka">What is Kafka</a>?</strong></p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Before we dive in, please <strong>subscribe</strong> to receive more such system design deep dives every week.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1><strong>Starting with a Single Server Setup</strong></h1><p>In the early stages of a web application, a single server setup often provides all the necessary resources. In this configuration, a single physical or virtual server typically hosts the <strong>web server, database, and cache</strong>. The simplicity of this architecture allows for quick deployment, minimal configuration, and low costs, making it ideal for <strong>small projects or MVPs (Minimum Viable Products)</strong>.</p><p>However, as user demand grows, this design quickly encounters limitations. A single server, no matter how powerful, has finite resources in terms of CPU, memory, storage, and network bandwidth. Running all application components (such as web server, database, and cache) on one server can lead to <strong>resource contention</strong>, where processes compete for CPU time and memory, leading to slower response times and degraded performance.</p><p>While starting with a single server is simple, <strong>transitioning to a distributed architecture</strong> becomes essential as traffic increases. This evolution ensures that no single component becomes a bottleneck, allowing each part of the application to scale as needed.</p><h2><strong>Request Flow: Understanding User Requests and Responses</strong></h2><p>In a typical web application request flow, understanding each step from user request to server response is crucial. Here&#8217;s a breakdown of this flow:</p><ol><li><p><strong>DNS Resolution</strong>: The first step is the <strong>Domain Name System (DNS)</strong> lookup. When a user enters a URL, DNS translates the human-readable domain name into an <strong>IP address</strong> that the browser can use to connect to the server.</p></li><li><p><strong>Web Server Processing</strong>: Once the request reaches the server, the <strong>web server</strong> (e.g., Apache, Nginx) processes the incoming request. This server can route requests to the appropriate part of the application, whether it&#8217;s static HTML content or dynamic data processing.</p></li><li><p><strong>Application Logic and Database Query</strong>: For dynamic requests, the application server may need to retrieve data from the database. This step could involve <strong>complex queries or multiple database operations</strong> to gather the necessary data.</p></li><li><p><strong>Response Generation</strong>: The application logic processes the data, formats it (often as HTML, JSON, or XML), and sends it back to the client.</p></li><li><p><strong>Rendering the Response</strong>: Finally, the client (usually a browser) renders the response for the user, displaying the content or updating the page based on the application&#8217;s logic.</p></li></ol><p>In this setup, each step is crucial for the application&#8217;s <strong>responsiveness and reliability</strong>. A delay or failure at any point affects the overall user experience.</p><h2>Challenges with single server setup: Single Point of Failure and Scaling Issues</h2><p>One of the most significant challenges of a single-server architecture is that it represents a <strong>single point of failure</strong>. If the server goes down, whether due to hardware failure, software issues, or unexpected traffic surges, the entire application becomes inaccessible. Downtime is a major concern for businesses, as it can result in <strong>lost revenue, reduced user trust, and potential reputational damage</strong>.</p><p>Moreover, a single server limits scalability. Adding more traffic means increasing CPU, memory, and storage demands on this single machine, which can only be scaled up to a certain point. Eventually, performance will degrade, leading to slower response times, increased latency, and a poor user experience.</p><div><hr></div><h1><strong>Database Separation and Scaling</strong></h1><p>As traffic and data requirements increase, the <strong>database</strong> becomes one of the first components to be decoupled from the web server. By moving the database to a separate server or cluster, we can scale the <strong>application layer</strong> and <strong>data layer</strong> independently.</p><ul><li><p><strong>Improved Performance</strong>: Separating the database reduces the workload on the web server, which no longer has to manage storage-heavy tasks. Instead, the web server can focus solely on handling requests, while the database server handles data queries and transactions.</p></li><li><p><strong>Enhanced Scalability</strong>: With a dedicated database server, scaling the application becomes easier. The application layer can be scaled horizontally (by adding more web servers) while keeping the database layer stable.</p></li><li><p><strong>Increased Reliability and Security</strong>: By isolating the database, we can implement separate <strong>backup strategies</strong> and <strong>security measures</strong>, reducing the risk of data loss and improving data access controls.</p></li></ul><p>Decoupling is an important step for any application with growing data needs, as it allows each layer to grow at its own pace, making the architecture more modular and easier to manage.</p><h3>Relational Databases (RDBMS) vs. Non-Relational (NoSQL) Databases</h3><p>Choosing the right database type is essential for scaling the application effectively. Here&#8217;s a comparison of <strong>RDBMS</strong> and <strong>NoSQL databases</strong>:</p><ul><li><p><strong>Relational Databases (RDBMS)</strong>: These databases, such as <strong>MySQL</strong> and <strong>PostgreSQL</strong>, store data in structured tables with predefined schemas. They are ideal for applications where data integrity, relationships, and <strong>ACID (Atomicity, Consistency, Isolation, Durability)</strong> compliance are important. RDBMS is highly suitable for applications that rely on <strong>complex relationships</strong>, such as e-commerce platforms with inventory, customer, and order relationships.</p></li><li><p><strong>Non-Relational Databases (NoSQL)</strong>: NoSQL databases, such as <strong>MongoDB</strong> and <strong>DynamoDB</strong>, are designed for handling large volumes of unstructured or semi-structured data. They often prioritize availability and scalability over consistency, which makes them suitable for applications with high read/write requirements, low-latency needs, or applications with <strong>dynamic data structures</strong> (e.g., social media platforms). NoSQL databases are usually schema-less, allowing for flexibility as data requirements evolve.</p></li></ul><p>In practice, many applications employ a <strong>polyglot persistence strategy</strong>, using a combination of RDBMS and NoSQL databases to leverage the strengths of each type.</p><div><hr></div><h1><strong>Scaling Approaches</strong></h1><p>To handle increased demand, there are two primary approaches to scaling infrastructure: <strong>Vertical Scaling</strong> and <strong>Horizontal Scaling</strong>.</p><h3>Vertical Scaling: Adding More Power to a Single Server</h3><p>Vertical scaling, also known as <strong>scaling up</strong>, involves increasing the resources (CPU, RAM, or storage) of an existing server. For example, upgrading a server from 16 GB to 32 GB of RAM or from 4 CPU cores to 8. This approach is straightforward and doesn&#8217;t require changes to the application architecture.</p><ul><li><p><strong>Pros</strong>: Simplicity and low configuration changes. Vertical scaling is quick and can often be done with minimal disruption.</p></li><li><p><strong>Cons</strong>: Limited scalability. Physical hardware has constraints, and each upgrade becomes increasingly costly. There&#8217;s a point at which further vertical scaling becomes impractical or even impossible.</p></li></ul><p>For small applications, vertical scaling may suffice. However, it becomes unsustainable as traffic and storage demands grow, especially for applications with unpredictable workloads.</p><h3>Horizontal Scaling: Distributing Load Across Multiple Servers</h3><p>Horizontal scaling, or <strong>scaling out</strong>, involves adding more servers to distribute the load across multiple instances. This approach often involves <strong>load balancing</strong>, where requests are spread across multiple servers to avoid overloading any single machine. Horizontal scaling is more complex than vertical scaling but offers nearly unlimited scalability if implemented properly.</p><ul><li><p><strong>Pros</strong>: Greater flexibility and reliability. By distributing load, horizontal scaling can prevent bottlenecks, reduce downtime, and support rapid increases in traffic.</p></li><li><p><strong>Cons</strong>: Complexity in setup and maintenance. Horizontal scaling often requires architectural changes, such as implementing distributed databases, caching layers, and synchronization across servers.</p></li></ul><div><hr></div><h1><strong>Load Balancing for High Availability and Performance</strong></h1><p>A <strong>load balancer</strong> distributes incoming traffic across multiple servers, ensuring no single server handles too much load. This setup improves performance and maintains uptime by allowing the system to continue functioning even if one server fails.</p><ul><li><p><strong>Failover</strong>: If a server goes down, the load balancer automatically routes traffic to other healthy servers, maintaining uptime.</p></li><li><p><strong>Scalability</strong>: With a load balancer, you can add more servers as traffic grows, smoothly scaling your application without disrupting users.</p></li><li><p><strong>Security</strong>: By positioning servers behind the load balancer, you limit direct access to servers, adding an extra layer of security.</p></li></ul><p>Load balancing allows applications to handle large traffic volumes while ensuring that each server performs optimally.</p><div><hr></div><h1><strong>Optimizing Response Time with Caching</strong></h1><p><strong>Caching</strong> reduces load on the database by storing frequently accessed data in memory. This speeds up response times significantly, especially in applications with a lot of read requests.</p><ul><li><p><strong>Cache Layers</strong>: Use caching solutions like <strong>Redis</strong> or <strong>Memcached</strong> as an additional layer to store commonly requested data.</p></li><li><p><strong>Read-Through Cache</strong>: The application checks the cache for data before querying the database. If the data isn&#8217;t in the cache, it&#8217;s fetched from the database, stored in the cache, and returned to the user. This minimizes database calls.</p></li><li><p><strong>Cache Expiration</strong>: Use time-to-live (TTL) values to automatically remove data from the cache after a set time, helping balance memory usage and data freshness.</p></li><li><p><strong>Eviction Policies</strong>: When cache memory fills up, eviction policies like <strong>LRU (Least Recently Used)</strong> or <strong>LFU (Least Frequently Used)</strong> manage which items to remove, optimizing memory for frequently accessed data.</p></li></ul><p>Caching can dramatically improve response times and reduce load, especially in high-traffic, read-heavy applications.<strong>Content Delivery Network (CDN) for Global Performance</strong></p><p>A CDN helps reduce latency for delivering static content by caching files on edge servers close to users. This offloads traffic from your main servers and speeds up content delivery.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Content Delivery Network (CDN) for Global Performance</h1><p>A <strong>Content Delivery Network (CDN)</strong> caches static files (like images, stylesheets, and scripts) on servers around the world, reducing load on your main servers and improving response times for users in different regions.</p><ul><li><p><strong>How CDNs Work</strong>: When a user requests content, the request is routed to the nearest CDN server. If the file is already cached, it&#8217;s served immediately. If not, the CDN fetches it from the origin server, caches it, and serves it to the user.</p></li><li><p><strong>Advantages of CDNs</strong>:</p><ul><li><p><strong>Reduced Latency</strong>: By serving files from a server close to the user, CDNs reduce the time it takes to load static content.</p></li><li><p><strong>Cost-Effective Scaling</strong>: Offloading traffic to CDN servers reduces demand on your primary servers, lowering infrastructure costs.</p></li></ul></li></ul><p>CDNs are essential for applications with users in multiple regions, providing faster load times and reducing the load on backend servers.</p><div><hr></div><h1>Stateless Web Tier for Horizontal Scaling</h1><p>In a <strong>stateless design</strong>, each request is handled independently, meaning that no server relies on previous requests to process the current one. This setup allows requests to be routed to any available server, simplifying load distribution and disaster recovery.</p><ul><li><p><strong>Session Management</strong>: In a stateless architecture, session data is not stored on individual servers but in a centralized data store like <strong>Redis</strong> or an <strong>RDBMS</strong>. This approach allows users to be routed to any server without losing session data, supporting seamless scaling and failover. Centralized session storage also ensures that user data remains consistent across requests.</p></li><li><p><strong>Autoscaling</strong>: With stateless design, you can automatically scale up or down based on traffic without disrupting user sessions. As demand increases, additional web servers can be added, distributing requests evenly and supporting higher throughput.</p></li></ul><div><hr></div><h1>Database Replication for Fault Tolerance</h1><p><strong>Database replication</strong> creates one or more copies of the database on different servers, which provides redundancy and high availability. This approach is especially valuable in production environments where database downtime can affect user experience.</p><ul><li><p><strong>Master-Slave Replication</strong>: In this setup, the <strong>master</strong> server is responsible for handling all write operations, while <strong>slave</strong> servers are designated for read operations. This division of roles helps increase read throughput by allowing reads to be distributed across multiple slave servers. If the master server fails, the system retains the latest read-only data on the slaves, minimizing potential data loss.</p></li><li><p><strong>Automatic Failover</strong>: If the master node fails, a slave node can be promoted to master to take over write operations. This process typically happens quickly and with minimal downtime, helping to keep the system operational even during outages.</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Sharding for Large-Scale Database Distribution</h1><p><strong>Sharding</strong> divides a large database into smaller parts, called shards, that are spread across multiple servers. This division distributes the load and improves overall database performance, making it an effective strategy for applications with rapidly growing data.</p><ul><li><p><strong>Sharding Key</strong>: Selecting an effective sharding key is crucial. This key, such as a user ID in a social media app, determines how data is distributed across shards. An even distribution reduces the risk of any single shard becoming a bottleneck, helping maintain consistent performance as data grows.</p></li><li><p><strong>Challenges</strong>:</p><ul><li><p><strong>Hotspots</strong>: Certain entities, like high-profile users, may generate excessive traffic and overload their shard. A common solution is to use dedicated shards for popular entities or to split traffic using a balanced sharding strategy.</p></li><li><p><strong>Re-sharding</strong>: As the data volume increases, you may need to modify the sharding strategy to distribute data more effectively. Techniques like <strong>consistent hashing</strong> can help re-balance the data across shards without extensive downtime, though this process requires careful planning.</p></li></ul></li></ul><div><hr></div><h1>Multi-Data Center Architecture for Global Availability</h1><p>Deploying applications across multiple data centers improves performance, increases resilience, and allows applications to serve users worldwide with reduced latency. This setup supports both disaster recovery and user experience.</p><ul><li><p><strong>GeoDNS</strong>: By using GeoDNS, you can route users to the closest data center, which reduces latency and speeds up response times. For example, users in Europe can be directed to a European data center, while users in Asia are directed to an Asian data center. This geographic routing also improves regional performance.</p></li><li><p><strong>Data Synchronization</strong>: Data is synchronized across all data centers, ensuring that users get a consistent experience no matter where they are located. Replicating data in this way allows each data center to serve as a backup for others, enhancing disaster recovery.</p></li></ul><div><hr></div><h1>Asynchronous Communication with Message Queues</h1><p><strong>Message queues</strong> facilitate asynchronous communication between services, allowing them to operate independently. This decoupling enhances scalability and resilience by enabling each service to function and scale at its own pace.</p><ul><li><p><strong>Decoupling Services</strong>: With a message queue, one service can produce messages, and another can consume them without having to be available at the same time. This setup improves reliability, as services can recover from downtime and pick up where they left off without losing data.</p></li><li><p><strong>Use Case Example</strong>: For example, in a media platform, when a user uploads a video, the request can trigger background processes for encoding and thumbnail generation. These processes run independently, using a message queue to coordinate tasks without slowing down the user&#8217;s upload experience.</p></li></ul><div><hr></div><h1>Monitoring, Logging, and Automation for Operational Excellence</h1><p>As systems grow, effective <strong>monitoring</strong>, <strong>logging</strong>, and <strong>automation</strong> are crucial to maintaining reliability, performance, and security.</p><ul><li><p><strong>Metrics Collection</strong>: Monitoring involves collecting key metrics, such as CPU and memory usage (host-level metrics), database and cache performance (application-level metrics), and user engagement (business metrics). By tracking these metrics, teams can identify and address performance bottlenecks before they affect users.</p></li><li><p><strong>Centralized Logging</strong>: Centralized logging aggregates logs from all services in a single location. This setup simplifies troubleshooting and enables teams to spot patterns, detect anomalies, and identify potential security risks.</p></li><li><p><strong>CI/CD Pipelines</strong>: Continuous integration and continuous deployment (CI/CD) pipelines automate the testing and deployment process, ensuring that updates are rolled out consistently and safely. This automation reduces human error and speeds up the release of new features and bug fixes.</p></li></ul><div><hr></div><h1>Conclusion: Scaling to Millions of Users</h1><p>Scaling from a single-server setup to a multi-million-user system requires a methodical approach, incorporating strategies like <strong>database sharding, caching, stateless architectures, </strong>and<strong> load balancing</strong>. By focusing on modular components and adopting distributed data management practices, engineers can ensure that systems are resilient, high-performing, and able to meet the demands of large-scale, geographically distributed users.</p><p>Mastering these principles is essential for any engineer building and maintaining scalable systems in today&#8217;s fast-evolving tech landscape.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Liked this article? Make sure to &#128153; click the like button.</p><p>Feedback or addition? Make sure to &#128172; comment.</p><p>Know someone who would find this helpful? Make sure to &#128257; share this post.</p>]]></content:encoded></item><item><title><![CDATA[How to use Kafka in system design interviews?]]></title><description><![CDATA[Master Kafka for system design interviews. Learn key concepts like scalability, fault tolerance, and messaging strategies to improve interview success]]></description><link>https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 06 Nov 2024 04:45:58 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the last post, we went over the <strong><a href="https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide">basics of Kafka</a></strong>. However, in the <strong><a href="https://read.systemdesignblueprint.com/i/148928014/step-address-key-issues-in-the-design">design deep dive</a></strong> phase of system design interviews, especially for <strong>senior engineering roles (&gt;</strong>4 yrs exp), interviewers often expect a deeper understanding of Kafka. Even for <strong>junior roles</strong>, knowing some of these concepts can give you an edge during the interview. In this post, we&#8217;ll explore <strong>key Kafka concepts</strong> you should know to tackle deep-dive questions in interviews and make the best use of Kafka in your designs.</p><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">How to crack any system design interview</a>?</strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">How to use Redis in system design interviews</a>?</strong></p></li><li><p>A crash course on Caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong>, <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-1-how-to-design-a-payment-system">How to design a payments system</a>?</strong></p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Kafka deep dive concepts</h1><h2>How to achieve Scalability in Kafka?</h2><p>In a Kafka environment, achieving scale relies on optimizing message handling, resource distribution, and throughput. On strong hardware, <strong>a single broker</strong> can manage around <strong>1TB of data</strong> and process roughly <strong>10,000 messages per second</strong> (this depends on message size and hardware specs, but serves as a rough guideline). If your requirements are within these limits, <strong>scaling may not be necessary</strong>. Here&#8217;s a breakdown of key strategies for scaling Kafka effectively.</p><h3><strong>Add more brokers for horizontal scaling</strong></h3><p>Adding brokers to the Kafka cluster is the most straightforward way to scale. This spreads the load and improves fault tolerance. However, to fully benefit from additional brokers, ensure that your topics have enough partitions to allow for parallelism. Without adequate partitions, new brokers won&#8217;t effectively balance the load.</p><h3><strong>Keep the message size small</strong></h3><p>Kafka performs best with smaller messages, which enable faster processing and save storage space. This can be achieved this by <strong>batching messages</strong> and configuring <strong>compression</strong> on producers using settings like <code>compression.type</code> and <code>batch.size</code>. Sample configs:</p><p><code>      message.max.bytes=1048576   # Maximum message size                                           should be 1MB<br>      batch.size=16384  # Maximum size of a batch in bytes<br>      linger.ms=1       # Time to wait before sending the batch<br>      compression.type=snappy  # Compression codec</code></p><h3><strong>Add more partitions to high-traffic topics in Kafka</strong></h3><p>For high-traffic topics, <strong>increase partition count</strong> and <strong>replication factor</strong> for better load distribution and fault tolerance. Here are ways to manage hot partitions effectively:</p><ol><li><p><strong>Random Partitioning (No Key)</strong>: Without a key, Kafka randomly assigns partitions, ensuring even distribution but no message ordering. Suitable if order doesn&#8217;t matter.</p></li><li><p><strong>Salting Keys</strong>: Add a random number or timestamp to keys (e.g., ad IDs) to spread the load evenly across partitions, though this may complicate later aggregation.</p></li><li><p><strong>Compound Keys</strong>: Combine the primary key (like ad ID) with another attribute, like region or user segment, to balance traffic across partitions.</p></li><li><p><strong>Back Pressure</strong>: Slow down the producer if partition lag is high. Managed Kafka services may offer this by default, or you can manually check and adjust the producer rate.</p></li></ol><h2>How to achieve Fault Tolerance and Reliability with Kafka</h2><h3><strong>Consumer rebalancing in Kafka</strong></h3><p>When consumers fail or new ones join a group, Kafka redistributes partitions to keep messages flowing. This rebalancing keeps partitions actively consumed but can add latency if it happens too frequently. You can control rebalancing speed with <code>session.timeout.ms</code> and <code>max.poll.interval.ms</code> settings to reduce unnecessary delays.</p><h3><strong>Replication strategies</strong></h3><p>Kafka's replication factor sets the number of copies for each message across brokers, adding fault tolerance and data protection. A higher replication factor minimizes data loss risk during broker failures but requires more storage and bandwidth. Use <code>KAFKA_REPLICATION_FACTOR</code> when creating topics to match your system&#8217;s availability needs.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please subscribe to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h3><strong>Processing guarantees in Kafka</strong></h3><p>Each partition acts as an <strong>append-only log</strong>, with unique offsets for every message. Consumers commit these offsets after processing, allowing them to pick up exactly where they left off after restarts, ensuring no missed or duplicated messages. <em><strong>Figure 1</strong></em> shows how Kafka consumers manage offsets</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IZni!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IZni!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 424w, https://substackcdn.com/image/fetch/$s_!IZni!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 848w, https://substackcdn.com/image/fetch/$s_!IZni!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!IZni!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IZni!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png" width="388" height="412.0729927007299" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1164,&quot;width&quot;:1096,&quot;resizeWidth&quot;:388,&quot;bytes&quot;:330571,&quot;alt&quot;:&quot;Offset management in Kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Offset management in Kafka" title="Offset management in Kafka" srcset="https://substackcdn.com/image/fetch/$s_!IZni!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 424w, https://substackcdn.com/image/fetch/$s_!IZni!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 848w, https://substackcdn.com/image/fetch/$s_!IZni!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!IZni!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F007f5fb0-21fe-4953-8d4f-85aeca00e0e9_1096x1164.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Offset management in Kafka</strong></figcaption></figure></div><p>Kafka offers <strong>three offset commit</strong> methods, each providing distinct processing guarantees:</p><h4><strong>At-most once processing</strong></h4><p>In this mode, each message may be processed only once or skipped if there&#8217;s a failure, as offsets are <strong>automatically committed</strong> after polling. With <code>enable.auto.commit</code> set to <code>true</code>, any processing failure means Kafka won&#8217;t reprocess the message, which can lead to data loss. This option works best for <strong>non-critical data</strong>. <em><strong>Figure 2</strong></em> shows the relevant code snippet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!insQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!insQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 424w, https://substackcdn.com/image/fetch/$s_!insQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 848w, https://substackcdn.com/image/fetch/$s_!insQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 1272w, https://substackcdn.com/image/fetch/$s_!insQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!insQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png" width="534" height="327.4611570247934" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:742,&quot;width&quot;:1210,&quot;resizeWidth&quot;:534,&quot;bytes&quot;:200136,&quot;alt&quot;:&quot;At-most once processing in Kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="At-most once processing in Kafka" title="At-most once processing in Kafka" srcset="https://substackcdn.com/image/fetch/$s_!insQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 424w, https://substackcdn.com/image/fetch/$s_!insQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 848w, https://substackcdn.com/image/fetch/$s_!insQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 1272w, https://substackcdn.com/image/fetch/$s_!insQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd67b8be4-99a1-4dfb-af2a-a836dd52bac5_1210x742.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Nodejs code for at-most once processing in Kafka</strong></figcaption></figure></div><h4>At-least once processing</h4><p>This mode ensures each message is processed at least once but may result in duplicates if retries occur after a failure. Here, <code>enable.auto.commit</code> is set to <code>false</code>, and offsets are committed only after processing completes successfully. It&#8217;s ideal when <strong>data loss is unacceptable</strong>, such as in logging applications, even if it means handling duplicate messages. <em><strong>Figure 3</strong></em> shows the relevant code snippet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Cfdr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Cfdr!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 424w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 848w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 1272w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Cfdr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png" width="532" height="312.31079136690647" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:816,&quot;width&quot;:1390,&quot;resizeWidth&quot;:532,&quot;bytes&quot;:235917,&quot;alt&quot;:&quot;At-least once processing in Kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="At-least once processing in Kafka" title="At-least once processing in Kafka" srcset="https://substackcdn.com/image/fetch/$s_!Cfdr!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 424w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 848w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 1272w, https://substackcdn.com/image/fetch/$s_!Cfdr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a6e2220-0821-40f5-af3a-8490851cfc75_1390x816.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Nodejs code for at-least once processing in Kafka</strong></figcaption></figure></div><h4>Exactly once processing</h4><p>EOS guarantees each message is processed exactly once using <strong>transactions</strong> and <strong>idempotent producers</strong> (<code>enable.idempotence=true</code>). Consumer offsets are committed only after a successful transaction, preventing both duplicates and data loss. This level of reliability is essential for <strong>sensitive applications</strong> like financial transactions, where precision is critical. <em><strong>Figure 4</strong></em> shows the relevant code snippet.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Fzo8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Fzo8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 424w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 848w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 1272w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Fzo8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png" width="728" height="436.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:873,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:376630,&quot;alt&quot;:&quot;Exactly once processing&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Exactly once processing" title="Exactly once processing" srcset="https://substackcdn.com/image/fetch/$s_!Fzo8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 424w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 848w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 1272w, https://substackcdn.com/image/fetch/$s_!Fzo8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66e89b64-d462-4b2d-9f68-a038bc3eda5f_2102x1260.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Nodejs code for exactly once processing</strong></figcaption></figure></div><h2>Handling Errors and Retries in Kafka</h2><p>Kafka retries help prevent message loss during temporary issues like network disruptions. Retries can be set up for both producers and consumers.</p><h3><strong>Producer retry strategies</strong></h3><p>Producer retries are configured using the <code>retries</code> (max attempts) and <code>retry.backoff.ms</code> (interval between retries) settings. When a producer faces a temporary issue, such as network instability or a broker being briefly unavailable, these settings allow Kafka to retry sending the message instead of discarding it. See <em><strong>Figure 5</strong></em> for code example</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IIZQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IIZQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 424w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 848w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 1272w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IIZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png" width="578" height="418.4838160136286" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:850,&quot;width&quot;:1174,&quot;resizeWidth&quot;:578,&quot;bytes&quot;:169936,&quot;alt&quot;:&quot;Retries in Kafka Producer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Retries in Kafka Producer" title="Retries in Kafka Producer" srcset="https://substackcdn.com/image/fetch/$s_!IIZQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 424w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 848w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 1272w, https://substackcdn.com/image/fetch/$s_!IIZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc2b2b082-598a-4ec5-ba4f-35dbe06f3c92_1174x850.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Retries in kafka producer</strong></figcaption></figure></div><h3>Consumer Retry strategies</h3><p>Consumers will attempt to process messages a set number of times before moving them to a <strong>Dead Letter Queue (DLQ)</strong>. <em><strong>Figure 6</strong></em> provides a detailed breakdown of how the retry process operates on the consumer side, including the transition to DLQ.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9zxt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9zxt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 424w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 848w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 1272w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9zxt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png" width="570" height="454.1208791208791" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1456,&quot;resizeWidth&quot;:570,&quot;bytes&quot;:406551,&quot;alt&quot;:&quot;Kafka consumer retries&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka consumer retries" title="Kafka consumer retries" srcset="https://substackcdn.com/image/fetch/$s_!9zxt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 424w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 848w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 1272w, https://substackcdn.com/image/fetch/$s_!9zxt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed7eee8e-c9ca-42fe-996f-95d1bb7955ba_2822x2248.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Kafka consumer retries</strong></figcaption></figure></div><p>Here is a step-by-step breakdown:</p><ul><li><p><strong>Main consumer</strong></p><ul><li><p>A producer sends a message to the Main Topic, and if the consumer fails to process it, the error context is captured and sent to a <strong>dedicated Retry Topic</strong>, isolating them from the main processing flow.</p></li><li><p>The original message ID and timestamps help track failures and aid debugging, while error details and service version information help identify recurring issues.</p></li></ul></li><li><p><strong>Persistence in MySQL</strong></p><ul><li><p>Failed messages are logged in MySQL with the <strong>current retry count</strong> and <strong>timestamps</strong>, helping to analyze failure patterns.</p></li><li><p>Error messages and service versions provide context for failures, supporting effective troubleshooting.</p></li></ul></li><li><p><strong>Scheduled Retries via Airflow</strong></p><ul><li><p><strong>Airflow</strong> regularly picks up failed messages to ensure all retries are processed promptly.</p></li><li><p><strong>Exponential backoff in retries</strong> helps avoid system overload during high failure rates.</p></li></ul></li><li><p><strong>Dead Letter Queue</strong></p><ul><li><p>If the message still fails processing even after N retry attempts from the retry topic, it is sent to a <strong>Dead Letter Queue (DLQ)</strong>. </p></li><li><p>DLQ is a <strong>separate Kafka topic</strong> that stores failed messages indefinitely, allowing developers to analyze them later, determine the failure cause, and fix them. The DLQ ensures no message is lost, providing a fallback for unresolvable errors.</p></li></ul></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please subscribe to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>Performance optimizations in Kafka</h2><h3>Use Schema Registry for data consistency</h3><p>In messaging or event streaming systems, data is often <strong>serialized</strong> using a <strong>schema format</strong> before being published to topics or queues. However, as data producers and consumers evolve, they might have different expectations of data formats, leading to issues such as:</p><ul><li><p><strong>Schema Mismatch</strong>: Producers may publish data in formats that consumers cannot read if they are using outdated or incompatible schemas.</p></li><li><p><strong>Maintenance Complexity</strong>: Manually updating schemas across multiple applications can be error-prone and challenging to coordinate.</p></li></ul><p>A <strong>schema registry</strong> is a centralized service that manages schemas for systems such as Kafka. It ensures that both producers and consumers use compatible schemas.</p><p><strong>Benefits of using a Schema Registry:</strong></p><ol><li><p><strong>Decoupling Schemas</strong>: Producers and consumers can work independently, allowing easier upgrades and maintenance.</p></li><li><p><strong>Central Schema Tracking</strong>: All schemas used in production are stored in a central location.</p></li><li><p><strong>Versioning Benefits:</strong> Facilitates schema changes by managing different versions, ensuring <strong>backward compatibility,</strong> and minimizing errors during updates.<br></p></li></ol><h4>What is Apicurio in Kafka?</h4><p><strong><a href="https://www.apicur.io/registry/">Apicurio Schema Registry</a></strong> is a runtime server that stores and manages schemas for data serialization. Key advantages of Apicurio:</p><ul><li><p><strong>Open Source</strong>: Supported by an active open-source community.</p></li><li><p><strong>Multi-Format Support</strong>: Supports Avro, JSON Schema, and Protobuf.</p></li><li><p><strong>Schema Evolution and Versioning</strong>: Provides schema versioning, supporting backward and forward compatibility.</p></li><li><p><strong>Schema Validation</strong>: Validates schemas against predefined rules to prevent deployment of invalid schemas.</p></li></ul><h4>How does Apicurio enhance Kafka's performance?</h4><p><em><strong>Figure 7</strong></em> shows the steps involved when using a schema registry:</p><ol><li><p>The producer <strong>registers the schema</strong> in the Apicurio Schema Registry before producing messages.</p></li><li><p>Before sending the message to Kafka, the producer <strong>retrieves the schema</strong> from the registry to serialize data.</p></li><li><p>The producer <strong>sends</strong> the <strong>serialized message to Kafka</strong> with a reference to the schema.</p></li><li><p>The consumer <strong>retrieves the schema</strong> from the registry to understand the data format.</p></li><li><p>The consumer uses the schema to <strong>deserialize the message</strong> and consume it.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!x2yY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!x2yY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 424w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 848w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 1272w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!x2yY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png" width="728" height="501.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1003,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:558934,&quot;alt&quot;:&quot;Steps in schema registry like apicurio&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Steps in schema registry like apicurio" title="Steps in schema registry like apicurio" srcset="https://substackcdn.com/image/fetch/$s_!x2yY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 424w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 848w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 1272w, https://substackcdn.com/image/fetch/$s_!x2yY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0cff6ea-03b8-438f-b841-0c1ba24836b9_3798x2617.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: How does schema registry work?</strong></figcaption></figure></div><h3>Consumer Lag in Kafka</h3><p>Kafka consumer lag refers to the <strong>difference between</strong> the <strong>last produced message offset</strong> and the <strong>last consumed message offset</strong> for a particular consumer group (<em><strong>Figure 8</strong></em>). It indicates how far behind a consumer is in processing messages from a Kafka topic. Monitoring consumer lag is crucial because high lag can lead to delays in message processing, impacting system performance and user experience.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ku7b!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ku7b!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 424w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 848w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 1272w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ku7b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png" width="618" height="461.37774725274727" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1087,&quot;width&quot;:1456,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:298249,&quot;alt&quot;:&quot;Consumer lag in kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Consumer lag in kafka" title="Consumer lag in kafka" srcset="https://substackcdn.com/image/fetch/$s_!ku7b!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 424w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 848w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 1272w, https://substackcdn.com/image/fetch/$s_!ku7b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0092eb95-7b70-4ec7-ab93-7117a7aacd2e_2314x1728.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Consumer lag in Kafka</strong></figcaption></figure></div><h4><strong>What causes consumer lag in Kafka?</strong></h4><ol><li><p><strong>Slow Processing:</strong> If the consumer application takes longer to process messages, it can lead to lag. For example, a consumer that performs complex data transformations may struggle to keep up with incoming messages.</p></li><li><p><strong>High Throughput:</strong> A sudden increase in message production can overwhelm consumers that are not properly scaled. For instance, during a flash sale, a retail application may receive more order messages than its consumers can handle.</p></li><li><p><strong>Network Latency:</strong> Poor network conditions between Kafka brokers and consumers can delay message delivery. For example, a consumer located far from the broker may face lag due to increased latency.</p></li><li><p><strong>Consumer Configuration:</strong> Misconfigurations, such as having too few consumer threads or setting low session timeouts, can also cause lag. For instance, a low value for <code>max.poll.records</code> might limit the number of messages a consumer can process at once.</p></li></ol><h4><strong>How to mitigate consumer lag?</strong></h4><ol><li><p><strong>Scaling Consumers</strong>: Increase the number of consumer instances or partitions to handle higher message throughput effectively.</p></li><li><p><strong>Optimizing Processing Logic</strong>: Streamline the consumer application logic to reduce processing time. For instance, break down complex operations or offload some tasks to background workers.</p></li><li><p><strong>Tuning Kafka Configurations</strong>: Adjust configurations like <code>fetch.min.bytes</code>, <code>fetch.max.bytes</code>, and <code>max.poll.interval.ms</code> to optimize message retrieval and consumption speed.</p></li><li><p><strong>Batch Processing</strong>: Implement batch processing to reduce the frequency of consumer polls and improve throughput.</p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please subscribe to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>When to use Kafka in system design interviews?</h1><p>Kafka can be used either as a <strong>message broker</strong> or a <strong>stream processor</strong>. As a message broker, it decouples services, ensuring asynchronous communication. As a stream processor, Kafka provides a framework for real-time data transformation, aggregation, and analytics.</p><h2>Kafka as a message queue</h2><h3><strong>Ticket Booking Waiting Queue</strong></h3><p>In systems like <strong>BookMyShow</strong>, Kafka queues ticket requests by user ID and timestamp to process them in the correct order. Each consumer handles requests one at a time to avoid race conditions and ensure fair processing. Failed requests are requeued for retries, so users have a fair chance to secure tickets.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WeoZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WeoZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 424w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 848w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 1272w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WeoZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png" width="480" height="262.0879120879121" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:795,&quot;width&quot;:1456,&quot;resizeWidth&quot;:480,&quot;bytes&quot;:97996,&quot;alt&quot;:&quot;Kafka in Ticket Booking systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka in Ticket Booking systems" title="Kafka in Ticket Booking systems" srcset="https://substackcdn.com/image/fetch/$s_!WeoZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 424w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 848w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 1272w, https://substackcdn.com/image/fetch/$s_!WeoZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6df149e7-fa77-4a1d-a827-fbaa47ed653d_1684x919.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Kafka in Ticket Booking systems</strong></figcaption></figure></div><h3><strong>Payment Processing with Kafka: Example from Stripe</strong></h3><p>Payment services (P2P, Card, B2B) send transaction events to Kafka, which then streams these to different services. For example, Kafka sends transaction data (such as transaction ID, location, and IP address) to the <strong>Fraud Detection Service</strong> for compliance checks. At the same time, successful transaction events are sent to the <strong>Wallet Service</strong> for <strong>balance updates</strong> and to the Ledger Service for <strong>accounting</strong>. Check out <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-2-accounting-in-payment-systems">Accounting 101 in Payment Systems</a></strong> for more on accounting system design.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oEfY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oEfY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 424w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 848w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 1272w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oEfY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png" width="564" height="421.45054945054943" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1088,&quot;width&quot;:1456,&quot;resizeWidth&quot;:564,&quot;bytes&quot;:269164,&quot;alt&quot;:&quot;Kafka in Payment Systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka in Payment Systems" title="Kafka in Payment Systems" srcset="https://substackcdn.com/image/fetch/$s_!oEfY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 424w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 848w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 1272w, https://substackcdn.com/image/fetch/$s_!oEfY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F828c964b-dd51-4c74-9b5f-e443f811dd98_2072x1548.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Kafka in Payment Systems</strong></figcaption></figure></div><h3><strong>Using Kafka for YouTube video transcoding</strong></h3><p>For YouTube, Kafka facilitates video transcoding by publishing upload details to a topic for real-time processing. Its replication feature protects against data loss, while multiple consumers handle tasks concurrently, ensuring timely video delivery in various formats. This setup enhances user experience by reducing buffering and enabling support for diverse devices.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!saZ0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!saZ0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 424w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 848w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 1272w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!saZ0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png" width="592" height="348.45054945054943" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:857,&quot;width&quot;:1456,&quot;resizeWidth&quot;:592,&quot;bytes&quot;:257979,&quot;alt&quot;:&quot;Kafka in Video Processing Systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka in Video Processing Systems" title="Kafka in Video Processing Systems" srcset="https://substackcdn.com/image/fetch/$s_!saZ0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 424w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 848w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 1272w, https://substackcdn.com/image/fetch/$s_!saZ0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcece5d92-ad77-447f-8263-c0a0b9bd213e_2432x1432.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 11: Kafka in Video Processing Systems</strong></figcaption></figure></div><h2>Kafka for message streaming</h2><p>Kafka should be used for <strong>message streaming</strong> when:</p><ul><li><p>You require continuous and immediate processing of incoming data, treating it as a real-time flow. For example, designing an <strong>Ad click aggregator</strong></p></li><li><p>Messages need to be processed by multiple consumers simultaneously. For example, while <strong>designing Facebook live comments,</strong> we can use Kafka as a pub/sub system to send comments to multiple consumers.</p></li></ul><div><hr></div><p>Congratulations on making it through! With these <strong>two guides</strong>, you&#8217;ll be well-prepared for interviews that require an in-depth understanding of Kafka.</p><p>In a recent poll, I asked what topics you&#8217;d like to see next, and most of you voted for <strong>Kubernetes</strong> and <strong>Elasticsearch</strong>. Upcoming articles will be about these topics. Looking forward to exploring them together!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Edhd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Edhd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 424w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 848w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 1272w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Edhd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png" width="466" height="327.4385382059801" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:846,&quot;width&quot;:1204,&quot;resizeWidth&quot;:466,&quot;bytes&quot;:92754,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Edhd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 424w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 848w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 1272w, https://substackcdn.com/image/fetch/$s_!Edhd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F305e7781-d74c-49f8-8f44-c3f307f39d4a_1204x846.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Liked this article? Make sure to &#128153; click the like button.</p><p>Feedback or addition? Make sure to &#128172; comment.</p><p>Know someone who would find this helpful? Make sure to &#128257; share this post.</p>]]></content:encoded></item><item><title><![CDATA[What is Kafka? Key components explained]]></title><description><![CDATA[A complete guide to Kafka&#8217;s architecture, components, and workflow, including practical examples and messaging strategies.]]></description><link>https://read.systemdesignblueprint.com/p/introduction-to-kafka</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/introduction-to-kafka</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 30 Oct 2024 04:45:42 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>Introduction to Kafka</h1><p>Building high-performance systems requires a deep understanding of reliable messaging solutions like <strong>Apache Kafka</strong>. In this series, we&#8217;ll explore Kafka&#8217;s <strong>foundational elements</strong> and <strong>advanced features</strong> such as schema registry, commit strategies, and retry mechanisms, helping you design notification systems and event-driven architectures that can handle heavy data traffic.</p><p>As an early engineer at Barq, I&#8217;ve worked on scaling our systems to support <strong>2 million users</strong> and have witnessed <strong>Kafka&#8217;s scaling challenges</strong> firsthand. I tried to look for a practical, interview-focused Kafka guide but couldn&#8217;t find any resource that fully addresses the real-world issues engineers face.</p><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">Cracking system design interviews</a></strong> covers how the "Design Deep Dive" part often decides interview outcomes, especially for senior roles. While <strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">How to use Redis in system design interviews</a></strong> covers <strong>Redis</strong> in detail, this <strong>two-part series</strong> will focus on <strong>Kafka</strong>&#8212;a component that often plays a central role in <strong>scaling discussions</strong>.</p><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide">How to use Kafka in system design interviews</a></strong>?</p></li><li><p>A crash course on Caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong>, <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-1-how-to-design-a-payment-system">How to design a payments system</a>?</strong></p></li></ol><p>List of topics covered in this <strong>2-part series</strong>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iBuT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iBuT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 424w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 848w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 1272w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iBuT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png" width="1456" height="611" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:611,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:245795,&quot;alt&quot;:&quot;Deep dive on kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Deep dive on kafka" title="Deep dive on kafka" srcset="https://substackcdn.com/image/fetch/$s_!iBuT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 424w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 848w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 1272w, https://substackcdn.com/image/fetch/$s_!iBuT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6ff3163-a07c-41fe-a2d2-ca388bd4dfaf_2398x1006.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Kafka series</strong></figcaption></figure></div><p>Before diving in, I want to share that I focus on making <strong>each piece of content</strong> as <strong>relevant</strong> and <strong>valuable</strong> as possible for <strong>system design interviews</strong>. That&#8217;s why I take the time to produce only <strong>in-depth</strong>, <strong>long-form </strong>content<strong>.</strong></p><p>With that in mind, it would mean a lot if you could take a moment to answer a <strong>quick poll</strong>. Your input will help me <strong>choose the topics</strong> that matter <strong>most to you</strong>&#128071;.</p><div class="poll-embed" data-attrs="{&quot;id&quot;:229540}" data-component-name="PollToDOM"></div><div><hr></div><h1>Basics of Kafka</h1><p><strong><a href="https://kafka.apache.org/">Apache Kafka</a></strong> is a distributed event-streaming platform created by LinkedIn and later <strong><a href="https://github.com/apache/kafka">open sourced</a></strong> by the Apache Software Foundation. It&#8217;s built to handle large volumes of data in real time, enabling multiple applications to share and process data streams efficiently.</p><p>Unlike tools that process data in batches at set times, Kafka allows data to be <strong>processed continuously</strong> as it arrives, which is crucial in areas like <strong>e-commerce</strong>, <strong>IoT</strong>, <strong>social media</strong>, and <strong>finance</strong>.</p><p>Let&#8217;s look at a <strong>relatable example</strong> to understand how Kafka is used.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>A real-world example of Kafka</h2><p>Consider a <strong>ride-hailing platform</strong> like Uber, where each ride generates a stream of updates: the <strong>driver&#8217;s location</strong>, <strong>trip status</strong> (e.g., started, ongoing, completed), and the estimated time of arrival (ETA).</p><p>These updates are placed in a queue by a process known as the <em><strong>producer</strong></em>. On the other side, another process called the <em><strong>consumer</strong></em> reads from this queue and performs its business logic.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-WHQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-WHQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 424w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 848w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 1272w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-WHQ!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png" width="1200" height="271.15384615384613" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:329,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:1464257,&quot;alt&quot;:&quot;Kafka producer and consumer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="Kafka producer and consumer" title="Kafka producer and consumer" srcset="https://substackcdn.com/image/fetch/$s_!-WHQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 424w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 848w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 1272w, https://substackcdn.com/image/fetch/$s_!-WHQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb08cbcb4-9dc8-4950-bd7a-d83aa95e8714_8562x1935.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Kafka producer-consumer pattern for ride-hailing like Uber</strong></figcaption></figure></div><p>Let&#8217;s look at the problems with this simple setup (<em><strong>Figure 2</strong></em>).</p><p><strong>Problem 1: Scaling to Millions of Rides</strong></p><p>Uber handled nearly a <strong><a href="https://investor.uber.com/news-events/news/press-release-details/2024/Uber-Announces-Results-for-Fourth-Quarter-and-Full-Year-2023/default.aspx#:~:text=Trips%20during%20the%20quarter%20grew,on%20a%20constant%20currency%20basis.">million rides per day</a></strong> in 2023. For each ride, many events are generated at once, creating a huge volume of data. A <strong>single queue</strong> can&#8217;t handle this volume.</p><p><strong>Solution:</strong> To manage this, Kafka allows data to be grouped into <em><strong>topics</strong></em>. Different types of updates are sent to <strong>separate topics</strong>, as shown in <em><strong>Figure 3</strong></em>:</p><ul><li><p><strong>driver-locations</strong>: For live location updates of drivers.</p></li><li><p><strong>trip-updates</strong>: For ride status updates (start, ongoing, or completed).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!p_qS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!p_qS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 424w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 848w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 1272w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!p_qS!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png" width="1200" height="408.7912087912088" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:496,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:1661320,&quot;alt&quot;:&quot;Topics in kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="Topics in kafka" title="Topics in kafka" srcset="https://substackcdn.com/image/fetch/$s_!p_qS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 424w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 848w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 1272w, https://substackcdn.com/image/fetch/$s_!p_qS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F551115bb-0390-450a-a623-d5ebfa550aff_8157x2781.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Segrating topics based on business case</strong></figcaption></figure></div><p><strong>Problem 2: Consumers Overwhelmed by Location Data</strong></p><p>With so many location updates coming in, a <strong>single consumer</strong> quickly becomes <strong>overloaded</strong>, slowing down processing and causing delays.</p><p><strong>Solution:</strong> Kafka allows multiple consumers to work together in a <em><strong>consumer group</strong></em>, where each event is handled by only one consumer in the group. For example, <em><strong>Figure 3</strong></em><strong> </strong>shows the <strong>driver-location-group</strong> which has two consumers, each handling a part of the data, speeding up processing and avoiding duplication.</p><p></p><p><strong>Problem 3: High-Volume Data and Processing Delays</strong></p><p>A global platform like Uber has thousands of rides happening at once. If all updates go to the same topic, it can create bottlenecks, slowing down tasks like ETA prediction.</p><p><strong>Solution:</strong> Kafka lets us split topics into <em><strong>partitions</strong></em> based on specific keys (<em><strong>Figure 4</strong></em>). For example, the <strong>driver-locations</strong> topic can be divided by <strong>driver ID</strong>, so each driver&#8217;s updates go to a separate partition. This balances the workload, reducing delays and making sure each update is processed promptly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NT3Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NT3Q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 424w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 848w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 1272w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NT3Q!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png" width="1200" height="418.68131868131866" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:508,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:1354111,&quot;alt&quot;:&quot;Topics with multiple partitions in kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="Topics with multiple partitions in kafka" title="Topics with multiple partitions in kafka" srcset="https://substackcdn.com/image/fetch/$s_!NT3Q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 424w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 848w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 1272w, https://substackcdn.com/image/fetch/$s_!NT3Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01bde486-bcdc-499c-9d6d-10c1e5f942b4_7291x2544.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Each topic can have multiple partitions</strong></figcaption></figure></div><h2>Why choose Kafka? Key benefits in distributed systems</h2><ol><li><p><strong>Scalability:</strong> Kafka can handle millions of messages per second by spreading data across multiple servers, making it suitable for high-traffic applications.</p></li><li><p><strong>Reliability:</strong> Kafka&#8217;s replication ensures that data remains available even if some servers fail, providing strong reliability for critical applications.</p></li><li><p><strong>Independent Communication:</strong> Kafka&#8217;s design allows services to send and receive messages independently, enabling multiple services to access the same data without direct connections.</p></li><li><p><strong>Data Persistence:</strong> Kafka can store data for a set time, letting services replay past data when needed, which is useful for reprocessing after failures or for accessing historical data.</p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Kafka components explained</h1><p>In the ride-hailing example above, we highlighted several components through a problem-solution approach. Now, let&#8217;s explore each of these components in <strong>greater detail</strong>.</p><p><em><strong>Figure 5</strong></em> shows a <strong>high-level architecture</strong> with all the components in a Kafka setup.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sZwm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sZwm!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 424w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 848w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 1272w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sZwm!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png" width="1386" height="623.5096153846154" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:655,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1386,&quot;bytes&quot;:10114899,&quot;alt&quot;:&quot;Kafka architecture diagram&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="Kafka architecture diagram" title="Kafka architecture diagram" srcset="https://substackcdn.com/image/fetch/$s_!sZwm!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 424w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 848w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 1272w, https://substackcdn.com/image/fetch/$s_!sZwm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff544868b-d733-4da6-a2d9-671ad39f2ef9_12863x5783.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: High-level architecture of Kafka</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">If you like the above diagram, please <strong>subscribe</strong> to receive more such system design diagrams every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h3>Kafka topics</h3><ul><li><p>A <em><strong>topic</strong></em> is a stream of data where a <strong>specific type of data</strong> is stored. For instance, in the ride-hailing example we discussed above, different topics were created for specific use cases (eg: <strong>&#8220;driver-locations&#8221; </strong>or<strong> &#8220;trip-updates&#8221;</strong>)</p></li><li><p>Kafka has configurable <strong>retention policies</strong> for topics. For example, a topic can be set to keep data only for a few days, after which the older messages are automatically deleted.</p></li></ul><h3>Kafka partitions: a key to parallel processing</h3><ul><li><p>Each topic can be split into multiple <em><strong>partitions</strong></em>, allowing messages to be processed independently. This makes Kafka <strong>scalable</strong> because messages are spread across multiple servers instead of all being stored in one place.</p></li><li><p>Partitions are <strong>replicated</strong> across <strong>different servers</strong> (or &#8220;<strong>brokers</strong>&#8221;) in a Kafka cluster. This replication ensures that if one server fails, other servers have copies of the data, making Kafka <strong>resilient</strong> against <strong>data loss</strong> and <strong>system failures</strong>.</p></li></ul><h3>Kafka brokers: distributing and managing data streams</h3><ul><li><p><em><strong>Brokers</strong></em> are the servers in a Kafka cluster that handle data storage, processing, and retrieval. Each broker stores one or more partitions of a topic and handles requests from <strong>clients (producers)</strong> and <strong>servers (consumers)</strong>.</p></li><li><p>In Kafka, each partition has one broker assigned as the <strong>leader</strong>, which manages the <strong>read and write</strong> operations. Other brokers have <strong>replica</strong> copies of the data in the partition but don&#8217;t process requests unless the leader fails.</p></li></ul><h3>Kafka producers: creating and sending messages</h3><ul><li><p><em><strong>Producers</strong></em> are services or applications that <strong>send messages</strong> to Kafka topics. They decide which topic and partition to send data to, often based on a <strong>unique key</strong> (eg: driver ID). This ensures that messages related to the same entity (like a driver) are sent to the same partition, keeping their order.</p></li><li><p>Producers can be set up with SSL/TLS for secure data transfer. Additionally, they can be configured to prevent duplicate messages, ensuring that each message is sent only once, even if there are retries.</p></li></ul><h3>Kafka consumers: reading and processing data streams</h3><ul><li><p><em><strong>Consumers</strong></em> are services or applications that <strong>read messages</strong> from Kafka topics. They consume messages at their own pace, which allows them to process data independently and resume from where they left off if interrupted.</p></li><li><p>Each consumer tracks its <strong>&#8220;offset&#8221;</strong> which represents the last message it successfully read. By tracking offsets, Kafka enables consumers to resume from the correct position in the event of a failure.</p></li><li><p>Consumers can be grouped into <strong>&#8220;consumer groups&#8221;</strong> allowing Kafka to distribute messages across multiple consumers. For example, if you have a group with three consumers, Kafka will assign each <strong>partition of a topic</strong> to a <strong>different consumer</strong> in the group. This setup ensures each message is processed only once within the group.</p></li></ul><h3>Zookeeper: ensuring fault tolerance and scalability</h3><ul><li><p><em><strong>ZooKeeper</strong></em> is a separate service used to <strong>coordinate</strong> the <strong>Kafka brokers</strong>. It stores <strong>metadata</strong> about the cluster, including which broker is responsible for each partition.</p></li><li><p>When a <strong>broker fails</strong>, ZooKeeper helps <strong>reassign</strong> leadership of its partitions to another broker, ensuring Kafka continues to operate smoothly.</p></li></ul><div><hr></div><h1>How does Kafka work?</h1><h2>Step 1: How are messages produced in Kafka?</h2><p>When an event occurs, a Kafka producer formats a <strong>message</strong> (often called a <strong>record</strong>) and sends it to a <strong>Kafka topic</strong>. Each message includes a required field (<strong>value</strong>), which contains the main data, and three optional fields:</p><ul><li><p><strong>Headers</strong>: These are optional key-value pairs that store metadata about the message, such as HTTP headers.</p></li><li><p><strong>Key</strong>: Used to specify which partition the message should go to, ensuring that all messages for a use case go to the same partition.</p></li><li><p><strong>Timestamp</strong>: Marks the time the event occurred, helping Kafka to maintain chronological order within each partition.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CPjj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CPjj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 424w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 848w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 1272w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CPjj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png" width="241" height="300.16766467065867" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:624,&quot;width&quot;:501,&quot;resizeWidth&quot;:241,&quot;bytes&quot;:34567,&quot;alt&quot;:&quot;Message structure in kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Message structure in kafka" title="Message structure in kafka" srcset="https://substackcdn.com/image/fetch/$s_!CPjj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 424w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 848w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 1272w, https://substackcdn.com/image/fetch/$s_!CPjj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbcc06fc4-0eb3-448e-a95a-caed50473d76_501x624.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Kafka message structure</strong></figcaption></figure></div><p>For example, if a producer is set up to send <strong>messages on driver locations</strong>, it might send a message with the <strong>driver&#8217;s ID</strong> as the <strong>key</strong>, the <strong>current GPS coordinates</strong> as the <strong>value</strong>, and <strong>optional metadata</strong> (eg: device type) in the <strong>headers</strong>.</p><h2>Step 2: How does Kafka partition messages?</h2><p>Kafka decides the partition for each message depending on whether a key is present or not:</p><ul><li><p>If the <strong>key</strong> is <strong>specified</strong>, Kafka applies a <strong>hashing algorithm</strong> to the key, ensuring that all messages with the same key end up in the same partition.</p></li><li><p>If <strong>no key</strong> is <strong>provided</strong>, Kafka can use a <strong>round-robin approach</strong> to distribute messages randomly across the available partitions or a different partitioning logic configured by the producer.</p></li></ul><h2>Step 3: Broker assignment in Kafka</h2><p>After Kafka decides on a partition, it identifies the <strong>broker</strong> that holds that partition through the metadata it maintains about the cluster. The producer uses this information to send messages directly to the broker responsible for the intended partition.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uI_E!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uI_E!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 424w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 848w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 1272w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uI_E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp" width="727" height="372.1790730337079" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:729,&quot;width&quot;:1424,&quot;resizeWidth&quot;:727,&quot;bytes&quot;:140866,&quot;alt&quot;:&quot;Kafka partitions&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka partitions" title="Kafka partitions" srcset="https://substackcdn.com/image/fetch/$s_!uI_E!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 424w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 848w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 1272w, https://substackcdn.com/image/fetch/$s_!uI_E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe92a55c0-f61b-48ab-981b-9fcd923a81a4_1424x729.webp 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Topic partition distribution</strong></figcaption></figure></div><h2>Step 4: Kafka&#8217;s message storage</h2><p>Each partition in Kafka functions as an <strong>append-only log (</strong><em><strong>Figure 8</strong></em><strong>)</strong>, where messages are added sequentially at the end of the log file. This log structure is central to Kafka&#8217;s architecture for several reasons:</p><ul><li><p><strong>Immutability</strong>: Once written, messages cannot be changed or deleted, avoiding inconsistencies.</p></li><li><p><strong>Efficiency</strong>: Writing messages to the end of a log file minimizes disk seek times, making it a quick operation.</p></li><li><p><strong>Scalability</strong>: The simplicity of the append-only log allows Kafka to scale horizontally by adding more partitions across multiple brokers.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kuqn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kuqn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 424w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 848w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 1272w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kuqn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp" width="631" height="483.9644723092999" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:957,&quot;resizeWidth&quot;:631,&quot;bytes&quot;:102108,&quot;alt&quot;:&quot;Kafka message storage&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka message storage" title="Kafka message storage" srcset="https://substackcdn.com/image/fetch/$s_!Kuqn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 424w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 848w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 1272w, https://substackcdn.com/image/fetch/$s_!Kuqn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6db54635-3ffb-466b-9789-c447b4e1a19e_957x734.webp 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Append-only logs in Kafka partition</strong></figcaption></figure></div><p><em><strong>Figure 9</strong></em> shows sample <strong>producer code</strong> using kafkajs, a popular Node.js client for Kafka</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pKOa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pKOa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 424w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 848w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 1272w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pKOa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png" width="644" height="500.390243902439" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:892,&quot;width&quot;:1148,&quot;resizeWidth&quot;:644,&quot;bytes&quot;:158718,&quot;alt&quot;:&quot;Kafka producer in nodejs&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka producer in nodejs" title="Kafka producer in nodejs" srcset="https://substackcdn.com/image/fetch/$s_!pKOa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 424w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 848w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 1272w, https://substackcdn.com/image/fetch/$s_!pKOa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca21151-450f-4fb0-9508-897d85de1db8_1148x892.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Sample producer code in nodejs</strong></figcaption></figure></div><h2>Step 5: Managing message offsets in kafka</h2><p><strong>Each</strong> <strong>message</strong> in a Kafka partition is assigned a unique <strong>sequential</strong> <strong>offset</strong>, which helps consumers track their position as they read messages from the topic (<em><strong>Figure 10</strong></em>). By keeping track of offsets, consumers can <strong>resume reading messages</strong> from the last known position if there is an interruption or failure, ensuring that no messages are lost or skipped.</p><p>Consumers can have different types of <strong>offset commit strategies,</strong> which I will cover in the <strong>next article.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mDBp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mDBp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 424w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 848w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 1272w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mDBp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp" width="625" height="525.0783699059562" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:804,&quot;width&quot;:957,&quot;resizeWidth&quot;:625,&quot;bytes&quot;:115278,&quot;alt&quot;:&quot;Kafka consumer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka consumer" title="Kafka consumer" srcset="https://substackcdn.com/image/fetch/$s_!mDBp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 424w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 848w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 1272w, https://substackcdn.com/image/fetch/$s_!mDBp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19f4348c-5e1e-48cd-940a-39f8232f1489_957x804.webp 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Consumer reads from last committed offset</strong></figcaption></figure></div><h2>Step 6: Kafka replication for fault tolerance</h2><p>Kafka uses a <strong>leader-follower model</strong> for replication to ensure data durability and availability.</p><ul><li><p><strong>Leader Replica</strong>: Each partition has a leader replica located on one broker, which handles <strong>all read and write requests</strong>.</p></li><li><p><strong>Follower Replicas</strong>: Each partition also has <strong>follower replicas</strong> on different brokers that are ready to take over if the leader fails.</p></li><li><p><strong>Synchronization</strong>: Followers continuously sync with the leader to ensure they hold the latest data. If the leader replica for a partition fails, one of the <strong>in-sync followers</strong> is promoted to become the new leader, minimizing downtime.</p></li></ul><p><strong>Zookeeper</strong> is responsible for managing this replication process, monitoring broker health, and <strong>reassigning leadership</strong> when a broker goes down.</p><h2>Step 7: Consuming messages in Kafka</h2><p>In most setups, consumers <strong>poll</strong> <strong>Kafka</strong> to fetch new messages, allowing them to control the pace of consumption. Consumers can be grouped into <strong>consumer groups</strong> to handle high message volumes  where each partition is read by only one consumer.</p><p>For example, if <strong>multiple replicas of a service</strong> need to process driver location updates from a <code>driver-location-updates</code> topic, each replica can be in the same consumer group, with each service reading from separate partitions.</p><p><em><strong>Figure 11</strong></em> shows a sample <strong>consumer code</strong> using kafkajs</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XMQ7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XMQ7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 424w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 848w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 1272w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XMQ7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png" width="672" height="449.05164319248826" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:854,&quot;width&quot;:1278,&quot;resizeWidth&quot;:672,&quot;bytes&quot;:212474,&quot;alt&quot;:&quot;Kafka consumer in nodejs&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka consumer in nodejs" title="Kafka consumer in nodejs" srcset="https://substackcdn.com/image/fetch/$s_!XMQ7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 424w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 848w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 1272w, https://substackcdn.com/image/fetch/$s_!XMQ7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4a30624-475c-4e2a-bcc7-35e23e947d4a_1278x854.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 11: Sample consumer code in nodejs</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Kafka messaging strategies: acks=0, acks=1, acks=all</h1><p>In Kafka, <strong>message delivery mechanisms</strong> are controlled by the <code>acks</code> parameter in the producer configuration. This parameter sets the <strong>number of acknowledgments</strong> required from the Kafka cluster before a message is considered successfully delivered. The value of <code>acks</code> can be <code>0</code>, <code>1</code>, or <code>all</code>. Let&#8217;s break down each one of these:</p><h4><code>acks=0: Unacknowledged message strategy in Kafka</code></h4><ul><li><p>The producer sends the message <strong>without</strong> <strong>waiting</strong> for any <strong>acknowledgment</strong> from the broker. </p></li><li><p><strong>Pro:</strong> Since the producer doesn&#8217;t wait for a response, message delivery is <strong>fast</strong>.</p></li><li><p><strong>Con: </strong>There&#8217;s <strong>no message delivery guarantee</strong> since there is no confirmation the message reached the broker.</p></li><li><p>Best for non-critical data like logging or metrics where losing some messages is acceptable.</p></li></ul><h4><code>acks=1: Single acknowledgment messaging strategy in Kafka</code></h4><ul><li><p>The producer waits for an <strong>acknowledgment</strong> <strong>from the leader broker</strong> once it has written the message to its local log.</p></li><li><p><strong>Pro: </strong>Since the leader broker acknowledges message receipt, this setting reduces the chance of data loss compared to <code>acks=0</code>.</p></li><li><p><strong>Con: </strong>Waiting for an acknowledgment from the leader introduces a slight delay.</p></li></ul><h4><code>acks=all: Ensuring full replication in Kafka messaging</code></h4><ul><li><p>The producer waits for <strong>acknowledgments</strong> from <strong>all in-sync replicas</strong> (<strong>ISRs</strong>), ensuring that the message is fully replicated across the cluster.</p></li><li><p><strong>Pro</strong>: Guarantees the<strong> strongest delivery guarantee</strong> by waiting for all in-sync replicas to acknowledge the message.</p></li><li><p><strong>Con</strong>: Waiting for all replicas to confirm receipt can introduce significant delay, as network and processing time increases.</p></li><li><p><strong>Necessary </strong>for<strong> </strong>strict use cases<strong> </strong>such as financial transactions.</p></li></ul><div><hr></div><h1>Practical use cases of Kafka</h1><h3><strong>Using Kafka for Logging, Monitoring &amp; Alerting</strong></h3><p>Log/metrics collector agents such as <strong>OpenTelemetry</strong> push logs and metrics from application services to <strong>ElasticSearch</strong> (for <strong>indexing</strong>) via <strong>Kafka</strong>. <strong>Kibana</strong> helps in visualizing logs, while <strong>Apache Flink</strong> helps in aggregating and visualizing metrics.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6fkR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6fkR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 424w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 848w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 1272w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6fkR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png" width="1456" height="493" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:493,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1049180,&quot;alt&quot;:&quot;Using kafka for Logging, Monitoring &amp; Alerting&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Using kafka for Logging, Monitoring &amp; Alerting" title="Using kafka for Logging, Monitoring &amp; Alerting" srcset="https://substackcdn.com/image/fetch/$s_!6fkR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 424w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 848w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 1272w, https://substackcdn.com/image/fetch/$s_!6fkR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed57618c-5e17-4bea-b327-707267d8b1c6_5622x1905.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 12: Kafka for Logging, Monitoring and Alerting</strong></figcaption></figure></div><h3><strong>Kafka for data streaming in recommendations</strong></h3><p>Kafka processes <strong>real-time user clickstreams</strong> to enhance recommendation systems. User activity data is ingested into <strong>Kafka</strong>, which routes it to <strong>Flink</strong> for processing, enabling real-time aggregations and calculations. The processed data is sent to a <strong>Data Lake</strong> from where it&#8217;s fed to <strong>machine learning models</strong> to generate recommendations based on user interactions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pWgK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pWgK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 424w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 848w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 1272w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pWgK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png" width="713" height="312.4271978021978" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:638,&quot;width&quot;:1456,&quot;resizeWidth&quot;:713,&quot;bytes&quot;:1237585,&quot;alt&quot;:&quot;Kafka for data streaming in recommendations&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka for data streaming in recommendations" title="Kafka for data streaming in recommendations" srcset="https://substackcdn.com/image/fetch/$s_!pWgK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 424w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 848w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 1272w, https://substackcdn.com/image/fetch/$s_!pWgK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16ffd194-dee3-4076-b8ae-2e6185c54345_4892x2144.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 13: Kafka for designing recommendation systems</strong></figcaption></figure></div><h3><strong>Change Data Capture (CDC) with Kafka: Keeping databases in sync</strong></h3><p>Kafka can be used to sync source and destination data stores in real-time. In a CDC scenario, Kafka captures <strong>real-time changes</strong> from <strong>source databases</strong> via transaction logs. These logs are routed to various <strong>sinks</strong> such as ElasticSearch, Redis, and replica DBs via respective <strong>connectors</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hQx7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hQx7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 424w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 848w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 1272w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hQx7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png" width="1456" height="384" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:384,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:451157,&quot;alt&quot;:&quot;Change Data Capture with Kafka&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Change Data Capture with Kafka" title="Change Data Capture with Kafka" srcset="https://substackcdn.com/image/fetch/$s_!hQx7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 424w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 848w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 1272w, https://substackcdn.com/image/fetch/$s_!hQx7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4401f5dc-7282-40ee-b4b9-068b024d2a54_4241x1118.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 14: Kafka for syncing data sources using CDC</strong></figcaption></figure></div><h2><strong>Kafka for system migration</strong></h2><p><strong>Disaster recovery</strong> is essential for large-scale applications where high availability is critical. <strong>Kafka</strong> supports this by replicating entire production workloads <strong>across data centers</strong>. With <strong><a href="https://docs.confluent.io/platform/current/connect/index.html">Kafka Connect</a></strong>, large volumes of events can be easily ingested, making it an ideal tool for replicating production data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Fp90!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Fp90!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 424w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 848w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 1272w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Fp90!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png" width="1456" height="394" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:394,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:305101,&quot;alt&quot;:&quot;Kafka for system migration&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Kafka for system migration" title="Kafka for system migration" srcset="https://substackcdn.com/image/fetch/$s_!Fp90!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 424w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 848w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 1272w, https://substackcdn.com/image/fetch/$s_!Fp90!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4b82c9e-3d25-44d0-872d-74cfb789a2ef_3330x901.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 15: Kafka for replicating production setups for Disaster Recovery</strong></figcaption></figure></div><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Conclusion</h1><p>This post was more about learning the <strong>basics of Kafka</strong> along with its <strong>most common use cases</strong>. In the next post, I will try to cover as many <strong>scaling challenges</strong> as possible based on my <strong>practical experiences</strong> with Kafka in a large-scale fintech system. Stay tuned &#129304;&#127995;.</p><div><hr></div><p>That&#8217;s all for today. Thanks for reading!</p><p><strong>Liked</strong> this article? Make sure to &#128153; click the like button.</p><p><strong>Feedback</strong> or addition? Make sure to &#128172; <strong>comment</strong>.</p><p>Know someone who would find this helpful? Make sure to &#128257; <strong>share</strong> this post.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Probabilistic data structures: Bloom Filters, Count Min Sketch & Skip Lists]]></title><description><![CDATA[Understand Bloom filters, Count Min Sketch, and Skip Lists and how they work in system design to improve data efficiency]]></description><link>https://read.systemdesignblueprint.com/p/probabilistic-data-structures-bloom-filter-count-min-sketch</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/probabilistic-data-structures-bloom-filter-count-min-sketch</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 23 Oct 2024 04:45:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the <strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">previous article</a></strong>, we covered common <strong><a href="https://read.systemdesignblueprint.com/i/150216521/understanding-redis-data-structures">data structures in Redis</a></strong>, which led me to investigate their internals. In this post, we&#8217;ll explore <strong>key probabilistic data structures</strong>, including one that serves as the foundation for <strong>Redis Sorted Sets</strong>.</p><h1>Why use probabilistic data structures?</h1><p>Analyzing massive Terabyte-sized datasets is increasingly common in <strong>web analytics</strong> and <strong>online advertising</strong>. Traditional methods, like <strong>Hadoop</strong> and <strong>MapReduce</strong>, can lead to slow, high-latency processes, making them unsuitable for real-time applications. </p><p>While <strong>simple metrics</strong>, such as <strong>total page views</strong> or <strong>average conversion prices</strong>, can be efficiently summarized using daily data or in-stream counters, more <strong>complex metrics</strong>&#8212;like <strong>unique visitor counts</strong> or the <strong>most frequent items</strong>&#8212;are resource-intensive to compute directly. </p><p>The rest of the article will explore <strong>three probabilistic data structures</strong> that can efficiently handle such complex queries. But first, let&#8217;s walk through an example to understand the challenges involved.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h2>Real-world examples of Probabilistic Data Structures</h2><p>Given a dataset with <strong>10 million</strong> integers, where no more than <strong>1 million</strong> are <strong>distinct</strong> (resulting in a <strong>40MB</strong> raw dataset size at 4 bytes per integer), here are some <strong>key</strong> <strong>questions</strong> we need to address:</p><ul><li><p>Does the dataset contain a specific element (<strong>Membership Query</strong>)?</p></li><li><p>What are the <strong>most frequent</strong> elements (<strong>heavy hitters</strong> or <strong>top-k elements</strong>)?</p></li><li><p>What are the frequencies of those elements?</p></li><li><p>How many elements fall within a specific range (<strong>Range Query</strong>, like <code>SELECT count(v) WHERE v &gt;= c1 AND v &lt; c2</code> in SQL)?</p></li></ul><p>For the <strong>first question</strong>, using a sorted list or hash table would need around <strong>4MB</strong> <strong>space</strong> (1M * 4 bytes). This works for smaller datasets, but for datasets that grow into terabytes, it&#8217;s not scalable. A <strong>probabilistic data structure</strong> like <strong>LogLogCounter</strong>, however, can handle this in just <strong>2KB</strong> <strong>space</strong>!</p><p>Sounds interesting, right? Let&#8217;s dive into the data structures that help answer each of these questions efficiently. </p><p>Read on till the end for a concise comparison between <strong>probabilistic</strong> and <strong>non-probabilistic</strong> data structures.</p><div><hr></div><div><hr></div><h1>What are probabilistic data structures?</h1><p>Probabilistic data structures are designed to efficiently answer the questions mentioned above while <strong>optimizing</strong> for <strong>time</strong> and <strong>space</strong> but with an <strong>acceptable</strong> <strong>margin</strong> of <strong>error</strong>.</p><p>In simple terms, <strong>non-probabilistic </strong>data structures provide exact answers&#8212;such as <em><strong>"How many items are there? Exactly 1,600,000"</strong></em>&#8212;<strong>probabilistic</strong> data structures might respond with <em><strong>"How many items are there? Approximately 1,523,425, with a 99% probability."</strong></em></p><h2>Types of Probabilistic Data Structures</h2><h3>Bloom Filters</h3><h4>What are bloom filters and why use them?</h4><p><strong>Bloom filters</strong> can estimate if a dataset contains a specific element (<strong>Membership Query</strong>) in a time-efficient manner. They can return 2 responses:</p><ul><li><p>If a Bloom filter returns <em><strong>False</strong></em>, the item is <strong>not</strong> in the set. </p></li><li><p>If it returns <em><strong>True</strong></em>, the item <strong>might be</strong> in the set, but there&#8217;s a chance of a <strong>false positive</strong> (we&#8217;ll have a look at why this happens shortly).</p></li></ul><h4>How do bloom filters work?</h4><p><strong>Membership Query using bloom filters</strong></p><p>A Bloom filter has <strong>two key</strong> components:</p><ol><li><p><strong>Data structure</strong>: A bit array of length <code>n</code>. For example, a bit array of length 10 (with indices <code>0-9</code>) [<em><strong>Figure 1</strong></em>]</p></li><li><p><strong>Hash functions</strong>: There are <code>k</code> hash functions that output an index between 0 and <em>n-1</em>, setting the corresponding bit in the array.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MDCI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MDCI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 424w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 848w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 1272w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MDCI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png" width="1180" height="184" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:184,&quot;width&quot;:1180,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:72933,&quot;alt&quot;:&quot;Bloom filters data structure&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Bloom filters data structure" title="Bloom filters data structure" srcset="https://substackcdn.com/image/fetch/$s_!MDCI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 424w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 848w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 1272w, https://substackcdn.com/image/fetch/$s_!MDCI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F77666ec1-4688-4a09-98ec-8e4b9fc1ec9e_1180x184.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Bit array data structure used in Bloom Filters</strong></figcaption></figure></div><p>Let's see how items are <strong>added</strong> and <strong>looked up</strong> in a Bloom filter.</p><h5><em>Adding an item</em></h5><p>To <strong>add an item</strong> to a Bloom filter, the following steps are performed:</p><ol><li><p>The item is hashed using <em>k</em> hash functions.</p></li><li><p>The modulo operation with the bit array length (<em>n</em>) is applied to the hash outputs to find the positions (buckets) in the array.</p></li><li><p>The bits at these <em>k</em> positions are set to 1.</p></li></ol><p>In <em><strong>Figure 2</strong></em>, the items X and Y are added as follows:</p><p>For <strong>X</strong>:</p><ul><li><p>hash1(<em>X</em>) mod 10 = 1</p></li><li><p>hash2(<em>X</em>) mod 10 = 2</p></li><li><p>hash3(<em>X</em>) mod 10 = 4</p></li></ul><p>For <strong>Y</strong>:</p><ul><li><p>hash1(<em>Y</em>) mod 10 = 4</p></li><li><p>hash2(<em>Y</em>) mod 10 = 5</p></li><li><p>hash3(<em>Y</em>) mod 10 = 7</p></li></ul><p>The bucket at index 4 is set to one both the items <em>X</em> and <em>Y</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WfvI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WfvI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 424w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 848w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 1272w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WfvI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png" width="648" height="357.1485148514852" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:668,&quot;width&quot;:1212,&quot;resizeWidth&quot;:648,&quot;bytes&quot;:152819,&quot;alt&quot;:&quot;Adding an item in bloom filters&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Adding an item in bloom filters" title="Adding an item in bloom filters" srcset="https://substackcdn.com/image/fetch/$s_!WfvI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 424w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 848w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 1272w, https://substackcdn.com/image/fetch/$s_!WfvI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61715100-a1f9-4a06-87dd-04327a38c4e2_1212x668.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Adding an item in a Bloom Filter</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h5><em>Looking up an item</em></h5><p>To check if an <strong>item is present</strong> in a Bloom filter, the following steps are taken:</p><ol><li><p>The item is hashed using the same <em>k</em> hash functions as during the insert operation.</p></li><li><p>The modulo operation with the bit array length (<em>n</em>) is applied to each hash output to find the corresponding array positions (buckets).</p></li><li><p>The bits at these positions are checked.</p><ol><li><p>If any of these bits are 0, the item is <strong>not</strong> in the Bloom filter.</p></li><li><p>If all bits are 1, the item <strong>might</strong> be in the Bloom filter due to potential hash collisions or bits set by other items.</p></li></ol></li></ol><p><em>Examples</em> (<em><strong>Figure 3</strong></em> &amp; <em><strong>Figure 4</strong></em>):</p><p>For <strong>Y</strong> (<strong>existing item</strong>)</p><ul><li><p>hash1(<em>Y</em>) mod 10 = 4</p></li><li><p>hash2(<em>Y</em>) mod 10 = 5</p></li><li><p>hash3(<em>Y</em>) mod 10 = 7</p></li></ul><p>Since all the bits at these positions are 1, <strong>Y might</strong> be in the filter.</p><p>For <strong>Z</strong> (<strong>item that doesn&#8217;t exist</strong>)</p><ul><li><p>hash1(<em>Z</em>) mod 10 = 3</p></li><li><p>hash2(<em>Z</em>) mod 10 = 5</p></li><li><p>hash3(<em>Z</em>) mod 10 = 8</p></li></ul><p>Since the bits at positions 3 and 8 are 0, <strong>Z is not</strong> in the Bloom filter.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SpQ4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SpQ4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 424w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 848w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 1272w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SpQ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png" width="514" height="284.990099009901" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:672,&quot;width&quot;:1212,&quot;resizeWidth&quot;:514,&quot;bytes&quot;:132861,&quot;alt&quot;:&quot;Looking up an item in bloom filters&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Looking up an item in bloom filters" title="Looking up an item in bloom filters" srcset="https://substackcdn.com/image/fetch/$s_!SpQ4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 424w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 848w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 1272w, https://substackcdn.com/image/fetch/$s_!SpQ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F273e047d-c4f9-4e74-a876-8262a3fa984d_1212x672.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Looking up &#8220;existing&#8221; item in Bloom Filter</strong></figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1VvT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1VvT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 424w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 848w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 1272w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1VvT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png" width="518" height="290.62706270627064" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:680,&quot;width&quot;:1212,&quot;resizeWidth&quot;:518,&quot;bytes&quot;:131630,&quot;alt&quot;:&quot;Looking up an item in bloom filters&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Looking up an item in bloom filters" title="Looking up an item in bloom filters" srcset="https://substackcdn.com/image/fetch/$s_!1VvT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 424w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 848w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 1272w, https://substackcdn.com/image/fetch/$s_!1VvT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43e9ffe5-da47-4de2-83b4-9826cd9c9114_1212x680.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Looking up &#8220;non-existent&#8221; item in Bloom Filter</strong></figcaption></figure></div><h5><em>False Positives</em></h5><p>In <em><strong>Figure 5</strong></em>, the Bloom filter is queried to check if <strong>F</strong> is a member. The following calculations are made:</p><ul><li><p>hash1(<em>F</em>) mod 10 = 2</p></li><li><p>hash2(<em>F</em>) mod 10 = 4</p></li><li><p>hash3(<em>F</em>) mod 10 = 7</p></li></ul><p>Bits at positions 2, 4, and 7 are set, indicating that item <strong>F</strong> <strong>might</strong> be present. However, this is a <strong>false positive</strong> because <strong>X</strong> and <strong>Y</strong> have set those bits. Therefore, <strong>F</strong> is not a member of the Bloom filter, illustrating the inherent uncertainty and possibility of false positives in Bloom Filters.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!906d!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!906d!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 424w, https://substackcdn.com/image/fetch/$s_!906d!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 848w, https://substackcdn.com/image/fetch/$s_!906d!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 1272w, https://substackcdn.com/image/fetch/$s_!906d!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!906d!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png" width="516" height="292.91089108910893" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:688,&quot;width&quot;:1212,&quot;resizeWidth&quot;:516,&quot;bytes&quot;:129339,&quot;alt&quot;:&quot;False positives in bloom filters&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="False positives in bloom filters" title="False positives in bloom filters" srcset="https://substackcdn.com/image/fetch/$s_!906d!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 424w, https://substackcdn.com/image/fetch/$s_!906d!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 848w, https://substackcdn.com/image/fetch/$s_!906d!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 1272w, https://substackcdn.com/image/fetch/$s_!906d!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa7f01614-88f5-4fa5-bd6f-8524901fa73f_1212x688.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: False positives in Bloom Filter</strong></figcaption></figure></div><h5><em>Time &amp; Space Complexity of bloom filters</em></h5><p>Bloom filters are more efficient than hash sets for large data lookups due to their constant time and space complexity.</p><ul><li><p><strong>Time Complexity</strong>: <em>O(k)</em> to add and lookup an element, where <em>k</em> is the number of hash functions.</p></li><li><p><strong>Space Complexity</strong>: Constant space usage, as Bloom filters utilize a fixed number of bits. Each item contributes only a few bits, and bits are reused. Notably, Bloom filters do not store actual values.</p></li></ul><h4>Practical use cases of Bloom Filters</h4><p>The bloom filters are useful in high-ingestion systems to prevent expensive disk seeks. Few applications of bloom filters</p><ul><li><p>Reducing disk lookups for the non-existing keys in a database</p></li><li><p>Determining whether a user ID is already taken</p></li><li><p>Filtering out previously shown posts on recommendation engines</p></li><li><p>Checking words for misspellings and <a href="https://systemdesign.one/system-design-pastebin/#write-path">profanity with a spellchecker</a></p></li><li><p>identifying malicious URLs, blocked IPs, and fraudulent transactions</p></li><li><p>Log-structured merge tree (<a href="https://en.wikipedia.org/wiki/Log-structured_merge-tree">LSM</a>) storage engine used in databases such as <a href="https://cassandra.apache.org/_/index.html">Cassandra</a> uses a bloom filter to check if the key exists in the SSTable</p></li><li><p><a href="https://en.wikipedia.org/wiki/MapReduce">MapReduce</a> uses the bloom filter for the efficient processing of large datasets</p></li></ul><h4>Bloom filter calculator</h4><p><strong><a href="https://hur.st/bloomfilter/">hur.st bloom filter calculator</a></strong> can be used to choose the optimal size for the bloom filter and explore how the different parameters interact. The accuracy of the bloom filter depends on the following:</p><ul><li><p>number of hash functions (k)</p></li><li><p>quality of hash functions</p></li><li><p>length of the bit array (n)</p></li><li><p>number of items stored in the bloom filter</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h3>Count-Min Sketch: Efficient frequency counting</h3><h4>What are bloom filters and why use them?</h4><p>A <strong>Count-Min Sketch (CMS)</strong> is a data structure specifically optimized for two scenarios without the need to store the actual elements:</p><ol><li><p>Estimating the <strong>frequency of elements</strong> in a stream</p></li><li><p>Identifying <strong>heavy hitters</strong></p></li></ol><p>CMS utilizes a 2-D array of size <code>w x d</code> (width &#215; depth) [<em><strong>Figure 6</strong></em>], where <code>w</code> represents the number of counters in each row and <code>d</code> denotes the number of rows. Each row is associated with its hash function.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6S8J!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6S8J!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 424w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 848w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 1272w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6S8J!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png" width="281" height="233.02439024390245" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:306,&quot;width&quot;:369,&quot;resizeWidth&quot;:281,&quot;bytes&quot;:60175,&quot;alt&quot;:&quot;Count min sketch data structure&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Count min sketch data structure" title="Count min sketch data structure" srcset="https://substackcdn.com/image/fetch/$s_!6S8J!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 424w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 848w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 1272w, https://substackcdn.com/image/fetch/$s_!6S8J!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dea9bdd-9314-4986-9628-9be4215129cb_369x306.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 6: 4 x 3 (w x d) 2D array representing a Count Min Sketch</strong></figcaption></figure></div><h4>How does a count-min sketch work?</h4><p><strong>A. Frequency Estimation using count-min sketch</strong></p><h5><em><strong>Adding an item</strong></em></h5><ol><li><p>The item is hashed using <code>d</code> independent hash functions. Each hash function corresponds to one row in the 2D array and maps the element to one of the <code>w</code> counters in that row.</p></li><li><p>For each hash function, increment the counter at the corresponding position in the array.</p></li></ol><p><em><strong>Figure 7</strong></em> shows the counters of each position after the items <code>X</code> and <code>Y</code> are added to the CMS:</p><p>For <strong>X</strong>:</p><ul><li><p>hash1(<em>X</em>) mod 4 = 2</p></li><li><p>hash2(<em>X</em>) mod 4 = 3</p></li><li><p>hash3(<em>X</em>) mod 4 = 1</p></li></ul><p>For <strong>Y</strong>:</p><ul><li><p>hash1(<em>Y</em>) mod 4 = 3</p></li><li><p>hash2(<em>Y</em>) mod 4 = 3</p></li><li><p>hash3(<em>Y</em>) mod 4 = 2</p></li></ul><p>The counters at the above positions in the corresponding rows are incremented by 1.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GarK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GarK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 424w, https://substackcdn.com/image/fetch/$s_!GarK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 848w, https://substackcdn.com/image/fetch/$s_!GarK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 1272w, https://substackcdn.com/image/fetch/$s_!GarK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GarK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png" width="510" height="279.09448818897636" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:417,&quot;width&quot;:762,&quot;resizeWidth&quot;:510,&quot;bytes&quot;:126741,&quot;alt&quot;:&quot;Adding an item in count min sketch&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Adding an item in count min sketch" title="Adding an item in count min sketch" srcset="https://substackcdn.com/image/fetch/$s_!GarK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 424w, https://substackcdn.com/image/fetch/$s_!GarK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 848w, https://substackcdn.com/image/fetch/$s_!GarK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 1272w, https://substackcdn.com/image/fetch/$s_!GarK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F141e2c84-cee2-4099-865a-fe1280bfb7ae_762x417.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Adding an item in Count Min Sketch</strong></figcaption></figure></div><h5><em>Querying an item&#8217;s frequency</em></h5><ol><li><p>Apply the same <code>d</code> hash functions to the query item.</p></li><li><p>Find the counter at the corresponding position in its row for each hash function. The <strong>estimated frequency</strong> of the element is the <strong>minimum value</strong> of these <strong>counters</strong>.</p></li></ol><p><em><strong>Figure 8</strong></em> shows how the frequency of an item <code>X</code> is determined:</p><ul><li><p>hash1(Y) mod 4 = 2 (counter = 1)</p></li><li><p>hash2(Y) mod 4 = 3 (counter = 2)</p></li><li><p>hash3(Y) mod 4 = 1 (counter = 1)</p></li></ul><p>The estimated frequency of <code>Y</code> is <code>min(1, 2, 1) = 1</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sVjU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sVjU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 424w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 848w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 1272w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sVjU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png" width="510" height="279.2857142857143" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:414,&quot;width&quot;:756,&quot;resizeWidth&quot;:510,&quot;bytes&quot;:104932,&quot;alt&quot;:&quot;Querying an item in count min sketch&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Querying an item in count min sketch" title="Querying an item in count min sketch" srcset="https://substackcdn.com/image/fetch/$s_!sVjU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 424w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 848w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 1272w, https://substackcdn.com/image/fetch/$s_!sVjU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04dd6be6-8134-4f05-8824-ad25d4a084ce_756x414.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Querying an item in Count Min Sketch</strong></figcaption></figure></div><h5><em>Time &amp; Space Complexity of count-min sketch</em></h5><ul><li><p><strong>Time Complexity</strong>: <code>O(d)</code>, where <code>d</code> is the number of hash functions (or rows).</p></li><li><p><strong>Space Complexity</strong>: <code>O(w &#215; d)</code>.</p></li></ul><p><strong>B. Heavy Hitters using count-min sketch</strong></p><p>CMS combined with a heap can efficiently identify <strong>heavy hitters</strong>/<strong>elements with frequencies greater than</strong> <code>k%</code> of the total dataset size (<code>N</code>). The approach is simple:</p><ol><li><p>Initialize a CMS to track the frequency of elements and an empty heap to store the top elements. Set a counter <code>N</code> to track the total number of processed elements.</p></li><li><p>For each new element, update the CMS and estimate its frequency. If the frequency exceeds <code>k x N</code>, add the element to the heap.</p></li><li><p>Periodically prune the heap by removing elements that no longer meet the <code>k x N</code> frequency threshold.</p></li></ol><p><strong>C. Range queries using count-min sketch</strong></p><p>An array of Count-Min Sketches can also handle <strong>Range Queries</strong> (eg: SQL query "<code>SELECT count(v) WHERE v &gt;= c1 AND v &lt; c2</code>), though I won't go into detail here as it's outside the focus of this article.</p><p>This is mainly relevant when designing systems like Databases (eg: Clickhouse), Analytics tools (eg: Mixpanel), or Metrics platforms (eg: Grafana). If you're interested in learning more, I've included references at the end for further reading.</p><p></p><h4>Practical use cases <strong>of Count-Min Sketch</strong></h4><ul><li><p><strong>Network Traffic Monitoring</strong>: Estimate the frequency of IP addresses in a traffic stream.</p></li><li><p><strong>Data Streams</strong>: Track word counts in massive text streams (e.g., search query trends).</p></li><li><p><strong>Approximate Counting in Distributed Systems</strong>: Manage resource usage and limit counters in large-scale systems where exact counting is expensive.</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h3>Skip List</h3><h4>What are skip lists &amp; why use them?</h4><p>A <strong>Skip List</strong> is a probabilistic data structure that provides an <strong>alternative</strong> to <strong>balanced</strong> <strong>trees</strong> (self-balancing BST and AVL trees). It uses multiple linked lists to represent different <em><strong>"levels"</strong></em> of the dataset, allowing for fast lookups, insertions, and deletions with <strong>logarithmic</strong> <strong>time</strong> <strong>complexity</strong>.</p><p><em><strong>Figure 9</strong></em> shows the representation of a SkipList. </p><p>There are multiple levels, with each level containing a subset of elements from the previous one, also in sorted order. While it may look like nodes appear multiple times, like Node 5 being in all three levels, it's the same node connected across different levels. For example, Node 5 connects to Nodes 24, 15, and 7 at various levels.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!78k2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!78k2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 424w, https://substackcdn.com/image/fetch/$s_!78k2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 848w, https://substackcdn.com/image/fetch/$s_!78k2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 1272w, https://substackcdn.com/image/fetch/$s_!78k2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!78k2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png" width="1456" height="419" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:419,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:91421,&quot;alt&quot;:&quot;Skip list data structure&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Skip list data structure" title="Skip list data structure" srcset="https://substackcdn.com/image/fetch/$s_!78k2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 424w, https://substackcdn.com/image/fetch/$s_!78k2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 848w, https://substackcdn.com/image/fetch/$s_!78k2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 1272w, https://substackcdn.com/image/fetch/$s_!78k2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F29b054a9-22f9-4555-9380-9bc1a00654bc_1694x488.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Skip List data structure</strong></figcaption></figure></div><h4>How do skip lists work?</h4><p>Let&#8217;s have a look at various CRUD scenarios in a Skip List</p><h5>Adding an item</h5><p>The gif in <em><strong>Figure 9</strong></em> explains this scenario:</p><ol><li><p>Start at the topmost level and move forward through the list until you find a node larger than the element to be inserted.</p></li><li><p>Drop down to the next level and continue searching from that position, repeating this process until you reach the base level.</p></li><li><p>Insert the element at the correct position in the base level.</p></li><li><p>Flip a coin to decide if the element should be promoted to the next higher level.</p></li><li><p>If the coin returns heads, insert the element at the same position in the higher level, repeating the promotion and insertion process.</p></li><li><p>Stop promoting the element when the coin returns tails, finishing the insertion process.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!o5s-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!o5s-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 424w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 848w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 1272w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!o5s-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif" width="800" height="277" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:277,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:53629,&quot;alt&quot;:&quot;Adding an item in skip lists&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Adding an item in skip lists" title="Adding an item in skip lists" srcset="https://substackcdn.com/image/fetch/$s_!o5s-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 424w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 848w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 1272w, https://substackcdn.com/image/fetch/$s_!o5s-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd6a30972-50f3-4f5b-a420-fd87855d8e4c_800x277.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Adding an item in Skip List</strong></figcaption></figure></div><h5>Querying an item</h5><p>The gif in <em><strong>Figure 11</strong></em> explains how an item is queried in a SkipList:</p><ol><li><p>Start at the topmost level and move forward, comparing each node&#8217;s value with the target.</p></li><li><p>If a larger node is found without matching the target, drop down to the next level and resume the search.</p></li><li><p>Continue this process at each level, moving down and checking nodes until you reach the base level.</p></li><li><p>Stop if the target is found at any level, otherwise the search concludes at the base level. If the target is still not found, it is not in the skip list.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Hilj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Hilj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 424w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 848w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 1272w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Hilj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif" width="847" height="376" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/def00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:376,&quot;width&quot;:847,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:260121,&quot;alt&quot;:&quot;Searching an element in skip lists&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Searching an element in skip lists" title="Searching an element in skip lists" srcset="https://substackcdn.com/image/fetch/$s_!Hilj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 424w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 848w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 1272w, https://substackcdn.com/image/fetch/$s_!Hilj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdef00df3-f0d1-4808-9957-9049dffa5f9a_847x376.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 11: Searching an element in Skip List</strong></figcaption></figure></div><h5>Deleting an item</h5><p><em><strong>Figure 12</strong></em> illustrates deletion of an item from a SkipList:</p><ol><li><p>Traverse the list at the topmost level to find the node just before the element to be deleted.</p></li><li><p>Drop to lower levels from the current node, continuing to search for the node before the target element.</p></li><li><p>At each level where the element exists, update pointers to bypass and unlink the target node.</p></li><li><p>Once the element is removed from all levels, the deletion is complete.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!k6nD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!k6nD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 424w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 848w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 1272w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!k6nD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif" width="847" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:847,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:292132,&quot;alt&quot;:&quot;Deleting an item from skip lists&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Deleting an item from skip lists" title="Deleting an item from skip lists" srcset="https://substackcdn.com/image/fetch/$s_!k6nD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 424w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 848w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 1272w, https://substackcdn.com/image/fetch/$s_!k6nD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F675fef7e-a328-455a-8459-4d3cf1323641_847x400.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 12: Deleting an item from Skip List</strong></figcaption></figure></div><h5>Time and Space Complexity of a skip list</h5><ul><li><p><strong>Time Complexity</strong>:</p><ul><li><p><strong>Average Case:</strong> The level of a node is determined by coin flips and <strong>fewer nodes</strong> appear on <strong>higher levels</strong>. This results in the <strong>average</strong> time complexity of <strong>O(log n)</strong> for most operations, as only a few levels need to be traversed.</p></li><li><p><strong>Worst Case:</strong> Although rare, coin flips can create an unbalanced skip list where most nodes are concentrated in just 1 or 2 levels, leading to a <strong>worst-case </strong>time complexity of<strong> O(n)</strong>.</p></li></ul></li><li><p><strong>Space Complexity</strong>: <strong>O(n)</strong>, where n is the number of elements.</p></li></ul><h4>Use cases of skip lists</h4><p>Skip lists are useful for fast lookups in large datasets. Some practical use cases include:</p><ul><li><p><strong>In-Memory Databases</strong>: Used for indexing and maintaining sorted data with fast insertions and lookups.</p></li><li><p><strong>Priority Queues</strong>: Efficiently manage dynamically changing priorities.</p></li><li><p><strong>Redis Sorted Sets</strong>: Skip lists are the underlying data structure in sorted sets.</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><div><hr></div><h1>References for reading on probabilistic data structures</h1><ul><li><p>https://highlyscalable.wordpress.com/2012/05/01/probabilistic-structures-web-analytics-data-mining/</p></li></ul><div><hr></div><div><hr></div><h1>Conclusion</h1><p>The table in <em><strong>Figure 13</strong></em> quickly compares commonly used probabilistic data structures, highlighting their use cases, error rates, and memory consumption.</p><p>It's worth noting that <strong>LogLog Counter</strong> and <strong>Stream-Summary</strong> are two additional probabilistic data structures, which we will explore in detail later.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!c7F3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!c7F3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 424w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 848w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 1272w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!c7F3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png" width="540" height="773.3611691022965" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1372,&quot;width&quot;:958,&quot;resizeWidth&quot;:540,&quot;bytes&quot;:279725,&quot;alt&quot;:&quot;Probabilistic data structures cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Probabilistic data structures cheat sheet" title="Probabilistic data structures cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!c7F3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 424w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 848w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 1272w, https://substackcdn.com/image/fetch/$s_!c7F3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1fa1bde-3f36-428f-a048-0221bdeaa408_958x1372.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 13: Comparison matrix of probabilistic data structures</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Alamanac</strong>! Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Thanks for reading!</p><p><strong>Liked</strong> this article? Make sure to &#128153; click the like button.</p><p><strong>Feedback</strong> or addition? Make sure to &#128172; <strong>comment</strong>.</p><p>Know someone who would find this helpful? Make sure to &#128257; <strong>share</strong> this post.</p>]]></content:encoded></item><item><title><![CDATA[How to use Redis in system design interviews?]]></title><description><![CDATA[Master Redis for system design interviews. Discover how to leverage its speed, commands, and popular use cases such as caching, rate limiting, and async communication]]></description><link>https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 16 Oct 2024 04:45:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">Cracking system design interviews</a></strong> often requires a deep understanding of specific technologies, especially for senior roles. Even for junior positions, building a solid foundation early on helps. <strong>Redis</strong> is one of those <strong>key components</strong> interviewers often expect you to know well, and here&#8217;s why:</p><ol><li><p><strong>Versatility</strong>: Redis isn't just a cache; it handles diverse use cases, such as rate limiting, session management, leaderboards, etc.</p></li><li><p><strong>Simplicity and Speed</strong>: Redis excels at executing simple operations quickly, making it perfect for high-performance systems.</p></li></ol><p>Redis can do much more than just caching and can be a major technology in the <strong><a href="https://read.systemdesignblueprint.com/i/148928014/step-address-key-issues-in-the-design">Design Deep Dive</a></strong> interview phase. In this article, we&#8217;ll dive into the <strong>key systems</strong> you can design using Redis.</p><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">How to crack any system design interview</a>?</strong></p></li><li><p>A crash course on Caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong>, <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-9-load-balancers-101">What are Load Balancers</a></strong>?</p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Redis Basics: Why and how Redis is essential for system design</h2><p>Redis is an <strong>open-source, in-memory</strong> data store renowned for its speed and efficiency. It supports various data structures like strings, hashes, lists, and sets, making it ideal for core use cases such as caching, rate limiting, pub/sub messaging, and even designing social media feeds.</p><h3>Why Is Redis Fast?</h3><ul><li><p><strong>In-Memory Storage:</strong> Redis stores data in RAM, making it significantly faster than disk-based storage.</p></li><li><p><strong>Single-threaded Architecture:</strong> Redis processes all requests on a single thread, avoiding the overhead of context switching and locks.</p></li><li><p><strong>Non-blocking I/O:</strong> An event loop and asynchronous request handling ensure low latency and high throughput.</p></li><li><p><strong>Optimized Data Structures:</strong> Redis uses efficient data structures for fast read/write operations.</p></li></ul><h3>Understanding Redis data structures</h3><p><strong>Strings </strong>are commonly used for:</p><ol><li><p>Storing session data for users in web applications</p></li><li><p>Caching frequently accessed API responses.</p></li></ol><p><strong>Lists </strong>are commonly used for:</p><ol><li><p>Implementing a message queue for asynchronous tasks.</p></li><li><p>Storing a timeline in social media applications.</p></li></ol><p><strong>Sets </strong>are commonly used for:</p><ol><li><p>Tracking unique visitors to a website.</p></li><li><p>Maintaining a list of unique hashtags used in posts.</p></li></ol><p><strong>Sorted Sets:</strong> This data structure combines a <strong>hash table</strong> and a <strong>sorted tree</strong>. Each entry has a string "<strong>member</strong>" (the key) and a double "<strong>score</strong>" (the sorted value). This allows you to access entries directly by member or score, and retrieve members in order based on their score.</p><p>Use cases of Sorted Sets:</p><ol><li><p>Creating a leaderboard for an online game.</p></li><li><p>Ranking search results based on the number of likes for a keyword.</p></li></ol><p><strong>Hashmaps:</strong> Useful for storing structured data like objects. Commonly used for:</p><ol><li><p>Storing user profiles with fields like name, email, and age.</p></li><li><p>Caching database query results in a structured format.</p><p></p></li></ol><h3>Essential Redis commands for system design</h3><pre><code>SET foo 1  
GET foo     # Returns 1
INCR foo    # Returns 2
ZADD leaderboard 100 user1    # Adds a user to a sorted set (leaderboard) with score 100
ZRANGE leaderboard 0 0 WITHSCORES    # Returns the top user and score in the leaderboard
SADD users user1     # Adds user1 to a set of users
SMEMBERS users     # Retrieves all members of the set
HSET user:1 name "John Doe" age 30    # Stores structured data in a hash
HGETALL user:1    # Retrieves all fields and values from the hash
XADD mystream * name Sara surname OConnor    # Adds an item to a stream</code></pre><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Top Redis use cases in System Design interviews</h2><p>In this section, we will explore <strong>each system</strong> that can be built <strong>on top of redis</strong> in detail along with the <strong>data structures</strong> that make this possible.</p><h3>Redis as a cache</h3><p>The most common use case of Redis is as a <strong>&#8220;cache&#8221;</strong>. It is deployed in a multi-node setup where <strong><a href="https://read.systemdesignblueprint.com/i/149605774/consistent-hashing-in-distributed-caching">Consistent Hashing</a></strong> is used to distribute keys across nodes to ensure high availability. <strong><a href="https://read.systemdesignblueprint.com/i/149605774/cache-aside">Cache Aside</a></strong> is the most commonly used <strong><a href="https://read.systemdesignblueprint.com/i/149605774/types-of-caching-strategies-for-distributed-systems">caching strategy</a></strong> where Time To Live (TTL) is often employed on each key for expiry. Redis guarantees you'll never read the value of a key after the TTL has expired and the TTL is used to decide which items to evict from the server - keeping the cache size manageable even in the case where you're trying to cache more items than memory allows.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-QPi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-QPi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 424w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 848w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 1272w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-QPi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png" width="444" height="529.9945054945055" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1738,&quot;width&quot;:1456,&quot;resizeWidth&quot;:444,&quot;bytes&quot;:225004,&quot;alt&quot;:&quot;How to use Redis for designing cache&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="How to use Redis for designing cache" title="How to use Redis for designing cache" srcset="https://substackcdn.com/image/fetch/$s_!-QPi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 424w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 848w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 1272w, https://substackcdn.com/image/fetch/$s_!-QPi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e65e961-b20b-4799-accf-9e89e1bf5db0_1473x1758.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Redis as a cache (Cache Aside pattern)</strong></figcaption></figure></div><h3>Redis as a distributed lock: Ensuring data consistency in System Design</h3><p>Another use case of Redis is as a <strong>distributed lock</strong>. Occasionally we have data in our system and we need to maintain consistency during updates, or we need to make sure multiple people aren't acting at the same time.</p><blockquote><p>Tip&#128161;: Most databases (including Redis) will offer <em>some</em> consistency guarantees. If your core database can provide consistency, <strong>don't rely</strong> on a distributed lock which may introduce extra complexity and issues. Your interviewer will likely ask you to think through the edge cases in order to make sure you really understand the concept.</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!b9Ds!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!b9Ds!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 424w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 848w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 1272w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!b9Ds!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png" width="270" height="544.4505494505495" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2936,&quot;width&quot;:1456,&quot;resizeWidth&quot;:270,&quot;bytes&quot;:351065,&quot;alt&quot;:&quot;Redis as a distributed lock: Ensuring data consistency in System Design&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis as a distributed lock: Ensuring data consistency in System Design" title="Redis as a distributed lock: Ensuring data consistency in System Design" srcset="https://substackcdn.com/image/fetch/$s_!b9Ds!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 424w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 848w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 1272w, https://substackcdn.com/image/fetch/$s_!b9Ds!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac11971b-a8e7-4ffc-becb-6c1425126390_1862x3755.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Redis as a distributed lock</strong></figcaption></figure></div><p><em><strong>Figure 2</strong></em> shows how a simple distributed lock works. When we want to try to acquire the lock, we run <code>INCR</code> with a <strong>TTL</strong>. If the response is 1 (ie: we own the lock), we proceed. If the response is &gt; 1 (ie: someone else has the lock), we wait and retry again later. When we're done with the lock, we can <code>DEL</code> the key so that other proceesses can make use of it.</p><h3>Redis as Leaderboard in Gaming applications</h3><p>Redis' <strong>sorted sets</strong> maintain ordered data which can be queried in log time which make them appropriate for <strong>leaderboard applications</strong>. The <strong>high write throughput</strong> and l<strong>ow read latency</strong> make this especially useful for scaled applications where something like a SQL DB will start to struggle.</p><p><em><strong>Figure 3</strong></em> shows how this happens:</p><ul><li><p><strong>Game Service</strong> adds players to the Redis sorted set (leaderboard) using <code>ZADD</code>, associating each player with a score and unique ID.</p></li><li><p>When a player's score changes, the application updates the sorted set with the new score using <code>ZADD</code>, which automatically updates the existing score.</p></li><li><p>The application fetches the <strong>top 10 players</strong> from the leaderboard using <code>ZRANGE leaderboard 0 9 WITHSCORES</code>, ordered by score.</p></li><li><p>The rank of a specific player is found using <code>ZREVRANK leaderboard player_123</code>.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!K7Vo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!K7Vo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 424w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 848w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 1272w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!K7Vo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png" width="1456" height="1313" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1313,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:464294,&quot;alt&quot;:&quot;Redis as Leaderboard in Gaming applications&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis as Leaderboard in Gaming applications" title="Redis as Leaderboard in Gaming applications" srcset="https://substackcdn.com/image/fetch/$s_!K7Vo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 424w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 848w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 1272w, https://substackcdn.com/image/fetch/$s_!K7Vo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f2773e3-899a-409c-8366-4d9c3d17b160_2651x2390.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Redis as Leaderboard</strong></figcaption></figure></div><h3>Redis as a Rate Limiter: Best Practices for High Traffic Systems</h3><p>As a data structure server, implementing a wide variety of rate-limiting algorithms is possible. A common algorithm is a <strong>fixed-window rate limiter</strong> where we guarantee that the number of requests does not exceed <code>N</code> over some fixed window of time <code>W</code>. <strong><a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">A crash course on Rate Limiting</a></strong> talks about different rate-limiting algorithms and their pros &amp; cons.</p><p><em><strong>Figure 4</strong></em> shows a simple implementation of a <strong>Rate Limiter</strong> in Redis. When a request comes in, we increment (<code>INCR</code>) the key for our rate limiter and check the response. If the response is greater than <code>N</code>, we wait. If it's less than <code>N</code>, we can proceed. We call <code>EXPIRE</code> on our key so that after time period <code>W</code>, the value is reset.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_JgZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_JgZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 424w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 848w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 1272w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_JgZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png" width="286" height="490.67857142857144" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2498,&quot;width&quot;:1456,&quot;resizeWidth&quot;:286,&quot;bytes&quot;:333655,&quot;alt&quot;:&quot;Redis as a Rate Limiter: Best Practices for High Traffic Systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis as a Rate Limiter: Best Practices for High Traffic Systems" title="Redis as a Rate Limiter: Best Practices for High Traffic Systems" srcset="https://substackcdn.com/image/fetch/$s_!_JgZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 424w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 848w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 1272w, https://substackcdn.com/image/fetch/$s_!_JgZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49e720a7-d69e-49c7-8812-bbaa01c968d2_1977x3392.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Figure 4: Redis as Rate Limiter</figcaption></figure></div><h3>Locating nearby drivers with Redis Geohashes in ride-hailing apps</h3><p>Redis can help <strong>locate nearby drivers</strong> in ride-hailing services like Uber. <strong>Sorted Set</strong> is the data structure that facilitates this and the algorithm involved is called <strong>Geohashing</strong> </p><ul><li><p><strong>Geohash</strong>: A 52-bit integer created by interleaving the bits of a driver's latitude and longitude coordinates.</p></li><li><p><strong>Sorted Sets</strong>: It stores the driver ID with the corresponding geohash as the <strong>score</strong>, allowing the set to be sorted based on location (see the sorted set table in <em><strong>Figure 5</strong></em>).</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7-FQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7-FQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 424w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 848w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 1272w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7-FQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png" width="1456" height="951" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:951,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:801740,&quot;alt&quot;:&quot;Locating nearby drivers with Redis Geohashes in ride-hailing apps&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Locating nearby drivers with Redis Geohashes in ride-hailing apps" title="Locating nearby drivers with Redis Geohashes in ride-hailing apps" srcset="https://substackcdn.com/image/fetch/$s_!7-FQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 424w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 848w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 1272w, https://substackcdn.com/image/fetch/$s_!7-FQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53de2799-bec0-4add-935f-ae488ea335ed_3935x2570.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Redis for finding Nearby Drivers</strong></figcaption></figure></div><p><em><strong>Figure 5</strong></em> shows a step-by-step breakdown of how this happens:</p><ol><li><p><strong>Driver Location Service:</strong></p><ul><li><p>Updates Redis with driver locations using <code>GEOADD drivers:current_location 13.3 38.1 "driver_123"</code> (sorted set name, latitude, longitude, driver ID).</p></li><li><p>A <strong>geohash</strong> is calculated from lat/long pair and is stored as a score with the <strong>driver_id</strong> in a sorted set (drivers:current_location)</p></li></ul></li><li><p><strong>Ride Request Service:</strong></p><ul><li><p>Queries Redis for nearby rides using <code>GEOSEARCH drivers:current_location FROMLONLAT 15.0 37.0 BYRADIUS 2km</code> (sorted set name, user&#8217;s lat/long, radius).</p></li><li><p>Computes a geohash/score from the user&#8217;s coordinates to find the closest drivers in the <code>drivers:current_location</code> sorted set.</p></li><li><p>Retrieves driver IDs and additional details (availability, ETA) from Redis or another data store.</p></li></ul></li></ol><blockquote><p>Tips &#128161;:</p><ol><li><p>There are additional complexities to consider in <strong>&#8220;Finding nearby drivers in Uber&#8221;</strong>, such as the <strong>status of driver</strong>s who may be available or currently completing another ride.</p></li><li><p>Depending on <strong>search traffic</strong>, it may be necessary to maintain <strong>different sorted sets</strong> for various locations.</p></li><li><p>Redis is best utilized for scenarios where latitude and longitude updates occur frequently (eg: finding nearby drivers in Uber). For use cases with <strong>infrequent location changes</strong>, like storing the locations of all outlets for a store, <strong>static data storage</strong> is more appropriate.</p></li></ol></blockquote><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h3>Redis for async communication: Message Queues</h3><p>Redis provides a <strong>List</strong> data structure that can be utilized as a <strong>queue</strong> to facilitate <strong>asynchronous communication</strong> between services. Below is a step-by-step overview of how to implement Redis for an image processing use case:</p><ol><li><p>The user uploads an image and the <strong>image service</strong> pushes a job to the Redis list <code>image_queue</code> using <code>LPUSH</code>.</p></li><li><p>The <strong>background worker</strong> retrieves the job from the queue using <code>BRPOP</code>, waiting if the queue is empty.</p></li><li><p>The worker <strong>processes</strong> the image (in this case, resizing it).</p></li><li><p>Once the image processing is complete, the worker pushes a completion notification into another Redis list, <code>processed_images</code>.</p></li><li><p>The notification service reads this message to inform the user about the completion.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FibH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FibH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 424w, https://substackcdn.com/image/fetch/$s_!FibH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 848w, https://substackcdn.com/image/fetch/$s_!FibH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 1272w, https://substackcdn.com/image/fetch/$s_!FibH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FibH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png" width="728" height="484" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:968,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:507935,&quot;alt&quot;:&quot;Redis as a message queue for async communication&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis as a message queue for async communication" title="Redis as a message queue for async communication" srcset="https://substackcdn.com/image/fetch/$s_!FibH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 424w, https://substackcdn.com/image/fetch/$s_!FibH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 848w, https://substackcdn.com/image/fetch/$s_!FibH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 1272w, https://substackcdn.com/image/fetch/$s_!FibH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb56e681-c2bb-473e-9b52-e56c4581e165_3392x2256.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Redis as a message queue</strong></figcaption></figure></div><h4>Downsides of using Redis as a message queue</h4><p>While Redis can handle this use case, it's <strong>not</strong> ideally suited for <strong>large-scale systems</strong> where solutions like <strong>Kafka</strong> are <strong>more</strong> <strong>appropriate</strong> due to the following limitations of Redis:</p><ol><li><p><strong>Lack of Message Durability:</strong> As an in-memory store, Redis can lose messages if it crashes or restarts. Although it supports persistence through AOF (Append Only Files) and RDB (Redis Database), there are chances of messages getting lost between snapshots.</p></li><li><p><strong>No Message Re-delivery Guarantee:</strong> Unlike Kafka, Redis <strong>lacks</strong> a <strong>message commit mechanism</strong>, meaning there's no assurance that lost messages will be retried.</p></li><li><p><strong>Not Designed for Scale:</strong> Being <strong>single-threaded</strong>, Redis can become a bottleneck in high-throughput scenarios, whereas distributed message brokers like Kafka or RabbitMQ are built for scalability.</p></li><li><p><strong>No Message Ordering Guarantees:</strong> Redis does not guarantee the order of messages when multiple consumers are involved.</p></li></ol><h3>Storing and analyzing time series data in Redis</h3><p><strong>Time series data</strong> is a sequence of data points collected or recorded at successive time intervals, typically in chronological order. Each data point is associated with a timestamp, which represents when the data was captured. Common examples of time-series data:</p><ol><li><p><strong>Stock Prices</strong>: Daily closing prices of a stock over time</p></li><li><p><strong>Weather Data</strong>: Temperature readings recorded every hour or day</p></li><li><p><strong>Server Metrics</strong>: CPU utilization of a server over time</p></li><li><p><strong>IoT Sensor Data</strong>: Sensor readings from devices over time</p></li><li><p><strong>Financial Transactions</strong>: Sequence of financial transactions like payments made over time, categorized by timestamp.</p></li></ol><p>Redis provides a NoSQL data structure called <strong><a href="https://redis.io/timeseries">RedisTimeSeries</a></strong> which allows <strong>storing</strong> and <strong>querying</strong> time series data.</p><p><em><strong>Figure 7</strong></em> shows the steps involved:</p><ol><li><p>The client sends data to the <strong>Analytics Service</strong> with a timestamp and the number of visits (can also be aggregated by the client and sent at once). </p></li><li><p>The Analytics Service adds this information to <strong>Redis</strong> using the <code>TS.ADD</code> command, which stores the number of visits with the given timestamp in Redis time series DB</p></li><li><p><code>TS.RANGE</code> command can be used to retrieve time series data between two timestamps/in the last hour.</p></li><li><p><code>TS.MRANGE</code> command can be used with an aggregation option (<code>AGGREGATION count 60</code>) to answer queries such as <strong>&#8220;how many visits occurred per minute&#8221;</strong>.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2qo4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2qo4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 424w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 848w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 1272w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2qo4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png" width="1456" height="1160" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:628050,&quot;alt&quot;:&quot;Storing and analyzing time series data in Redis&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Storing and analyzing time series data in Redis" title="Storing and analyzing time series data in Redis" srcset="https://substackcdn.com/image/fetch/$s_!2qo4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 424w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 848w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 1272w, https://substackcdn.com/image/fetch/$s_!2qo4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F10b8df0c-ae18-4f3d-9d05-ac0d903b15b0_3358x2675.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Fgure 7: Redis for Time Series data</strong></figcaption></figure></div><h3>Managing user sessions with Redis</h3><p>Another popular use case for Redis is <strong>managing user sessions</strong>. Its built-in support for time-to-live (TTL) makes it easy to automatically expire sessions after a designated interval.</p><p><em><strong>Figure 8</strong></em> shows the steps involved:</p><ol><li><p>When a user logs in, the <strong>Login Service</strong> stores the <strong>session</strong> data for each user (<code>456_session_token</code>: <code>{user_id: 456, logged_in: true}</code>) in Redis using the <code>HSET</code> command.</p></li><li><p>A TTL (eg: 1 hr) is set for every token <code>EXPIRE 456_session_token 3600</code>.</p></li><li><p>During each API request, the login service retrieves the session data by using the <code>HGETALL</code> command on the session token (<code>456_session_token</code>).</p></li><li><p>Session expires after 1hr and the user is prompted to login again.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!V24m!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!V24m!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 424w, https://substackcdn.com/image/fetch/$s_!V24m!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 848w, https://substackcdn.com/image/fetch/$s_!V24m!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 1272w, https://substackcdn.com/image/fetch/$s_!V24m!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!V24m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png" width="1456" height="887" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:887,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:381760,&quot;alt&quot;:&quot;Managing user sessions with Redis&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Managing user sessions with Redis" title="Managing user sessions with Redis" srcset="https://substackcdn.com/image/fetch/$s_!V24m!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 424w, https://substackcdn.com/image/fetch/$s_!V24m!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 848w, https://substackcdn.com/image/fetch/$s_!V24m!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 1272w, https://substackcdn.com/image/fetch/$s_!V24m!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F75c709e8-7573-4906-b319-14754039cbc0_3093x1884.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Redis for storing User Sessions</strong></figcaption></figure></div><h3>Redis for top posts in Social Media: Real-Time ranking and engagement optimization</h3><p>Designing <strong>social media feeds</strong> or displaying the <strong>top posts</strong> related to a specific topic is another prevalent use case for Redis. By utilizing <strong>sorted sets</strong> (<code>ZSET</code>), Redis effectively stores <strong>posts</strong> as <strong>members</strong> and uses the <strong>number of likes</strong> as <strong>scores</strong> to rank them. As posts receive likes, their scores are updated in real time, maintaining accurate rankings.</p><p><em><strong>Figure 9</strong></em> shows the steps involved:</p><ol><li><p>Whenever a new post is created, <strong>relevant keywords</strong> are extracted and stored in a Redis <strong>sorted set</strong>. For instance, a post like &#8220;Google launched Gemini&#8230;&#8221; would use the command <code>ZADD keyword:google 100 post_id_12345</code> to store its likes.</p></li><li><p>The score of a post can be based on likes, impressions, or other metrics.</p></li><li><p>When a user searches for a keyword (e.g., "google"), the <strong>top posts</strong> are retrieved using <code>ZREVRANGE keyword:google 0 9 WITHSCORES</code>, which returns the top 10 posts sorted by likes.</p></li><li><p>When a post receives a like, its <strong>score</strong> is <strong>updated</strong> to reshuffle rankings with <code>ZINCRBY keyword:google 1 post_id_12345</code>, increasing the likes for that post to 101</p></li><li><p>To <strong>manage space</strong>, less popular posts are periodically removed. For example, <code>ZREMRANGEBYRANK keyword:google 100 -1</code> removes all posts ranked below the 100th position, and <code>ZREMRANGEBYSCORE keyword:tiger -inf 9</code> deletes posts with scores below 10.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XFZf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XFZf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 424w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 848w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 1272w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XFZf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png" width="728" height="411" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:822,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:790714,&quot;alt&quot;:&quot;Redis for top posts in Social Media: Real-Time ranking and engagement optimization&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis for top posts in Social Media: Real-Time ranking and engagement optimization" title="Redis for top posts in Social Media: Real-Time ranking and engagement optimization" srcset="https://substackcdn.com/image/fetch/$s_!XFZf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 424w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 848w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 1272w, https://substackcdn.com/image/fetch/$s_!XFZf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8cd33fe3-97ef-48f6-96cb-e544a5f8a809_4571x2581.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Redis for Top posts on social media</strong></figcaption></figure></div><div><hr></div><h2>Other use cases of Redis:</h2><p>In addition to the use cases already covered, Redis can also be utilized to develop the following systems:</p><ol><li><p>Real-time messaging using Pub-Sub (<em><strong>Figure 10</strong></em>)</p></li><li><p>Approximate the cardinality of a large set using HyperLogLog</p></li><li><p>Full-text search using the RedisSearch module</p></li><li><p>Storing nested JSON without re-serialization costs using RedisJSON</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EA1Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EA1Z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 424w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 848w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 1272w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EA1Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png" width="612" height="457.31868131868134" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1088,&quot;width&quot;:1456,&quot;resizeWidth&quot;:612,&quot;bytes&quot;:463272,&quot;alt&quot;:&quot;Redis for pub-sub&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Redis for pub-sub" title="Redis for pub-sub" srcset="https://substackcdn.com/image/fetch/$s_!EA1Z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 424w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 848w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 1272w, https://substackcdn.com/image/fetch/$s_!EA1Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0bc39aaa-1831-43e5-bd30-e6d30e7f1667_3220x2407.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Redis as Pub/Sub</strong></figcaption></figure></div><div><hr></div><h2>Conclusion</h2><p>Redis is a <strong>powerful</strong> and <strong>versatile</strong> tool for system design interviews. Its straightforward data structures make it easy to reason about scaling implications, enabling you to engage in in-depth discussions with your interviewer without requiring extensive knowledge of Redis internals.</p><p>Apart from Redis, understanding other<strong> key technologies</strong> such as <strong>Kafka, Elasticsearch, Cassandra</strong> will give you an edge during deep dives in a system design interview. Will write separate posts on them soon &#128521;.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Thanks for reading!</p><p><strong>Liked</strong> this article? Make sure to &#128153; click the like button.</p><p><strong>Feedback</strong> or addition? Make sure to &#128172; <strong>comment</strong>.</p><p>Know someone who would find this helpful? Make sure to &#128257; <strong>share</strong> this post.</p>]]></content:encoded></item><item><title><![CDATA[A crash course on caching: Caching challenges in distributed systems (Part 3/3)]]></title><description><![CDATA[Learn to solve common caching issues like Thundering Herd, Cache Penetration, and Hot Keys with effective solutions for optimized performance]]></description><link>https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 09 Oct 2024 04:45:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is the <strong>final part</strong> of the three-part series on Caching. Today, we will focus on&nbsp;<strong>common caching challenges </strong>and <strong>how to mitigate </strong>them.</p><p>Before we begin, here&#8217;s a <strong>quick recap</strong> of what we covered in the previous parts:</p><ul><li><p><strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Caching Part 1</a>: </strong>What is caching, How it happens at the hardware level, Where should caching be implemented?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/distributed-caching-algorithms-and-strategies">Caching Part 2</a>: </strong>Distributed Caches, Types of Caching Strategies, Types of Cache Eviction Strategies, Comparison between popular Distributed caches</p></li></ul><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">How to use Redis in system design interviews</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">How to crack any system design interview</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/introduction-to-kafka">What is Kafka</a>?</strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide">How to use Kafka in system design interviews</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/ep-1-how-to-design-a-payment-system">How to design a payments system</a>?</strong></p></li></ol><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Thundering Herd Problem in Distributed Caching</h1><h2>Understanding the Thundering Herd problem</h2><p>In a <strong>high-traffic</strong> environment, it&#8217;s common for one or more resources&#8212;like databases, cache nodes, or application servers&#8212;to become overwhelmed by an influx of requests. When this happens, traffic may unintentionally be redirected to a single instance, leading to cascading failures and resulting in service unavailability. This scenario, characterized by a sudden spike in incoming traffic, is known as the <strong>Thundering Herd Problem</strong>.</p><p>The Thundering Herd Problem can be seen in <strong>caching</strong> in two ways:</p><h3>Cache Stampede Issue</h3><p><strong>Cache stampede</strong> is a situation that occurs when <strong>a popular cached key</strong> expires, causing multiple clients to request the same data simultaneously. This surge in requests <strong>overwhelms</strong> the <strong>underlying data store</strong>, as each client attempts to fetch or recompute the missing data at once. As a result, the sudden influx of traffic can lead to significant performance degradation and service unavailability.</p><p><em><strong>Figure 2</strong></em> shows the situation which leads to a cache stampede situation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!agGU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!agGU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 424w, https://substackcdn.com/image/fetch/$s_!agGU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 848w, https://substackcdn.com/image/fetch/$s_!agGU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 1272w, https://substackcdn.com/image/fetch/$s_!agGU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!agGU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png" width="334" height="346.0515463917526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:804,&quot;width&quot;:776,&quot;resizeWidth&quot;:334,&quot;bytes&quot;:155353,&quot;alt&quot;:&quot;Thundering herd in Distributed caching: Cache stampede issue&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Thundering herd in Distributed caching: Cache stampede issue" title="Thundering herd in Distributed caching: Cache stampede issue" srcset="https://substackcdn.com/image/fetch/$s_!agGU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 424w, https://substackcdn.com/image/fetch/$s_!agGU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 848w, https://substackcdn.com/image/fetch/$s_!agGU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 1272w, https://substackcdn.com/image/fetch/$s_!agGU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9bbab546-ed90-4b74-b8ab-ff530e4ab730_776x804.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Cache stampede</strong></figcaption></figure></div><blockquote><p><em>Example</em>: E-commerce platforms during flash sales face cache stampede when the product availability cache expires, leading to a flood of DB queries as users check out at the same time.</p></blockquote><p></p><h3>Cache Avalance in Distributed Systems</h3><p>Cache avalanche occurs when <strong>multiple cached keys</strong> in a <strong>single node</strong> expire simultaneously, or when a <strong>cache node goes down</strong>, leading to an overwhelming number of requests directed to the underlying data store or other cache nodes. </p><p>Unlike a <strong>cache stampede</strong>, which is triggered by the expiration of a single key, a cache avalanche involves the <strong>mass expiration</strong> of many keys at once. This scenario can severely strain backend DBs and result in performance issues, as they struggle to handle the sudden surge in requests.</p><p><em><strong>Figure 3</strong></em> shows one of the situations which leads to a cache avalanche situation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ubdd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ubdd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 424w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 848w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 1272w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ubdd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png" width="334" height="361.8333333333333" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:832,&quot;width&quot;:768,&quot;resizeWidth&quot;:334,&quot;bytes&quot;:157800,&quot;alt&quot;:&quot;Thundering herd in Distributed caching: Cache avalanche issue&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Thundering herd in Distributed caching: Cache avalanche issue" title="Thundering herd in Distributed caching: Cache avalanche issue" srcset="https://substackcdn.com/image/fetch/$s_!ubdd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 424w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 848w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 1272w, https://substackcdn.com/image/fetch/$s_!ubdd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9dfc676-59b3-44e5-beea-a05f5c1a2ab6_768x832.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Cache Avalanche</strong></figcaption></figure></div><h2>Solutions to Thundering Herd</h2><p>There are several strategies to address the thundering herd problem. Let&#8217;s explore each one of these in detail</p><h3>Using Consistent Hashing</h3><p>As discussed in the previous article on <strong><a href="https://read.systemdesignblueprint.com/i/149605774/consistent-hashing-in-distributed-caching">Consistent Hashing</a></strong>, distributing keys across multiple nodes, combined with replication, is crucial for maintaining high data availability in the event of node failures. This approach helps mitigate the effects of cache avalanches and cache stampedes by balancing the load among remaining servers, thereby reducing the risk of a single point of failure.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uYOZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uYOZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 424w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 848w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 1272w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uYOZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png" width="330" height="339.6116504854369" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:848,&quot;width&quot;:824,&quot;resizeWidth&quot;:330,&quot;bytes&quot;:101887,&quot;alt&quot;:&quot;Thundering herd solution using consistent hashing &quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Thundering herd solution using consistent hashing " title="Thundering herd solution using consistent hashing " srcset="https://substackcdn.com/image/fetch/$s_!uYOZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 424w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 848w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 1272w, https://substackcdn.com/image/fetch/$s_!uYOZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b099982-2770-4e1e-b84e-9a986636f93a_824x848.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Consistent hashing distributes keys across nodes to avoid SPOF</strong></figcaption></figure></div><p></p><h3>Implementing Key Locking/Leasing</h3><p>This approach is similar to mutexes in operating systems, where only one process can access a shared resource at a time. In caching, when multiple clients try to access an expired key, the first request locks the cache to update the key, while subsequent requests wait for the cache to refresh. This helps minimize the number of requests to the backend data source.</p><p><em><strong>Figure 5</strong></em> shows how this works:</p><ol><li><p><strong>Key Expiration</strong>: When a popular key (k1) expires, the first application server (A1) querying it experiences a cache miss.</p></li><li><p><strong>Lock Acquisition</strong>: A1 acquires a lock on the cache for k1, preventing other servers from accessing it until the update is complete.</p></li><li><p><strong>Request Queuing</strong>: Other servers trying to access k1 while it's locked must wait in a queue.</p></li><li><p><strong>Data Retrieval</strong>: A1 retrieves the latest data for k1 from the DB and updates the cache.</p></li><li><p><strong>Release of Lock</strong>: After updating, A1 releases the lock, allowing queued servers to access the updated value.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Kc3p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Kc3p!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 424w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 848w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 1272w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Kc3p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png" width="526" height="329.62666666666667" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11528562-90f0-402c-a232-b600251b2d5c_900x564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:564,&quot;width&quot;:900,&quot;resizeWidth&quot;:526,&quot;bytes&quot;:177699,&quot;alt&quot;:&quot;Thundering herd solution using key locking/leasing&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Thundering herd solution using key locking/leasing" title="Thundering herd solution using key locking/leasing" srcset="https://substackcdn.com/image/fetch/$s_!Kc3p!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 424w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 848w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 1272w, https://substackcdn.com/image/fetch/$s_!Kc3p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11528562-90f0-402c-a232-b600251b2d5c_900x564.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Locking expired key to avoid overwhelming the data store</strong></figcaption></figure></div><p></p><h3>Applying Random Expiration Policies</h3><p>To prevent <strong>cache avalanche</strong>, implement <strong>staggered expiration</strong> by assigning a <strong>random time-to-live (TTL)</strong> to each cached key. </p><p>This technique effectively spreads out the expiration times of cached entries, <strong>minimizing the risk</strong> of multiple items <strong>expiring simultaneously</strong>. By introducing randomness in the TTL, you reduce the chances of overwhelming the backend system with a flood of requests when a cache reset occurs.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!srl7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!srl7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 424w, https://substackcdn.com/image/fetch/$s_!srl7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 848w, https://substackcdn.com/image/fetch/$s_!srl7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 1272w, https://substackcdn.com/image/fetch/$s_!srl7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!srl7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png" width="252" height="190.53658536585365" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:372,&quot;width&quot;:492,&quot;resizeWidth&quot;:252,&quot;bytes&quot;:46898,&quot;alt&quot;:&quot;Thundering herd solution by applying random expiration policies&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Thundering herd solution by applying random expiration policies" title="Thundering herd solution by applying random expiration policies" srcset="https://substackcdn.com/image/fetch/$s_!srl7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 424w, https://substackcdn.com/image/fetch/$s_!srl7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 848w, https://substackcdn.com/image/fetch/$s_!srl7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 1272w, https://substackcdn.com/image/fetch/$s_!srl7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F018b6f18-16bb-40a6-b1c9-5bb0965eb1e8_492x372.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Assign random TTLs to cached keys</strong></figcaption></figure></div><p></p><h3>Using Circuit-Breaking Techniques</h3><p>Circuit breaking stops further requests to the database until it is back online and able to handle traffic again.</p><p></p><h3><strong>Request Rate Limiting and Load Shedding</strong></h3><p>Request rate limiting and load shedding help prevent cache stampede by managing how many requests are processed. These techniques can be applied per user, client, or system-wide to maintain stability during peak loads.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Cache Penetration in Distributed Systems</h1><h2>Overview of the Cache Penetration Problem</h2><p><strong>Cache penetration</strong> occurs when clients, often with malicious intent, send requests for keys that <strong>do not exist</strong> in <strong>both</strong> the <strong>cache</strong> and the underlying <strong>database</strong>. <em><strong>Figure 7</strong></em> shows an example scenario:</p><ol><li><p><strong>Requesting Non-existent Keys</strong>: Clients attempt to access a non-existent key("k1") which is absent in the cache.</p></li><li><p><strong>Cache Miss and Database Query</strong>: Since the cache does not contain the key, the system queries the underlying DB for that key.</p></li><li><p><strong>Null Response from Database</strong>: The database also lacks this key, resulting in a null response.</p></li><li><p><strong>Overwhelming the Database</strong>: The malicious clients repeatedly send requests for the non-existent key, leading to a flood of queries that overwhelm the database and can degrade its performance.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Xerd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xerd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 424w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 848w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 1272w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xerd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png" width="346" height="346" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:768,&quot;width&quot;:768,&quot;resizeWidth&quot;:346,&quot;bytes&quot;:151817,&quot;alt&quot;:&quot;What is cache penetration problem&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="What is cache penetration problem" title="What is cache penetration problem" srcset="https://substackcdn.com/image/fetch/$s_!Xerd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 424w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 848w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 1272w, https://substackcdn.com/image/fetch/$s_!Xerd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F072a9a32-86ca-41ca-b9d5-bcdddd45dcc0_768x768.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Cache Penetration</strong></figcaption></figure></div><blockquote><p><em>Example</em>: A news website might face this when many bots query outdated or non-existent articles, resulting in high backend load since the cache never stores these responses.</p></blockquote><p></p><h2>Solutions for Cache Penetration</h2><p>There are two ways to mitigate cache penetration. Let&#8217;s look at each one of these.</p><h3>Storing &#8220;null&#8220; in cache to reduce load</h3><p>Store a <strong>placeholder value</strong> like "null" in the cache for non-existent keys (<em><strong>Figure 8</strong></em>). This prevents unnecessary hits to the database by serving future requests directly from the cache. Set a TTL (Time to Live) for these entries to free up cache space after a reasonable period.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oj36!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oj36!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 424w, https://substackcdn.com/image/fetch/$s_!oj36!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 848w, https://substackcdn.com/image/fetch/$s_!oj36!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 1272w, https://substackcdn.com/image/fetch/$s_!oj36!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oj36!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png" width="416" height="262.49446494464945" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:684,&quot;width&quot;:1084,&quot;resizeWidth&quot;:416,&quot;bytes&quot;:167161,&quot;alt&quot;:&quot;Solve cache penetration by storing null in cache&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solve cache penetration by storing null in cache" title="Solve cache penetration by storing null in cache" srcset="https://substackcdn.com/image/fetch/$s_!oj36!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 424w, https://substackcdn.com/image/fetch/$s_!oj36!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 848w, https://substackcdn.com/image/fetch/$s_!oj36!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 1272w, https://substackcdn.com/image/fetch/$s_!oj36!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb9eebf-797f-438d-b8fe-5a04d71bce73_1084x684.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Store null against non-existent key &#8220;k1&#8220; in the cache to avoid DB calls</strong></figcaption></figure></div><h3>Using Bloom Filters to Prevent Misses</h3><p><strong><a href="https://read.systemdesignblueprint.com/i/150503362/bloom-filters">Bloom filters</a></strong> are probabilistic data structures used to quickly check whether <strong>a key is absent</strong> before querying the cache or database. If the filter suggests that a key isn&#8217;t present, it <strong>avoids</strong> hitting the <strong>cache</strong> or <strong>database</strong>, reducing unnecessary load, all while operating in <strong>constant time</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qNxF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qNxF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 424w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 848w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 1272w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qNxF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png" width="556" height="354.48034934497815" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:584,&quot;width&quot;:916,&quot;resizeWidth&quot;:556,&quot;bytes&quot;:151235,&quot;alt&quot;:&quot;Use Bloom filters to solve cache penetration problem&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Use Bloom filters to solve cache penetration problem" title="Use Bloom filters to solve cache penetration problem" srcset="https://substackcdn.com/image/fetch/$s_!qNxF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 424w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 848w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 1272w, https://substackcdn.com/image/fetch/$s_!qNxF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20797703-8c8a-4b7f-a115-877f26d931b7_916x584.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Bloom Filter to check if a key is present</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>That's it for our overview of Bloom Filters for now. I'll explore the topic in more detail in a <strong>future deep div</strong>e.</p><p>Now, let&#8217;s return to the challenges associated with distributed caches. &#128071;</p><div><hr></div><h1>Hot Key Problem in Caching</h1><h2>What is the hot  issue?</h2><p>In a distributed cache system, keys are distributed across different nodes based on the sharding strategy. However, frequently accessed keys may cluster on the same node, leading to congestion. This phenomenon is known as the <strong>hot key problem</strong>. For instance, keys <strong>"k5"</strong> and <strong>"k6"</strong> located in node 2, can potentially <strong>overwhelm that node</strong> due to high traffic.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Darw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Darw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 424w, https://substackcdn.com/image/fetch/$s_!Darw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 848w, https://substackcdn.com/image/fetch/$s_!Darw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 1272w, https://substackcdn.com/image/fetch/$s_!Darw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Darw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png" width="344" height="299.6701030927835" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:676,&quot;width&quot;:776,&quot;resizeWidth&quot;:344,&quot;bytes&quot;:94211,&quot;alt&quot;:&quot;What is the hot key problem in distributed caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="What is the hot key problem in distributed caching" title="What is the hot key problem in distributed caching" srcset="https://substackcdn.com/image/fetch/$s_!Darw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 424w, https://substackcdn.com/image/fetch/$s_!Darw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 848w, https://substackcdn.com/image/fetch/$s_!Darw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 1272w, https://substackcdn.com/image/fetch/$s_!Darw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca8f7353-ecb6-4dd9-a89f-2e468cf44252_776x676.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 15: Hot Key Problem</strong></figcaption></figure></div><blockquote><p><em>Example</em>: If a specific video goes viral on YouTube, the cache for the video metadata might become overloaded as millions of users access the same data, causing delays in responses.</p></blockquote><p></p><h2>Solutions for Hot Key Problem</h2><h3>Replication for better Load Management</h3><p>To effectively manage hot keys, it&#8217;s important to <strong>increase</strong> their <strong>replication factor</strong> beyond the standard approach used in a distributed cache cluster. </p><p>For instance in Figure 16, if <em>k5</em> and <em>k6</em> are identified as <strong>hot keys</strong>, create <strong>N replicas</strong> (<em>k5-1, k5-2, &#8230;, k5-N</em>) and distribute these replicas across various nodes. This way, when a read request for one of these keys is made, it can be served from any of the replicas at random, allowing for even traffic distribution. </p><p>This strategy helps prevent any single node from becoming overloaded, ensuring smoother performance for high-demand keys.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LwyI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LwyI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 424w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 848w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 1272w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LwyI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png" width="386" height="270.979797979798" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:556,&quot;width&quot;:792,&quot;resizeWidth&quot;:386,&quot;bytes&quot;:102620,&quot;alt&quot;:&quot;Solve hot key problem in distributed caches by breaking and replicating the keys on different nodes&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solve hot key problem in distributed caches by breaking and replicating the keys on different nodes" title="Solve hot key problem in distributed caches by breaking and replicating the keys on different nodes" srcset="https://substackcdn.com/image/fetch/$s_!LwyI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 424w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 848w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 1272w, https://substackcdn.com/image/fetch/$s_!LwyI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F48d7c6fa-9383-4e0a-806d-f0ab8aeb6492_792x556.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 16: Split and distribute hot keys across nodes</strong></figcaption></figure></div><p></p><h3>Applying Rate Limiting for high-traffic keys</h3><p>To manage <strong>excessive requests</strong> for a key, you can implement rate limiting by placing additional requests in a wait queue. These queued requests will be processed only after the current ones have been completed successfully.</p><p>For a more in-depth understanding of <strong>rate limiting</strong>, I&#8217;ve written separate articles, Rate Limiting<strong> <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a></strong> and <strong><a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong>, which include detailed explanations and cheat sheets. I encourage you to check them out and share your feedback &#128521;!</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Large key problem in Caching</h1><h2>What is the Large Key problem?</h2><p>The <strong>large key problem</strong> occurs when a cache is used to store oversized data, such as large JSON responses, images, or big data blobs. <em>Figure 17</em> shows a large key like <em>"k2"</em> with a 2MB payload can cause several issues:</p><ul><li><p><strong>Network Bandwidth</strong>: Frequent access to large keys consumes more bandwidth, slowing down lookup times.</p></li><li><p><strong>Update Overhead</strong>: Partial updates to large keys force the entire value to be rewritten, impacting performance.</p></li><li><p><strong>Cache Eviction</strong>: If evicted or invalidated, reloading large keys from storage is slow, further degrading query performance.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2d-6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2d-6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 424w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 848w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 1272w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2d-6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png" width="348" height="221.24503311258277" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:384,&quot;width&quot;:604,&quot;resizeWidth&quot;:348,&quot;bytes&quot;:48448,&quot;alt&quot;:&quot;What is the large key problem in distributed caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="What is the large key problem in distributed caching" title="What is the large key problem in distributed caching" srcset="https://substackcdn.com/image/fetch/$s_!2d-6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 424w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 848w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 1272w, https://substackcdn.com/image/fetch/$s_!2d-6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb88328e-e74b-4876-9ce8-009e39d38cd9_604x384.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 17: Large key problem</strong></figcaption></figure></div><p></p><h2>Solutions for Large Keys</h2><h3>Using Compression to Optimize Storage</h3><p>One effective approach to managing large values in caching is <strong>compressing</strong> <strong>them before storage</strong>. Compression reduces the memory used by cached data, making it easier to store larger values without consuming excessive cache space.</p><p>When a value exceeds a specified size, the system compresses it (using algorithms like <strong>GZIP, LZ4, or Snappy</strong>) before caching it. On retrieval, the data is decompressed for use.</p><p>However, compression adds latency to both read and write operations and can slow down debugging, as values must be decompressed before inspection.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qv88!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qv88!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 424w, https://substackcdn.com/image/fetch/$s_!qv88!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 848w, https://substackcdn.com/image/fetch/$s_!qv88!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 1272w, https://substackcdn.com/image/fetch/$s_!qv88!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qv88!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png" width="208" height="193.14285714285714" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:260,&quot;width&quot;:280,&quot;resizeWidth&quot;:208,&quot;bytes&quot;:21849,&quot;alt&quot;:&quot;Solving Large key problem in distributed caching by compressing the payload&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solving Large key problem in distributed caching by compressing the payload" title="Solving Large key problem in distributed caching by compressing the payload" srcset="https://substackcdn.com/image/fetch/$s_!qv88!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 424w, https://substackcdn.com/image/fetch/$s_!qv88!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 848w, https://substackcdn.com/image/fetch/$s_!qv88!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 1272w, https://substackcdn.com/image/fetch/$s_!qv88!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5d86a0e5-b2be-48b3-b77a-4e735b908f16_280x260.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 18: Compress large values before caching</strong></figcaption></figure></div><p></p><h3>Chunking Large Keys for Better Management</h3><p>For handling large data structures like JSON, lists, or hashmaps in the cache, one effective strategy is to <strong>split the data into smaller pieces</strong> and store them under separate keys.</p><p><em><strong>Figure 19</strong></em> shows how <strong>user</strong> <strong>video metadata</strong> (like video views and resume timestamp) is cached. </p><p>Instead of storing everything in one large object (eg: <code>user_id -&gt; video_metadata</code>), the key can be split into smaller parts. For instance, <code>user_1_v1_views: 10</code> and <code>user_1_v1_resume_at: 10:20</code> to <strong>store views</strong> and <strong>resume timestamps</strong> for video <code>v1</code> in separate keys, reducing the size and complexity of individual entries.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!xiSv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!xiSv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 424w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 848w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 1272w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!xiSv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png" width="468" height="199.0939226519337" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:308,&quot;width&quot;:724,&quot;resizeWidth&quot;:468,&quot;bytes&quot;:52710,&quot;alt&quot;:&quot;Solving large key problem by chunking nested keys&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solving large key problem by chunking nested keys" title="Solving large key problem by chunking nested keys" srcset="https://substackcdn.com/image/fetch/$s_!xiSv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 424w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 848w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 1272w, https://substackcdn.com/image/fetch/$s_!xiSv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc6aeac14-7b5c-430b-b31f-c8018ea897ad_724x308.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 19: Split complex data blobs and store them under simpler keys</strong></figcaption></figure></div><p></p><h3>Hybrid Caching Approaches for Large Keys</h3><p>In some cases, using a <strong>hybrid caching strategy</strong> with <strong>multiple cache layers</strong> can be efficient. <strong>Smaller</strong>, <strong>frequently accessed items</strong> can be stored in <strong>fast in-memory caches</strong> like Redis or Memcached, while <strong>larger, less frequently accessed data</strong> can be kept in disk-based caches, databases, or object storage systems.</p><p>For example in <em><strong>Figure 20</strong></em>, instead of storing large objects directly in the cache, the cache holds a reference (like an Amazon S3 URL) pointing to the data stored elsewhere. When large data is needed, the application retrieves it from the external storage using the cached reference.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SbMZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SbMZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 424w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 848w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 1272w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SbMZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png" width="234" height="178.7244094488189" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:388,&quot;width&quot;:508,&quot;resizeWidth&quot;:234,&quot;bytes&quot;:34772,&quot;alt&quot;:&quot;Solving large key problem by using hybrid caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solving large key problem by using hybrid caching" title="Solving large key problem by using hybrid caching" srcset="https://substackcdn.com/image/fetch/$s_!SbMZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 424w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 848w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 1272w, https://substackcdn.com/image/fetch/$s_!SbMZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2dffbb1f-ac78-4aa2-9faf-bfeb0bbfc01d_508x388.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 20: Hybrid caching</strong></figcaption></figure></div><p></p><h3>Using Proxy Clusters for Scalability</h3><p>A <strong>dedicated multi-node storage cluster</strong> can sit between app servers and the distributed cache cluster to handle large keys. While slower than in-memory caches, this layer, using <strong>NVMe</strong> or <strong>SSD</strong> drives, offers a faster alternative to disk-based storage, maintaining the benefits of caching for large keys.</p><p>Due to the added complexity of managing another storage layer, this approach should be considered an outlier. It is worth exploring only when there are many large keys and simpler solutions, like compression or chunking, aren&#8217;t effective.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fZdL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fZdL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 424w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 848w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 1272w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fZdL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png" width="326" height="253.17021276595744" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:584,&quot;width&quot;:752,&quot;resizeWidth&quot;:326,&quot;bytes&quot;:93484,&quot;alt&quot;:&quot;Solving large key problem by using proxy clusters&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Solving large key problem by using proxy clusters" title="Solving large key problem by using proxy clusters" srcset="https://substackcdn.com/image/fetch/$s_!fZdL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 424w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 848w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 1272w, https://substackcdn.com/image/fetch/$s_!fZdL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9f248e84-1ed4-47b8-9582-e396474c5aa0_752x584.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 21: Proxy Cluster for serving large keys</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>Memory Fragmentation in Caching</h1><h2>What is the memory fragmentation problem?</h2><p><strong>Fragmentation</strong> occurs when free memory becomes scattered in small, non-contiguous blocks throughout the system. This happens over time as data is repeatedly <strong>allocated</strong> and <strong>deallocated</strong> in the cache, making it inefficient since these fragmented blocks may be <strong>too small</strong> to hold <strong>new larger cache entries</strong>.</p><p>This issue is more common in in-memory caches like Redis or Memcached, especially when key-value sizes vary widely or memory reclamation isn&#8217;t consistent. As memory becomes fragmented, it can lead to reduced cache performance and difficulty in handling larger items.</p><p><strong>Example </strong>(<em><strong>Figure 22</strong></em>)<strong>:</strong></p><p>Imagine a Redis cache system with a memory capacity of 1 GB. Initially, <strong>small keys</strong> of size &lt; 5 KB each are stored in the cache. When a <strong>large key</strong> (eg: 10KB) is added, it won&#8217;t fit in smaller spaces and will start creating small gaps in the system. Even though the cache might have 500 MB of free memory available, it might only be in small chunks like 2 KB, 4 KB, and so on. These fragmented blocks cannot be used to store the larger entries, and eventually, the system runs into <strong>out-of-memory</strong> errors even though, theoretically, there is still enough memory available.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3fe0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3fe0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 424w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 848w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3fe0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png" width="328" height="510.41711229946526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1164,&quot;width&quot;:748,&quot;resizeWidth&quot;:328,&quot;bytes&quot;:252635,&quot;alt&quot;:&quot;What is memory fragmentation in distributed systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="What is memory fragmentation in distributed systems" title="What is memory fragmentation in distributed systems" srcset="https://substackcdn.com/image/fetch/$s_!3fe0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 424w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 848w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!3fe0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6ba01489-fb28-4056-a96e-291f4b915dea_748x1164.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 22: Frequent allocation &amp; deallocation causes memory fragmentation</strong></figcaption></figure></div><h2>Solutions to Mitigate Memory Fragmentation</h2><p>Most of the popular distributed caches already have inbuilt mechanisms to deal with memory fragmentation issues. Let&#8217;s look at how <strong>Redis</strong> and <strong>Memcached</strong> solve this:</p><h3>Applying Defragmentation techniques</h3><p>The <strong>Defragmentation/Garbage Collection</strong> process compacts memory blocks by relocating data to create contiguous blocks of free space. This can reduce fragmentation and make better use of available memory. <strong>Redis</strong>, for example, triggers memory defragmentation when it detects high levels of fragmentation, which can be configured through Redis commands like <code>MEMORY PURGE</code></p><h3>Using Slab Allocation for Efficient Memory Management</h3><p>Instead of dynamically allocating memory for each new cache entry, <strong>Memcached</strong> uses <strong>predefined memory chunks (slabs)</strong> of fixed sizes to store objects. Objects are grouped by size, and each size group is allocated memory from a corresponding slab. For example, objects of size 16 KB might all be stored in 16 KB slabs, while larger objects are stored in their appropriate size slabs. This prevents small objects from leaving gaps between larger ones, thus reducing fragmentation.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>This brings us to the <strong>end</strong> of this <strong>series</strong> on <strong>caching</strong>! This was the <strong>longest</strong> and the <strong>most important</strong> part because it shares a lot of <strong>practical challenges</strong> and <strong>mitigation strategies</strong> while dealing with caching systems at scale.</p><p>Congrats if you made it here &#127881;! Just like previous posts, sharing a <strong>consolidated cheat sheet on caching problems &amp; solutions</strong> &#129527;.</p><p><strong>PS</strong>: <em><strong>Cheatsheets </strong></em>of <strong>previous parts</strong> of the <strong>series</strong> &#8212; <strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/distributed-caching-algorithms-and-strategies">Part 2</a>.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0lM7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0lM7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 424w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 848w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 1272w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0lM7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png" width="728" height="1256.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2513,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:1676007,&quot;alt&quot;:&quot;Caching problems and solutions cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching problems and solutions cheat sheet" title="Caching problems and solutions cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!0lM7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 424w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 848w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 1272w, https://substackcdn.com/image/fetch/$s_!0lM7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd616787a-ad6a-4c0d-bb5e-abc958d9c34f_2049x3537.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Distributed Caching problems and solutions cheat sheet</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design deep dives</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>Liked this article? Make sure to &#128153; click the like button.</p><p>Feedback or addition? Make sure to &#128172; comment.</p><p>Know someone who would find this helpful? Make sure to &#128257; share this post.</p><div><hr></div><h2>Get in Touch</h2><p>You can find me on <strong><a href="https://www.linkedin.com/in/aniketsingh03/">LinkedIn</a></strong> or <strong><a href="https://x.com/an1kets1ngh03">Twitter</a></strong>.</p>]]></content:encoded></item><item><title><![CDATA[A crash course on caching: Distributed Caching & Strategies (Part 2/3)]]></title><description><![CDATA[Deep dive into distributed caching for software engineers: sharding algorithms, cache eviction, and caching strategies explained]]></description><link>https://read.systemdesignblueprint.com/p/distributed-caching-algorithms-and-strategies</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/distributed-caching-algorithms-and-strategies</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 02 Oct 2024 04:45:55 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In <strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">What is Caching &amp; How it Works</a></strong>, we covered the basics of caching, how it operates at the hardware level (CPU and OS), and its implementation across various infrastructure components.</p><p>Today, we will focus on <strong>Distributed Caching</strong>, exploring how large-scale systems manage caching effectively. The key topics we'll cover include:</p><ol><li><p>What is Distributed Caching and its advantages?</p></li><li><p>How does distributed caching work?</p></li><li><p>What is Consistent Hashing?</p></li><li><p>Types of caching strategies</p></li><li><p>Types of cache eviction strategies</p></li><li><p>Popular examples of distributed caches</p></li></ol><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p>A crash course on caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">How to crack any system design interview</a>?</strong></p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/introduction-to-kafka">What is Kafka</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">How to use Redis in system design interviews</a>?</strong></p></li></ol><p>Before we dive deep, here's a<strong> distributed caching cheat sheet</strong> &#129527;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oZzN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oZzN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 424w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 848w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 1272w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oZzN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png" width="1456" height="2281" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2281,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1714072,&quot;alt&quot;:&quot;distributed caching cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="distributed caching cheat sheet" title="distributed caching cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!oZzN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 424w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 848w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 1272w, https://substackcdn.com/image/fetch/$s_!oZzN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F85cfe713-0d71-4ef8-8493-eed779d37bc4_2580x4041.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Caching cheat sheet</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h1>What is distributed caching?</h1><p>A naive approach to caching data is to store it in a single-node system, but this method faces several challenges:</p><ul><li><p><strong>Scalability:</strong> How to handle the vast volume of data that needs to be cached?</p></li><li><p><strong>Fault Tolerance:</strong> How can we ensure high availability of cached data if one node fails?</p></li><li><p><strong>Load Balancing:</strong> How to handle a high volume of concurrent read requests, such as a Twitter feed accessed by millions of users?</p></li></ul><p><strong>Distributed caching</strong> addresses these challenges as systems scale to accommodate <strong>millions of users</strong> and <strong>large datasets</strong>.</p><p>In a distributed caching system, multiple nodes store replicas of each dataset. This architecture enhances fault tolerance and ensures high availability while allowing load balancing of requests across nodes, enabling the system to efficiently handle millions of reads.</p><div><hr></div><h1>How Distributed Caching works?</h1><p><em><strong>Figure 2</strong></em> shows the components of a distributed cache system and how it works:</p><ol><li><p><strong>Cache Client - </strong>When caching data, the <strong>client library</strong> utilizes a <strong>hashing</strong> or <strong>sharding</strong> algorithm to determine the appropriate node or partition for data storage.</p></li><li><p><strong>Cache Cluster</strong></p><ol><li><p>A dedicated <strong>multi-node cluster</strong> operates with a <strong>master-slave</strong> configuration to manage all read and write operations.</p></li><li><p>Data is <strong>replicated</strong> across multiple nodes to enhance <strong>fault tolerance</strong> and ensure <strong>high availability</strong>.</p></li><li><p>All <strong>write</strong> operations are directed to the <strong>master node</strong>, which determines how to distribute the data to the slave nodes.</p></li><li><p><strong>Coordination mechanisms</strong>, such as <strong>distributed locks</strong> or <strong>consensus protocols,</strong> keep cache nodes synchronized, especially during simultaneous data modifications by multiple nodes.</p></li></ol></li><li><p><strong>Sharding - </strong>Data is divided into <strong>shards</strong>, each stored on a <strong>different</strong> cache node. This approach helps distribute data evenly and enables horizontal scaling of the cache.</p></li><li><p><strong>Caching Strategies - </strong>Policies for read and write operations must be established to determine how data is updated between the database and the cache or vice versa.</p></li><li><p><strong>Eviction Policies - </strong>Caches implement eviction strategies, such as <strong>LRU</strong> (Least Recently Used), <strong>LFU</strong> (Least Frequently Used), or <strong>TTL</strong> (Time to Live), to remove outdated or less frequently accessed data and create space for new entries.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!S9kS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!S9kS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 424w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 848w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 1272w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!S9kS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png" width="644" height="412.16" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:864,&quot;width&quot;:1350,&quot;resizeWidth&quot;:644,&quot;bytes&quot;:248239,&quot;alt&quot;:&quot;How does distributed caching work&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="How does distributed caching work" title="How does distributed caching work" srcset="https://substackcdn.com/image/fetch/$s_!S9kS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 424w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 848w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 1272w, https://substackcdn.com/image/fetch/$s_!S9kS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb708bf2b-1f20-4a1e-9b4e-ed1148081d46_1350x864.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: How does distributed caching work?</strong></figcaption></figure></div><div><hr></div><h1>Types of sharding algorithms in distributed caching</h1><p>Sharding algorithms are essential for distributing data across multiple nodes in a distributed caching system. They determine which node to read from or write to. There are 3 types of sharding algorithms:</p><h2>Modulus Sharding</h2><p><strong>Modulus sharding</strong> involves assigning a key to a shard based on the hash value of the key modulo the total number of shards.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vHkv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vHkv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 424w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 848w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 1272w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vHkv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png" width="354" height="307.82608695652175" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:480,&quot;width&quot;:552,&quot;resizeWidth&quot;:354,&quot;bytes&quot;:85324,&quot;alt&quot;:&quot;Sharding algorithms: What is modulus sharding&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Sharding algorithms: What is modulus sharding" title="Sharding algorithms: What is modulus sharding" srcset="https://substackcdn.com/image/fetch/$s_!vHkv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 424w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 848w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 1272w, https://substackcdn.com/image/fetch/$s_!vHkv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420fe7ee-4d9f-4465-a143-1f552f07241a_552x480.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Modulus sharding</strong></figcaption></figure></div><h3><strong>Pros &amp; cons</strong></h3><p><strong>&#9989; Uniform Distribution:</strong> The modulus function evenly distributes data across shards, avoiding hot spots since the hash output is random.</p><p><strong>&#9989; Simplicity:</strong> Easy to implement, as it relies on a simple hash function to determine the shard based on a key.</p><p><strong>&#10060; Scaling Difficulty:</strong> Modulus-based sharding complicates adding or removing shards because keys have to be redistributed, causing significant data migration.</p><p><strong>&#10060; Poor Range Queries:</strong> Since the sharding is based on the hash of keys, executing range queries (eg: getting keys between 1000 and 2000) becomes inefficient and requires querying multiple shards.</p><p></p><h2>Range-based Sharding</h2><p><strong>Range-based sharding</strong> assigns keys to specific shards based on predefined key ranges. With this approach, the system can divide the key space into specific ranges and then map each range to a particular shard.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SRoB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SRoB!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 424w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 848w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 1272w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SRoB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png" width="357" height="318.66442953020135" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:399,&quot;width&quot;:447,&quot;resizeWidth&quot;:357,&quot;bytes&quot;:78446,&quot;alt&quot;:&quot;Sharding algorithms: What is range based sharding&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Sharding algorithms: What is range based sharding" title="Sharding algorithms: What is range based sharding" srcset="https://substackcdn.com/image/fetch/$s_!SRoB!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 424w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 848w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 1272w, https://substackcdn.com/image/fetch/$s_!SRoB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1fbe594c-cd46-4e75-b4d2-511548b4d1e4_447x399.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Range-based sharding</strong></figcaption></figure></div><h3><strong>Pros &amp; cons</strong></h3><p>&#9989; <strong>Efficient Range Queries:</strong> Range-based sharding optimizes range queries by storing data in specific shards, reducing the need to access multiple shards.</p><p>&#9989; <strong>Logical Data Grouping:</strong> Related data is stored together, enhancing retrieval efficiency.</p><p>&#10060; <strong>Load Imbalance Risk:</strong> Uneven data distribution can overload certain shards if many values fall within a specific range.</p><p></p><h2>Consistent Hashing in Distributed caching</h2><p>Modulus and range-based sharding are easy to manage but struggle with <strong>scaling</strong>. When a node goes down or a new node is added (a common scenario in distributed systems), keys must be completely redistributed, leading to downtime and complexity.</p><p>Consistent hashing <em><strong>(Figure 5)</strong></em> solves this problem by mapping <strong>both</strong> <strong>keys</strong> and <strong>nodes</strong> onto a <strong>fixed-size ring</strong> using a <strong>hash function</strong>. This method minimizes key redistribution when nodes change, improving scalability and stability.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iqb5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iqb5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 424w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 848w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 1272w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iqb5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png" width="379" height="355.62555066079295" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/306f9919-3422-46b7-be6d-de334db026d2_681x639.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:639,&quot;width&quot;:681,&quot;resizeWidth&quot;:379,&quot;bytes&quot;:87740,&quot;alt&quot;:&quot;Sharding algorithms: What is consistent hashing&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Sharding algorithms: What is consistent hashing" title="Sharding algorithms: What is consistent hashing" srcset="https://substackcdn.com/image/fetch/$s_!iqb5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 424w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 848w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 1272w, https://substackcdn.com/image/fetch/$s_!iqb5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F306f9919-3422-46b7-be6d-de334db026d2_681x639.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Consistent Hashing</strong></figcaption></figure></div><h3>How does consistent hashing work?</h3><ol><li><p>When a key is requested, the system applies a hash function to map the key to a specific position on the ring.</p></li><li><p>The system traverses the ring in a clockwise direction from the key's position until it encounters the first node.</p></li><li><p>This node is responsible for storing the data associated with the key.</p></li></ol><h3>Handling failed nodes in Consistent Hashing</h3><p>When a node in the ring goes down, all keys assigned to that node are reassigned to the <strong>next available</strong> node in the ring, while other keys remain unaffected.</p><p><em><strong>Figure 6</strong></em> explains this scenario. When <em>node 4</em> fails, the key <em>k6</em> which was previously assigned to <em>node 4</em>, is <strong>reassigned</strong> to the next node in the ring i.e. <em>node 1.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GbiZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GbiZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 424w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 848w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 1272w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GbiZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png" width="445" height="410.3246753246753" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:639,&quot;width&quot;:693,&quot;resizeWidth&quot;:445,&quot;bytes&quot;:102828,&quot;alt&quot;:&quot;Distributed caching: How to handle failed nodes in consistent hashing&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed caching: How to handle failed nodes in consistent hashing" title="Distributed caching: How to handle failed nodes in consistent hashing" srcset="https://substackcdn.com/image/fetch/$s_!GbiZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 424w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 848w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 1272w, https://substackcdn.com/image/fetch/$s_!GbiZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb4f9f63b-d53f-439c-83f3-e4e53c2082cc_693x639.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Redistribution of keys when a node is removed</strong></figcaption></figure></div><h3>Handling new nodes in Consistent Hashing</h3><p>When a new node is added to the ring, all keys that lie <strong>between</strong> this <strong>new</strong> <strong>node</strong> and the <strong>previous</strong> <strong>node</strong> are remapped to the new node, while keys assigned to other nodes remain unaffected.</p><p><em><strong>Figure 7</strong></em> explains this scenario. When a new node (<em>node 5)</em>, is added to the cluster, the key <em>k6</em>, previously assigned to <em>node 4</em>, is now <strong>reassigned</strong> to the new <em>node 5</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!BH78!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!BH78!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 424w, https://substackcdn.com/image/fetch/$s_!BH78!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 848w, https://substackcdn.com/image/fetch/$s_!BH78!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 1272w, https://substackcdn.com/image/fetch/$s_!BH78!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!BH78!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png" width="457" height="425.34632034632034" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:645,&quot;width&quot;:693,&quot;resizeWidth&quot;:457,&quot;bytes&quot;:101162,&quot;alt&quot;:&quot;Distributed caching: How to handle new nodes in consistent hashing&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed caching: How to handle new nodes in consistent hashing" title="Distributed caching: How to handle new nodes in consistent hashing" srcset="https://substackcdn.com/image/fetch/$s_!BH78!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 424w, https://substackcdn.com/image/fetch/$s_!BH78!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 848w, https://substackcdn.com/image/fetch/$s_!BH78!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 1272w, https://substackcdn.com/image/fetch/$s_!BH78!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe6f44f13-f389-451f-b1e1-98bdbe708add_693x645.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Redistribution of keys when a new node is added</strong></figcaption></figure></div><div><hr></div><h1>Types of Caching Strategies for Distributed Systems</h1><p>There are <strong>five types</strong> of caching strategies, categorized into <strong>two subtypes</strong> based on their focus: <strong>read strategies</strong> and <strong>write strategies</strong>.</p><ol><li><p><strong>Read strategies</strong>: Cache Aside, Read Through</p></li><li><p><strong>Write strategies</strong>: Write Around, Write Back, Write Through</p></li></ol><p>In the following sections, we will explore each strategy in detail and discuss their pros &amp; cons.</p><h2>Cache Aside</h2><ol><li><p>The application requests data for a key from the cache.</p></li><li><p>If the key is present in the cache (<strong>cache hit</strong>), it is returned to the application.</p></li><li><p>If key is not present in the cache (<strong>cache miss</strong>), the <strong>application</strong>:</p><ol><li><p>Fetches data from the main DB.</p></li><li><p>Updates the cache with latest data for future reads.</p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!KFC0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!KFC0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 424w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 848w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 1272w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!KFC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png" width="368" height="293.82945736434107" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/60a29822-1394-410d-bd66-e0356ae16df2_774x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:774,&quot;resizeWidth&quot;:368,&quot;bytes&quot;:81936,&quot;alt&quot;:&quot;Distributed cache strategies: What is cache aside?&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed cache strategies: What is cache aside?" title="Distributed cache strategies: What is cache aside?" srcset="https://substackcdn.com/image/fetch/$s_!KFC0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 424w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 848w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 1272w, https://substackcdn.com/image/fetch/$s_!KFC0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F60a29822-1394-410d-bd66-e0356ae16df2_774x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Cache Aside caching policy</strong></figcaption></figure></div><h3>Pros &amp; Cons of cache aside</h3><p><strong>&#9989; </strong>The system <strong>can</strong> <strong>tolerate</strong> <strong>cache</strong> <strong>failures</strong>, as it can still read from the storage.</p><p><strong>&#10060; </strong>The application must manage both cache and storage, <strong>complicating</strong> the code.</p><p></p><h2>Read Through cache</h2><ol><li><p>The application requests data for a key from the cache.</p></li><li><p>If the key is present in the cache (<strong>cache hit</strong>), it is returned to the application.</p></li><li><p>If key is not present in the cache (<strong>cache miss</strong>), the <strong>cache</strong>:</p><ol><li><p>Fetches data from the main DB.</p></li><li><p>Updates itself with the latest data and returns it to the application.</p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GDzR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GDzR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 424w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 848w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 1272w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GDzR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png" width="368" height="299.6363636363636" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:759,&quot;resizeWidth&quot;:368,&quot;bytes&quot;:86317,&quot;alt&quot;:&quot;Distributed cache strategies: What is read through cache&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed cache strategies: What is read through cache" title="Distributed cache strategies: What is read through cache" srcset="https://substackcdn.com/image/fetch/$s_!GDzR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 424w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 848w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 1272w, https://substackcdn.com/image/fetch/$s_!GDzR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01decc58-cc59-4213-a4ab-1dc1d8ac877a_759x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Read Through caching policy</strong></figcaption></figure></div><h3>Pros &amp; Cons of read-through cache</h3><p><strong>&#9989; </strong>The implementation is <strong>simple</strong>, as the application only needs to manage cache (<strong>not</strong> storage).</p><p><strong>&#10060; </strong>The system <strong>cannot</strong> <strong>tolerate</strong> <strong>cache</strong> <strong>failures</strong> as it doesn&#8217;t rely on DB.</p><p><strong>&#10060; </strong>The cache populates only after the first request, leading to <strong>slower initial access</strong> for new data&#8212; an issue for high-throughput systems that need pre-warmed caches.</p><p></p><h2>Write Around cache</h2><ol><li><p>All the <strong>write</strong> <strong>requests</strong> go directly to the DB and <strong>read</strong> <strong>requests</strong> land on the cache.</p></li><li><p>If the key is present in the cache (<strong>cache hit</strong>), it is returned to the application.</p></li><li><p>If key is not present in the cache (<strong>cache miss</strong>), the <strong>cache</strong>:</p><ol><li><p>Fetches data from the main DB</p></li><li><p>Updates itself with the latest data and returns it to the application</p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dw9F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dw9F!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 424w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 848w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 1272w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dw9F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png" width="370" height="301.2648221343874" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:618,&quot;width&quot;:759,&quot;resizeWidth&quot;:370,&quot;bytes&quot;:89553,&quot;alt&quot;:&quot;Distributed cache strategies: What is write around cache?&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed cache strategies: What is write around cache?" title="Distributed cache strategies: What is write around cache?" srcset="https://substackcdn.com/image/fetch/$s_!dw9F!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 424w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 848w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 1272w, https://substackcdn.com/image/fetch/$s_!dw9F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94272646-e706-4b4b-bf58-ac6cf7cf8dba_759x618.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Write Around caching policy</strong></figcaption></figure></div><h3>Pros &amp; Cons of write around cache</h3><p><strong>&#9989; </strong>Cache contains the data that it <strong>really needs</strong> (frequently read).</p><p><strong>&#10060; </strong>Frequent cache misses for recently written data can cause <strong>inconsistent read performance</strong>, as data may need to be fetched from the database.</p><p></p><h2>Write Back cache</h2><ol><li><p>All the data is written directly to the cache.</p></li><li><p>The actual write to the DB happens later, asynchronously, or when the cache data is evicted.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RAJ4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RAJ4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 424w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 848w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 1272w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RAJ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png" width="369" height="292.9748743718593" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:474,&quot;width&quot;:597,&quot;resizeWidth&quot;:369,&quot;bytes&quot;:64949,&quot;alt&quot;:&quot;Distributed cache strategies: What is write back cache&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed cache strategies: What is write back cache" title="Distributed cache strategies: What is write back cache" srcset="https://substackcdn.com/image/fetch/$s_!RAJ4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 424w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 848w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 1272w, https://substackcdn.com/image/fetch/$s_!RAJ4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ce06df7-73e8-401f-9ae1-28975693231c_597x474.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 11: Write back caching policy</strong></figcaption></figure></div><h3>Pros &amp; Cons of write-back cache</h3><p><strong>&#9989; Writes</strong> are <strong>faster</strong> since they are only performed in the cache, deferring the actual database write.</p><p><strong>&#9989; </strong>Multiple writes to the same data can be combined into a <strong>single database operation</strong>, <strong>reducing</strong> the number of <strong>I/O operations.</strong></p><p><strong>&#10060; </strong>The cache holds the most recent version of the data, so if other services access the database directly, they might see <strong>stale</strong> or <strong>outdated</strong> data.</p><p></p><h2>Write Through cache</h2><ol><li><p>All write requests from the application land on the cache.</p></li><li><p>The actual write to the DB also happens in sync before returning response to the application.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iOqg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iOqg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 424w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 848w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 1272w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iOqg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png" width="371" height="294.56281407035175" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:474,&quot;width&quot;:597,&quot;resizeWidth&quot;:371,&quot;bytes&quot;:65174,&quot;alt&quot;:&quot;Distributed cache strategies: What is write through cache?&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Distributed cache strategies: What is write through cache?" title="Distributed cache strategies: What is write through cache?" srcset="https://substackcdn.com/image/fetch/$s_!iOqg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 424w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 848w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 1272w, https://substackcdn.com/image/fetch/$s_!iOqg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6785a90c-018d-436b-b033-e84e4e9b4f07_597x474.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 12: Write through caching policy</strong></figcaption></figure></div><h3>Pros &amp; Cons of write-through cache</h3><p><strong>&#9989; </strong>Since writes are performed in both the cache and the database simultaneously, the cache is always in sync with the DB, ensuring <strong>data consistency</strong>.</p><p><strong>&#9989; </strong>By writing data to the cache during writes, future reads benefit from cached data, <strong>enhancing</strong> <strong>read performance</strong>.</p><p><strong>&#10060; </strong>Every write operation incurs the overhead of writing to both the cache and the database, which can <strong>slow down</strong> the <strong>write performance</strong>.</p><div><hr></div><h1>Cache eviction strategies in Distributed Caches</h1><p><strong>Cache eviction</strong> is crucial for maintaining <strong>fresh</strong> and <strong>relevant</strong> data. There are <strong>four</strong> <strong>main</strong> types of cache eviction policies:</p><h2>Least Recently Used (LRU)</h2><p>LRU evicts the cache entry that has <strong>not been used</strong> <strong>for the longest time</strong>. It assumes that data that hasn't been accessed in a while is less likely to be needed soon.</p><h4><strong>Pros &amp; Cons</strong></h4><p><strong>&#9989; </strong>Adapts well to scenarios with temporal locality (data accessed recently is likely to be accessed again soon).</p><p><strong>&#10060; </strong>Requires tracking the usage order of all items, which can be computationally expensive for large caches.</p><blockquote><p><em>Example:</em> <strong>Web browsers</strong> often use LRU to manage their cache of visited web pages. The most recently visited pages are kept in the cache, while the least recently accessed pages are evicted to make space for new ones.</p></blockquote><p></p><h2>Least Frequently Used (LFU)</h2><p>LFU evicts the items that are <strong>accessed</strong> <strong>the</strong> <strong>least number of times.</strong> It tracks the frequency of access to each cached item and removes those that have been used the least over time.</p><h4>Pros &amp; Cons</h4><p><strong>&#9989; </strong>Works well in scenarios where frequently accessed data is important (product details on an e-commerce website).</p><p><strong>&#10060; </strong>Harder to implement as it requires tracking access counts.</p><blockquote><p><em>Example:</em> <strong>Search engines</strong> use LFU in their query caching mechanisms to keep the most frequently searched queries cached, allowing fast retrieval for popular searches.</p></blockquote><p></p><h2>First In First Out (FIFO)</h2><p>FIFO evicts the oldest item in the cache, based on when it was added, regardless of how often it has been accessed. The <strong>first item added</strong> is the <strong>first item removed</strong>.</p><h4><strong>Pros &amp; Cons</strong></h4><p><strong>&#9989; </strong>Simple to implement since it doesn't require tracking access times or counts.</p><p><strong>&#10060; </strong>Doesn&#8217;t account for how often or how recently an item is used, which can lead to suboptimal cache efficiency.</p><blockquote><p><em>Example:</em> <strong>Message brokers</strong> like <strong>RabbitMQ</strong> may use FIFO in their in-memory message cache, where older messages are evicted to ensure space for newer ones without considering usage patterns.</p></blockquote><p></p><h2>Time To Live (TTL)</h2><p>TTL evicts items automatically from the cache after a predefined time period, regardless of how frequently or recently the item was accessed. It&#8217;s a <strong>time-based expiration</strong> method.</p><h4><strong>Pros &amp; Cons</strong></h4><p><strong>&#9989; </strong>Useful when data is time-sensitive, such as temporary data or cached results that may become invalid after a certain period.</p><p><strong>&#10060; </strong>Items may still be relevant when evicted if they haven&#8217;t been accessed frequently during the TTL window.</p><blockquote><p><em>Example:</em> <strong>User session tokens</strong> can be cached with a predefined TTL (eg: 5 mins). Once the TTL expires, the token is evicted from the cache, and a new session token must be generated.</p></blockquote><div><hr></div><h1>Popular distributed caching solutions</h1><p><strong><a href="https://github.com/redis/redis">Redis</a></strong> and <strong><a href="https://github.com/memcached/memcached">Memcached</a></strong> are two most popular open-sources distributed caches. A quick comparison matrix between them:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DbBj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DbBj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 424w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 848w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 1272w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DbBj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png" width="392" height="626.1296928327645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1404,&quot;width&quot;:879,&quot;resizeWidth&quot;:392,&quot;bytes&quot;:266820,&quot;alt&quot;:&quot;Which is better - redis or memcached?&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Which is better - redis or memcached?" title="Which is better - redis or memcached?" srcset="https://substackcdn.com/image/fetch/$s_!DbBj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 424w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 848w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 1272w, https://substackcdn.com/image/fetch/$s_!DbBj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F70779d87-0c7a-49e4-bb84-bf0839d10038_879x1404.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 12: Comparison matrix between Redis and Memcached</strong></figcaption></figure></div><p>Additionally, <strong>Amazon ElastiCache</strong> is a <strong>fully managed</strong> in-memory caching service from AWS that supports both<strong> Redis</strong> and <strong>Memcached</strong>, giving developers flexibility in choosing the right engine for their use case.</p><p>It offers <strong>multi-AZ</strong> (Availability Zone) deployments with automatic failover, ensuring high availability even during an AZ outage.</p><p>ElastiCache can also auto-scale the number of nodes in the cluster based on traffic demand, enabling seamless handling of fluctuating workloads without manual scaling efforts.</p><div><hr></div><p>That&#8217;s all for today<strong>. </strong>Thanks for reading<strong> &#128591;&#127995;</strong>!</p><p>In the next issue, we will explore <strong>key challenges with caches</strong> &amp; <strong>how to solve them</strong>, how to implement caching (with <strong>code snippets</strong>), &amp; key gotchas about how <strong>NOT</strong> to do caching.</p><p>Creating each article, along with the accompanying diagrams, takes about <strong>12-14 hours</strong> of research and hard work. If you enjoy my content, please &#10084;&#65039; and subscribe to encourage me to keep creating more such valuable content! &#128591;&#127995;.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you like my work, please subscribe to support!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[A crash course on caching: What is caching and how does it work (Part 1/3)]]></title><description><![CDATA[Learn the fundamentals of caching and its essential role in optimizing web performance, including key terms and real-world examples like Netflix and Facebook]]></description><link>https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 25 Sep 2024 04:45:23 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Caching</strong> is a fundamental technique in computing that enables <strong>quick retrieval</strong> of <strong>frequently accessed data</strong>. Research shows that a <strong>page load delay</strong> of just <strong>0.5 seconds</strong> in Google search can result in a <strong>20% drop in traffic</strong>. By improving access speed and reducing load times, caching boosts system performance and enhances user experience, directly impacting business outcomes.</p><p>Since caching is a broad topic, I will explore its various facets in a comprehensive <strong>3-part series</strong>. In this first issue, we will start with the <strong>basics</strong> and cover the following topics:</p><ol><li><p>What is caching &amp; why is it necessary?</p></li><li><p>How does caching occur at the hardware layer (CPU/OS Level)?</p></li><li><p>Where to implement caching?</p></li><li><p>Real-world examples from large-scale systems</p></li></ol><p>If you are a recent subscriber, here&#8217;s what you might have missed:</p><ol><li><p>A crash course on caching - <strong><a href="https://read.systemdesignblueprint.com/p/distributed-caching-algorithms-and-strategies">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/introduction-to-kafka">What is Kafka</a></strong>?</p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide">How to use Kafka in system design interviews</a></strong>?</p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-1-how-to-design-a-payment-system">How to design a payments system</a>?</strong></p></li></ol><p>Before we dive deep, here's a <strong>quick caching cheat sheet</strong> &#129527;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!j5YM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!j5YM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 424w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 848w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 1272w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!j5YM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png" width="1456" height="1570" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1570,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3357231,&quot;alt&quot;:&quot;Caching cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching cheat sheet" title="Caching cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!j5YM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 424w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 848w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 1272w, https://substackcdn.com/image/fetch/$s_!j5YM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F604c1022-1389-4f9b-955c-43953b35df84_5060x5455.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Caching cheat sheet</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>What is caching and why is it necessary?</h2><h4>What is caching?</h4><p>Caching is a technique used in computing to store <strong>frequently accessed data</strong> in a fast storage area, usually <strong>in memory</strong>. Instead of using a database which can be slow due to <strong>disk reads</strong>, caching lets the system quickly access the same data. This speeds things up and makes the system run more efficiently.</p><h4>Example</h4><p><em><strong>Figure 2</strong></em> illustrates a practical example of <strong>how caching works</strong>. In this scenario, data is <strong>first retrieved</strong> from the <strong>cache</strong> (stored in memory). If the data is <strong>not found</strong> in the cache, it is then fetched from the database. This contrasts with a system without caching, where data is <strong>always retrieved</strong> directly from the<strong> database</strong>, leading to longer response times.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!75Q0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!75Q0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 424w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 848w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!75Q0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png" width="476" height="375.63461538461536" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/df4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1149,&quot;width&quot;:1456,&quot;resizeWidth&quot;:476,&quot;bytes&quot;:232724,&quot;alt&quot;:&quot;what is caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="what is caching" title="what is caching" srcset="https://substackcdn.com/image/fetch/$s_!75Q0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 424w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 848w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 1272w, https://substackcdn.com/image/fetch/$s_!75Q0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf4b0918-023c-430e-93d3-a829c2eb596c_1520x1200.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: What is caching?</strong></figcaption></figure></div><h4>Key terms in caching</h4><ul><li><p><strong>Cache Hit:</strong> When data is found in the cache, it&#8217;s called a <em>cache hit</em>, allowing quick access without going to the DB.</p></li><li><p><strong>Cache Miss:</strong> A <em>cache miss</em> happens when the data isn&#8217;t in the cache, so the system has to fetch it from the DB.</p></li><li><p><strong>Cache Hit Ratio:</strong> The <em>cache hit ratio</em> measures how effective the cache is. It&#8217;s the number of cache hits divided by total requests&#8212;higher ratios mean better performance.</p></li></ul><h4>Pros &amp; Cons of caching</h4><h5>Pros:</h5><ol><li><p><strong>Faster data access:</strong> Caching speeds up data retrieval by avoiding slow database or disk access.</p></li><li><p><strong>Reduces backend load:</strong> By serving frequent requests from the cache, it eases the load on databases, APIs, and backend systems.</p></li><li><p><strong>Better user experience:</strong> Faster responses lead to smoother interactions. This improves user satisfaction.</p></li></ol><h5>Cons:</h5><ul><li><p><strong>Limited capacity</strong>: Caches have less space than disks. So, they can't store much data or provide long-term persistence.</p></li><li><p><strong>Stale data:</strong> Cached data can become outdated if not refreshed, causing inconsistencies with the DB.</p></li><li><p><strong>Higher costs:</strong> Storing data in memory is more expensive than on disk, and memory is more limited.</p></li><li><p><strong>Cold cache delays:</strong> When the cache is empty (a "cold cache"), initial data requests may still experience slower response times. <strong>Note:</strong> We will discuss problems with caching in detail in a later issue.</p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Caching at the hardware level: CPU and OS caching explained</h2><p><strong>Hardware caching</strong> is built into systems to make data access faster. CPUs and operating systems have caches that help retrieve frequently used data quickly, reducing the need to access slower memory or disks. In this section, we&#8217;ll cover how <strong>CPU</strong> and <strong>OS-level caches</strong> work to improve system performance.</p><p><em><strong>Figure 3</strong></em> shows different mechanisms used by the CPU and OS for caching.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bYy0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bYy0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 424w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 848w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 1272w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bYy0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png" width="560" height="607.6923076923077" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1580,&quot;width&quot;:1456,&quot;resizeWidth&quot;:560,&quot;bytes&quot;:768186,&quot;alt&quot;:&quot;Caching at the hardware level: CPU and OS caching explained&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching at the hardware level: CPU and OS caching explained" title="Caching at the hardware level: CPU and OS caching explained" srcset="https://substackcdn.com/image/fetch/$s_!bYy0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 424w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 848w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 1272w, https://substackcdn.com/image/fetch/$s_!bYy0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe609df4c-aff2-49fb-a4da-19cd4fd88a8c_2000x2170.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: CPU and OS level caching</strong></figcaption></figure></div><h4><strong>CPU Caching</strong></h4><p>CPUs use <strong>multi-level caches</strong> to store frequently accessed instructions for faster processing. There are four key types of caches found in the CPU:</p><ol><li><p><strong>L1 Cache - </strong>The fastest and smallest cache, typically under 64KB, with each CPU core having its own <strong>dedicated L1 cache</strong>.</p></li><li><p><strong>L2 Cache - </strong>The second level of caching, ranging from 512KB to 1MB, and also <strong>dedicated per core</strong>.</p></li><li><p><strong>L3 Cache - </strong>The largest but slowest cache, usually over 3MB, <strong>shared</strong> between all CPU cores.</p></li><li><p><strong>Translation Lookaside Buffer (TLB) - </strong>Stores recently used address mappings to speed up <strong>virtual-to-physical memory translation</strong>. It resides in the Memory Management Unit (MMU) and helps optimize memory access.</p></li></ol><h4><strong>OS-level caching</strong></h4><ol><li><p><strong>Page Cache - </strong>Located in main memory, it stores <strong>frequently accessed data pages</strong>, reducing the need for slower disk access.</p></li><li><p><strong>Inode Cache</strong> - An in-memory cache that stores file metadata (inodes). It speeds up access to file properties like <strong>size</strong> and <strong>permissions</strong>. This improves file system performance.</p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Where should caching be implemented?</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qwoo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qwoo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 424w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 848w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 1272w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qwoo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png" width="1456" height="1121" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1121,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2153709,&quot;alt&quot;:&quot;Where to implement caching -- api gateway, cdn, db, redis, distributed caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Where to implement caching -- api gateway, cdn, db, redis, distributed caching" title="Where to implement caching -- api gateway, cdn, db, redis, distributed caching" srcset="https://substackcdn.com/image/fetch/$s_!qwoo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 424w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 848w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 1272w, https://substackcdn.com/image/fetch/$s_!qwoo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37673eda-7d9b-405d-a2f8-333605d70188_4520x3480.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Caches at different levels of infrastructure</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Caching can be applied at different points in a system's infrastructure to enhance performance and reduce latency. Key areas include <strong>browsers</strong>, <strong>CDNs</strong>, <strong>API gateways</strong>, <strong>databases</strong>, and <strong>distributed caches</strong>. Let&#8217;s examine each one to see how caching optimizes system performance.</p><h3>Client-side caching: Improve user experience and reduce latency</h3><p>Also known as <strong>browser caching</strong>, this involves storing static assets like <strong>HTML</strong>, <strong>JavaScript</strong>, and <strong>images</strong> on the client side. This helps speed up page loads by avoiding repeated downloads from the server.</p><p><em><strong>Figure 5</strong></em> shows how HTML is cached in the browser with an expiration time. <strong>Future requests</strong> can then be served from the <strong>cache</strong>, instead of reloading from the server.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!guHX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!guHX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 424w, https://substackcdn.com/image/fetch/$s_!guHX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 848w, https://substackcdn.com/image/fetch/$s_!guHX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 1272w, https://substackcdn.com/image/fetch/$s_!guHX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!guHX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png" width="424" height="316.25274725274727" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1086,&quot;width&quot;:1456,&quot;resizeWidth&quot;:424,&quot;bytes&quot;:265104,&quot;alt&quot;:&quot;Client-side caching: Improve user experience and reduce latency&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Client-side caching: Improve user experience and reduce latency" title="Client-side caching: Improve user experience and reduce latency" srcset="https://substackcdn.com/image/fetch/$s_!guHX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 424w, https://substackcdn.com/image/fetch/$s_!guHX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 848w, https://substackcdn.com/image/fetch/$s_!guHX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 1272w, https://substackcdn.com/image/fetch/$s_!guHX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b536e31-454c-4713-8948-0b9e5007782d_1515x1130.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Client-side caching</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> When a user visits an e-commerce website, the browser caches images, stylesheets, and scripts. On future visits, these assets load from local storage. This speeds up the experience by avoiding unnecessary downloads.</p></blockquote><p><strong>Types of client-side caching</strong>:</p><ul><li><p><strong>Local Storage Cache</strong>: Stores static assets such as images and HTML in the browser&#8217;s local storage.</p></li><li><p><strong>Service Worker Cache</strong>: Allows applications to work offline by caching responses. It enables features like Progressive Web Apps (PWAs) to function without an internet connection.</p></li></ul><p></p><h3>CDN caching: Enhancing web performance through Content Delivery Network</h3><p>A Content Delivery Network (CDN) caches <strong>static</strong> <strong>content</strong> such as <strong>images</strong>, <strong>videos</strong>, <strong>stylesheets</strong>, and <strong>scripts</strong> on servers located in various geographic areas. This helps reduce latency by delivering content from a server <strong>closest to the user</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sfO1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sfO1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 424w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 848w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 1272w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sfO1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png" width="448" height="249.84615384615384" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:812,&quot;width&quot;:1456,&quot;resizeWidth&quot;:448,&quot;bytes&quot;:225770,&quot;alt&quot;:&quot;CDN caching: Enhancing web performance through Content Delivery Network&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="CDN caching: Enhancing web performance through Content Delivery Network" title="CDN caching: Enhancing web performance through Content Delivery Network" srcset="https://substackcdn.com/image/fetch/$s_!sfO1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 424w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 848w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 1272w, https://substackcdn.com/image/fetch/$s_!sfO1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe60007ce-9ddb-42c2-adcf-912046bfc234_1640x915.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: CDN caching</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> In <strong>video-streaming services</strong> like Netflix or YouTube, CDNs store video content in different locations. When a user in India watches a movie, the CDN delivers it from a nearby edge server instead of the original source.</p></blockquote><p><strong>Types of CDN caching</strong>:</p><ul><li><p><strong>Static Content Caching:</strong> Stores static files like images, videos, and scripts for quick access.</p></li><li><p><strong>Dynamic Content Caching:</strong> Some CDNs can also cache dynamic content using methods like edge-side includes or smart caching algorithms.</p></li></ul><p></p><h3>Caching at API Gateway/Load Balancer</h3><p>API gateways cache <strong>responses</strong> for <strong>frequently accessed</strong> API endpoints to reduce the load on backend services. This is ideal for read-heavy APIs with relatively static data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!US9y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!US9y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 424w, https://substackcdn.com/image/fetch/$s_!US9y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 848w, https://substackcdn.com/image/fetch/$s_!US9y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 1272w, https://substackcdn.com/image/fetch/$s_!US9y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!US9y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png" width="524" height="273.1565934065934" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:759,&quot;width&quot;:1456,&quot;resizeWidth&quot;:524,&quot;bytes&quot;:285749,&quot;alt&quot;:&quot;Caching at API Gateway or Load Balancer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching at API Gateway or Load Balancer" title="Caching at API Gateway or Load Balancer" srcset="https://substackcdn.com/image/fetch/$s_!US9y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 424w, https://substackcdn.com/image/fetch/$s_!US9y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 848w, https://substackcdn.com/image/fetch/$s_!US9y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 1272w, https://substackcdn.com/image/fetch/$s_!US9y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F824d3871-755b-43aa-a876-1cf6bcbf7360_2005x1045.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Load Balancer/API Gateway caching</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> A currency conversion API that updates its rates every 10 minutes can cache responses at the gateway, reducing the need to hit backend services for every request.</p></blockquote><p><strong>Types</strong>:</p><ul><li><p><strong>Per-Route Caching</strong>: Caches responses for specific API routes.</p></li><li><p><strong>Header-Based Caching</strong>: Cache can vary depending on headers like user-agent, locale, etc.</p></li></ul><p></p><h3>Distributed Caching</h3><p>Distributed caching stores data across multiple nodes. It ensures <strong>quick access</strong> and <strong>high availability</strong> in large-scale systems. Highly scalable systems use it to improve access speed across multiple regions or services.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FZG0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FZG0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 424w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 848w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 1272w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FZG0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png" width="494" height="260.57142857142856" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:768,&quot;width&quot;:1456,&quot;resizeWidth&quot;:494,&quot;bytes&quot;:275050,&quot;alt&quot;:&quot;What is distributed caching&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="What is distributed caching" title="What is distributed caching" srcset="https://substackcdn.com/image/fetch/$s_!FZG0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 424w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 848w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 1272w, https://substackcdn.com/image/fetch/$s_!FZG0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F55bd328f-ff1b-401b-a1a1-1d0ebef35b53_1820x960.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 8: Distributed caching</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> A social media platform may store user session data in a distributed cache (eg: Redis Cluster), ensuring that users can access their sessions quickly regardless of which region they&#8217;re in.</p></blockquote><p><strong>Redis and Memcached</strong> are two widely used distributed caching systems. </p><p><em><strong>Figure 8</strong></em> illustrates a typical setup &#8212; <strong>90% of read requests</strong> from clients are directed to a <strong>distributed cache cluster</strong> with <strong>multiple nodes</strong>. This design ensures high availability&#8212;if one node goes down, data can still be served from another node that holds a replica. Only <strong>10% of requests</strong> go to the database due to <strong>cache misses</strong>.</p><p><strong>Note:</strong> We will discuss distributed caching in detail in the <strong>next article</strong>.</p><p></p><h3>Caching in Relational databases</h3><p>Relational databases, such as MySQL, use in-memory caching to store <strong>query results</strong> or <strong>frequently accessed data</strong>. This approach speeds up subsequent access by reducing the need for costly disk I/O operations or executing complex queries each time.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WR0S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WR0S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 424w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 848w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 1272w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WR0S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png" width="382" height="298.8704663212435" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:755,&quot;width&quot;:965,&quot;resizeWidth&quot;:382,&quot;bytes&quot;:213192,&quot;alt&quot;:&quot;Caching in relational databases&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching in relational databases" title="Caching in relational databases" srcset="https://substackcdn.com/image/fetch/$s_!WR0S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 424w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 848w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 1272w, https://substackcdn.com/image/fetch/$s_!WR0S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15a818c2-9a3a-40e5-9cf3-25406048f5b7_965x755.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: Relational DB caching</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> An e-commerce site could cache the results of a popular query like "top-selling products," which doesn't change often, in a Redis cache to avoid re-executing the same query repeatedly.</p></blockquote><p><strong>Types of caches in relational DBs:</strong> </p><ol><li><p><strong>Transaction log:</strong> It records all <strong>atomic changes</strong> made to a database (eg: credit/debit in banking applications). It is used primarily for <strong>recovery purposes</strong>, ensuring that all operations can be <strong>rolled back</strong> in case of failure.</p></li><li><p><strong>Write-Ahead Logging (WAL)</strong>: WAL is a specific logging technique where changes are written to a log before being applied to the database. This ensures that data can be recovered in case of a crash.</p></li><li><p><strong>Replication log</strong>: They record changes to the primary database. Then, they send these to replicas to <strong>ensure consistency</strong> in distributed systems.</p></li><li><p><strong>Bufferpool</strong>: An area in memory where <strong>frequently accessed</strong> data <strong>pages</strong> from the database are cached. <strong>Example</strong>: In e-commerce, product information that is accessed frequently is cached in the buffer pool. This allows for quick retrieval at peak shopping times.</p></li><li><p><strong>Materialized views</strong>: It&#8217;s a <strong>precomputed result set</strong> that is stored like a table. It can be refreshed periodically to reflect changes in the underlying data. <strong>Example</strong>: In a <strong>reporting application</strong>, a materialized view aggregates <strong>sales data</strong> for quick access during analysis. This allows users to retrieve summary reports faster than querying the raw data directly.</p></li></ol><p></p><h3>Caching in message queues for high-traffic applications</h3><p>Message queues store messages on disk until they are processed by consumers. You can set c<strong>ustom retention policies</strong> to keep messages for a specific period. This allows for <strong>retries</strong> if consumers are <strong>unavailable</strong> or in <strong>case of failures</strong>, such as a Kafka outage.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VZer!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VZer!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 424w, https://substackcdn.com/image/fetch/$s_!VZer!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 848w, https://substackcdn.com/image/fetch/$s_!VZer!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 1272w, https://substackcdn.com/image/fetch/$s_!VZer!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VZer!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png" width="504" height="199.03846153846155" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/baf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:575,&quot;width&quot;:1456,&quot;resizeWidth&quot;:504,&quot;bytes&quot;:234275,&quot;alt&quot;:&quot;Caching in message queues for high-traffic applications&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching in message queues for high-traffic applications" title="Caching in message queues for high-traffic applications" srcset="https://substackcdn.com/image/fetch/$s_!VZer!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 424w, https://substackcdn.com/image/fetch/$s_!VZer!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 848w, https://substackcdn.com/image/fetch/$s_!VZer!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 1272w, https://substackcdn.com/image/fetch/$s_!VZer!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbaf8f5fc-ca0e-4ea3-91fc-6a99cf06902d_1685x665.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 10: Caching in message queues</strong></figcaption></figure></div><blockquote><p><strong>Example:</strong> In an e-commerce application, when a user places an order, the order details are sent to a message queue (eg: RabbitMQ or Kafka). The message is stored on disk until a fulfillment service processes it. A retention policy keeps unprocessed messages for a set duration eg: 7 days) to enable retries in case of failures.</p></blockquote><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><h3>Caching in search systems</h3><p>Search systems, such as <strong>Elasticsearch</strong> or <strong>Solr</strong>, index documents to facilitate quick retrieval based on queries. They use different indexing techniques, including <strong>forward</strong> and <strong>inverted indexing</strong>, to optimize search performance.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HKp-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HKp-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 424w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 848w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HKp-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png" width="356" height="357.54112554112555" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1160,&quot;width&quot;:1155,&quot;resizeWidth&quot;:356,&quot;bytes&quot;:286066,&quot;alt&quot;:&quot;Caching in search systems&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Caching in search systems" title="Caching in search systems" srcset="https://substackcdn.com/image/fetch/$s_!HKp-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 424w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 848w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 1272w, https://substackcdn.com/image/fetch/$s_!HKp-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5108f351-da2c-48cf-bc4d-d88bdb52b582_1155x1160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 11: Caching in search systems (inverted index example)</strong></figcaption></figure></div><p><strong>Types of caching in search systems:</strong></p><ul><li><p><strong>Forward Index</strong></p><ul><li><p>The forward index maps <strong>documents to their terms</strong>, allowing quick access to the content of each document.</p></li><li><p><strong>Example:</strong> Google Docs maintains a forward index that links document IDs to headings, enabling it to quickly generate the table of contents for billions of documents in real time.</p></li></ul></li><li><p><strong>Inverted Index</strong></p><ul><li><p>An inverted index (<em><strong>Figure 11</strong></em>) maps <strong>terms to their corresponding documents</strong>, allowing for rapid lookups. It is the backbone of most modern search engines.</p></li><li><p><strong>Example</strong>: In a search engine for a news website, the inverted index lists terms like "election," "president," and "vote," with pointers to the articles that contain those terms. If a user searches for "president," the system quickly retrieves all relevant articles without scanning every document.</p></li></ul></li></ul><div><hr></div><h2>How Netflix, Facebook, and Twitter leverage caching for efficiency?</h2><p>Let&#8217;s look at some <strong>real-world examples</strong> of caching in large-scale systems:</p><h3>Netflix</h3><ol><li><p><strong>Content Delivery Network (CDN)</strong> : Netflix uses its own CDN, called <strong>Open Connect</strong>, to cache video content closer to the users. By deploying servers at ISPs (Internet Service Providers), Netflix reduces latency and bandwidth costs while ensuring high-quality video streaming.</p></li><li><p><strong>Personalization and Recommendations</strong> : Netflix caches <strong>personalized recommendations</strong> and <strong>metadata</strong> about shows and movies. This allows the recommendation engine to quickly provide relevant suggestions without repeatedly querying the backend systems.</p></li></ol><h3>Facebook</h3><ol><li><p><strong>Memcached Deployment</strong>: Facebook is known for its <strong>large-scale deployment</strong> of <strong>Memcached</strong> to cache data retrieved from its databases. This caching layer helps reduce the load on databases, allowing them to scale horizontally.</p></li><li><p><strong>TAO (The Associations and Objects)</strong>: Facebook developed <strong>TAO</strong>, a <strong>geographically distributed data</strong> store that caches and manages the social graph (relationships and interactions between users). TAO ensures that frequently accessed data, such as friend lists and likes, are served quickly, improving the overall user experience [<strong><a href="https://engineering.fb.com/2013/06/25/core-infra/tao-the-power-of-the-graph/">Resource</a></strong>]</p></li></ol><h3>Twitter</h3><ol><li><p><strong>Timeline Caching</strong>: Twitter caches <strong>timelines</strong> (<strong>feeds of tweets</strong>) to ensure that users see updates quickly. By caching these timelines, Twitter reduces the need to query the database for every user request, significantly improving response times.</p></li><li><p><strong>Redis for In-Memory Caching</strong>: Twitter uses Redis for various caching purposes, including caching <strong>user sessions</strong>, <strong>trending topics</strong>, and other <strong>frequently accessed data</strong>.</p></li></ol><div><hr></div><p><strong>Thanks</strong> for reading<strong> &#128591;&#127995;</strong>!</p><p>This concludes the first part of our <strong>3-part series</strong> on caching. In the next issue, we will explore key concepts such as <strong>distributed caching</strong>, <strong>types of caching strategies</strong>, and <strong>cache invalidation/eviction methods</strong>.</p><p>Creating each article, along with the accompanying diagrams, takes about <strong>12-14 hours</strong> of research and hard work. If you enjoy my content, please &#10084;&#65039; and subscribe to encourage me to keep writing! &#128591;&#127995;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you like my work, please subscribe to support!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[How to crack any system design interview?]]></title><description><![CDATA[System design interview framework to help beginners crack system design interviews.]]></description><link>https://read.systemdesignblueprint.com/p/system-design-interview-crash-course</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/system-design-interview-crash-course</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 18 Sep 2024 04:01:31 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Designing systems is tough. A system design interview is tougher. With little time to impress, it&#8217;s vital to <strong>stay focused</strong>&nbsp;and avoid getting sidetracked by <strong>unimportant details</strong>. That's why a <strong>structured framework</strong> is essential. It keeps you on track and ensures you emphasize the right aspects of <strong>problem-solving</strong>, <strong>communication</strong>, and <strong>technical skills</strong>.</p><p><strong>Why</strong> does this matter?</p><ul><li><p>It can determine whether you&#8217;re seen as a fit for <strong>junior</strong> or <strong>senior</strong> roles.</p></li><li><p>It also has a direct impact on your compensation.</p></li></ul><p>This article will explore a proven framework. It will help you ask the right questions and focus on what matters in a system design interview. &#9889;&#65039;</p><p>If you&#8217;ve recently subscribed, these articles will help you get up to speed on system design:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/how-to-use-redis-in-system-design-interviews">How to use Redis in system design interviews</a>?</strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/kafka-system-design-interview-guide">How to use Kafka in system design interviews</a></strong>?</p></li><li><p>A crash course on Caching&#8212;<strong><a href="https://read.systemdesignblueprint.com/p/what-is-caching-and-how-it-works">Part 1</a></strong>, <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 2</a></strong> &amp; <strong><a href="https://read.systemdesignblueprint.com/p/caching-challenges-and-solutions-thundering-herd-cache-penetration">Part 3</a></strong></p></li><li><p>A crash course on Rate Limiters<strong> - <a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">Part 1</a> &amp; <a href="https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions">Part 2</a></strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/ep-1-how-to-design-a-payment-system">How to design a payments system</a>?</strong></p></li></ol><p>Before we dive deep, here's a <strong>quick system design cheat sheet</strong> &#129527;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6Iir!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6Iir!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 424w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 848w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 1272w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6Iir!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png" width="1456" height="1432" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1432,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:18239956,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!6Iir!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 424w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 848w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 1272w, https://substackcdn.com/image/fetch/$s_!6Iir!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6d79d0d-4895-4ace-b680-553148b81e3e_9364x9208.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: &#8220;How to answer any system design question&#8221; cheat sheet</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;..</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>Step 1: Clarifying System Design Requirements</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sE2E!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sE2E!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 424w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 848w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 1272w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sE2E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png" width="1456" height="956" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:956,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5674032,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!sE2E!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 424w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 848w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 1272w, https://substackcdn.com/image/fetch/$s_!sE2E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac643a63-db43-411d-9c9a-822021d66a2a_6288x4128.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Requirement clarification</strong></figcaption></figure></div><p>The first step in any system design interview is to grasp the problem. It's vital to ask good questions. They ensure a deep understanding of the system's purpose, users, and limits.</p><h3>1.a Types of requirements in system design</h3><p>A key part of this step is distinguishing between the two broad types of requirements.</p><h4>Functional requirements</h4><ul><li><p><strong>Core features:</strong> What is the primary function of the system?</p></li><li><p><strong>Feature prioritization:</strong> Are there any features that are more critical or time-sensitive?</p></li><li><p><strong>Users:</strong> Who are the main users (e.g., customers, internal teams like developers, DevOps, QA)?</p></li><li><p><strong>User interaction:</strong> How will users interact with the system&#8212;via web, mobile, or API?</p></li><li><p><strong>External integrations:</strong> Does the system need to connect with third-party services or APIs?</p></li><li><p><strong>Business logic:</strong> Must the system follow any specific rules or constraints?</p></li><li><p><strong>Notifications:</strong> Should there be real-time or delayed notifications (eg: emails, push notifications)?</p></li><li><p><strong>Error handling:</strong> What should happen in the case of a system error or failure?</p></li></ul><h4>Non-functional requirements</h4><h5><strong>High-Priority Considerations:</strong></h5><ol><li><p><strong>Scalability:</strong> How many users and requests should the system handle? Plan for future growth.</p></li><li><p><strong>Availability:</strong> Must the system be highly available, or can it have some downtime?</p></li><li><p><strong>Latency:</strong> What are the acceptable latency and throughput for efficient system functioning?</p></li></ol><h5><strong>Low Priority Considerations:</strong></h5><ol><li><p><strong>Consistency:</strong> Can the system tolerate eventual consistency? Or does it need strong consistency?</p></li><li><p><strong>Security</strong>: How critical is securing the APIs and protecting data? Are there specific security requirements or compliance needs?</p></li><li><p><strong>Accuracy</strong>: How important is absolute accuracy in the system? Can it tolerate minor discrepancies, or must it be 100% accurate?</p></li></ol><blockquote><p><strong>Tip&#128161;:</strong> For <strong>senior roles</strong>, emphasize <strong>non-functional requirements</strong> like scalability, availability, and performance. For <strong>junior roles</strong>, focus more on the core<strong> functional requirements</strong> of the system.</p></blockquote><div><hr></div><h3>1.b Capacity Estimation</h3><p>The second sub-step estimates the needed system capacity. It is based on insights from the previous step. This estimate is vital for sound design choices in the next stages of the interview. Capacity estimation helps guide choices around infrastructure, scaling strategies, and performance optimization.</p><p>We can break down capacity estimation into <strong>four key aspects</strong>:</p><ul><li><p><strong>Users</strong></p><ul><li><p>Estimate the number of <strong>monthly</strong> and <strong>daily active users</strong>.</p></li><li><p>Account for <strong>peak activity periods</strong> by estimating the number of users.</p></li></ul></li><li><p><strong>Storage</strong></p><ul><li><p>Estimate <strong>volume</strong> through activity patterns (eg: 2 daily posts for a Twitter-like system).</p></li><li><p>Identify the <strong>type of data</strong> (eg: text, images, videos) that is being stored and the expected duration of storage.</p></li><li><p>Calculate the <strong>total storage capacity</strong> required based on the volume and type of data.</p></li></ul></li><li><p><strong>Requests</strong></p><ul><li><p>Estimate the system's <strong>Queries Per Second (QPS)</strong> based on the number of users and their activity.</p></li><li><p>Provide a <strong>peak QPS</strong> estimate to ensure the system can handle high traffic.</p></li><li><p>Divide QPS into <strong>read </strong>and<strong> write operations</strong> to inform design decisions, such as database architecture and caching.</p></li></ul></li><li><p><strong>Memory</strong></p><ul><li><p>Estimate the <strong>memory required</strong> to store frequently accessed data for rapid retrieval.</p></li></ul></li></ul><p><em><strong>Figure 3</strong></em> below shows a <strong>back-of-the-envelope</strong> calculation for a <strong>Twitter-like system</strong> based on the above aspects<strong> </strong>(These numbers are illustrative, not actual Twitter data).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Iwjd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Iwjd!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 424w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 848w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 1272w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Iwjd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png" width="1456" height="892" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:892,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:895105,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!Iwjd!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 424w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 848w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 1272w, https://substackcdn.com/image/fetch/$s_!Iwjd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F54700748-df2d-4c44-b0ca-a2d948697fda_2002x1226.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Back-of-the-envelope calculations for Twitter-like system</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><div><hr></div><h2>Step 2: Propose a high-level system design</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tP5F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tP5F!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 424w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 848w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 1272w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tP5F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png" width="1456" height="906" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:906,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:7901650,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!tP5F!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 424w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 848w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 1272w, https://substackcdn.com/image/fetch/$s_!tP5F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff9491d6-1bd1-4e33-a5c4-770e22c4dbab_6692x4164.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Sub-steps of high-level design</strong></figcaption></figure></div><p>With the requirements and capacity estimation phase complete, the next step is <strong>designing the system</strong>. This process has <strong>three key parts</strong> <em><strong>(Figure 4)</strong></em>, each of which will be discussed in detail.</p><h3>2.a High level design</h3><p>Create a <strong>simple block diagram</strong>. It should show the key system components. It must outline the high-level flow of data and requests. This starts from the client, goes through the backend, and returns the response. </p><p><em><strong>Figure 5</strong></em> highlights the essential components that should be emphasized in any high-level design.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!v9JC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!v9JC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 424w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 848w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 1272w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!v9JC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png" width="1456" height="709" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:709,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:491857,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!v9JC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 424w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 848w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 1272w, https://substackcdn.com/image/fetch/$s_!v9JC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F693f3003-1ebc-4105-abe7-59f709a16826_3328x1620.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Components in a high-level design diagram</strong></figcaption></figure></div><p>The diagram should include the following components:</p><ul><li><p><strong>Client Applications</strong></p><ul><li><p>Specify if the client is a <strong>mobile app</strong>, <strong>web app</strong>, or an <strong>internal dashboard</strong> for internal teams.</p></li><li><p>If internal teams are the users, the client may be a <strong>CLI tool</strong> within the VCN.</p></li></ul></li><li><p><strong>Content Delivery Networks (CDN)</strong></p><ul><li><p><strong>Use case:</strong> If handling <strong>media storage or streaming</strong>, CDNs improve performance by <strong>caching media</strong> close to users.</p></li><li><p>CDNs can also help limit&nbsp;<strong>rates at the edge</strong> to protect backend services.</p></li></ul></li><li><p><strong>API Gateway</strong></p><ul><li><p><strong>Entry point</strong> for all incoming requests into the VCN, handling routing to respective services.</p></li><li><p>Functions: <strong>Load balancing</strong>, <strong>rate limiting</strong>, and sometimes <strong>authentication</strong> (e.g., <strong>AWS API Gateway</strong>).</p></li></ul></li><li><p><strong>Application Servers</strong></p><ul><li><p>Deploy microservices for specific functionalities (e.g., <strong>user onboarding</strong> and <strong>authentication</strong> services).</p></li><li><p><strong>Bonus</strong>: Briefly mention <strong>Monolith vs Microservices</strong> but focus on microservices, which is the modern approach.</p></li></ul></li><li><p><strong>Database</strong></p><ul><li><p>Specify database type based on the use case.</p></li><li><p>Examples: <strong>MySQL</strong> for <strong>relational data</strong> and <strong>MongoDB</strong> for <strong>unstructured data</strong> in document-oriented databases.</p></li></ul></li><li><p><strong>Caching</strong></p><ul><li><p>Use <strong>Redis</strong> or <strong>Memcached</strong> to reduce database load by caching frequently accessed data.</p></li><li><p><strong>Bonus:</strong> Mention <strong>caching strategies</strong> such as write-through or TTL for cache invalidation.</p></li></ul></li><li><p><strong>Message Queues</strong></p><ul><li><p>Required for <strong>asynchronous communication,</strong> like <strong>OTP</strong> or <strong>email delivery</strong>.</p></li><li><p><strong>Bonus:</strong> Compare <strong>Kafka</strong> (distributed, scalable) vs <strong>RabbitMQ</strong> (traditional messaging) if needed.</p></li></ul></li><li><p><strong>External Services</strong></p><ul><li><p>Outline <strong>external dependencies</strong> like <strong>payment processors</strong> and <strong>alert services</strong>. Note any third-party integrations critical to the core functionality</p></li><li><p><strong>Bonus:</strong> Discuss methods of status updates like <strong>webhooks</strong>, <strong>status APIs</strong>, or <strong>event-driven updates</strong>.</p></li></ul></li><li><p><strong>Bonus Components (Optional)</strong>: If time allows, and you understand them, add these optional components to your block diagram.</p><ul><li><p><strong>Ingress Gateway</strong></p><ul><li><p>Sits at the <strong>edge of the Kubernetes cluster</strong>, directing traffic from the <strong>API gateway</strong> to the microservices.</p></li></ul></li><li><p><strong>Egress Gateway</strong></p><ul><li><p>It handles <strong>outbound requests</strong> from microservices to external services. It sits at the edge of the Kubernetes cluster.</p></li></ul></li></ul></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h3>2.b API Design</h3><p>Designing the API and choosing the right protocols are crucial. They define how system components communicate and how clients access the system's features.</p><p><em><strong>Figure 6</strong></em> shows the key aspects of API design.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-BOg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-BOg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 424w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 848w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 1272w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-BOg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png" width="1456" height="1225" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1225,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:699182,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!-BOg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 424w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 848w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 1272w, https://substackcdn.com/image/fetch/$s_!-BOg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc855b439-4adb-4829-aaea-f604321d11bd_2244x1888.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: API Design</strong></figcaption></figure></div><ul><li><p><strong>Types of Communication Protocols:</strong></p><ul><li><p><strong>HTTPS</strong>: Standard for secure communication in most APIs (eg: REST APIs for user authentication or data fetching).</p></li><li><p><strong>WebSockets</strong>: Ideal for real-time data exchange where low latency is key (eg: live chat, stock market updates).</p></li><li><p><strong>gRPC</strong>: Best for fast, efficient communication between microservices (eg: internal service-to-service calls).</p></li><li><p><strong>Message Protocols</strong>: Use in asynchronous scenarios (eg: Kafka or RabbitMQ for event-driven architectures).</p></li></ul></li><li><p><strong>Types of APIs:</strong></p><ul><li><p><strong>REST:</strong> It is often used for <strong>CRUD operations</strong>, like creating, reading, updating, and deleting user details.</p></li><li><p><strong>GraphQL</strong>: Allows clients to request specific data fields (eg: flexibly fetching custom user profiles).</p></li><li><p><strong>XML</strong>: Suitable for legacy systems or applications needing a strict schema (eg: financial data exchange systems).</p></li></ul></li><li><p><strong>Follow Common Conventions:</strong></p><ul><li><p><strong>Plural Naming</strong>: Use plurals for resource names (eg: <code>/v1/users</code> for multiple users).</p></li><li><p><strong>Versioning</strong>: Use versioning for backward compatibility (eg: <code>/v1/users</code> not <code>/users/v1</code>).</p></li><li><p><strong>Pagination</strong>: Always paginate search or listing APIs to handle large datasets (eg: <code>GET /v1/users?page=5&amp;pageSize=20</code>).</p></li></ul></li><li><p><strong>Idempotency</strong>:</p><ul><li><p>Ensure critical APIs, like <strong>payment processing</strong>, are <strong>idempotent</strong>. This prevents duplicate charges. For example, use an <strong>idempotency key</strong> like <code>{idempotency_key: "abcd-fdfs"}</code> with <code>POST /v1/payment/initiate</code>.</p></li></ul></li><li><p><strong>Data in API Calls</strong>:</p><ul><li><p>Define <strong>request/response body</strong> structures for consistency and ease of use. For example, include user details in the <code>POST /v1/users</code> request body.</p></li><li><p>For use cases such as <strong>fraud detection</strong>, <strong>geolocation</strong> and <strong>IP address</strong> are often required. Pass this data via headers (eg: <code>x-latitude</code> for location and <code>x-ip-address</code> for the user's IP).</p></li><li><p>Mention response status codes clearly:</p><ul><li><p><strong>200 OK</strong>: Request was successful and data is returned.</p></li><li><p><strong>201 CREATED</strong>: A new resource was successfully created.</p></li><li><p><strong>403 FORBIDDEN</strong>: The request is valid, but the server is refusing to fulfill it.</p></li><li><p><strong>302 FOUND</strong>: The requested resource has been temporarily moved to a different URL.</p></li></ul></li></ul></li><li><p><strong>API Segregation:</strong></p><ul><li><p><strong>Public APIs</strong>:</p><ul><li><p>These are open to external clients, often with few restrictions. For example, anyone can create an account via the public <code>POST /v1/signup</code>.</p></li><li><p>Implement rate limiting to prevent abuse. Inform clients of limits via headers (eg: <code>x-rate-limit: 1000</code> for a max of 1000 requests per user).</p></li></ul></li><li><p><strong>Private APIs</strong>:</p><ul><li><p>Require authentication and are accessible only to authorized users. For example, to access sensitive account details, use <code>POST /v1/account</code> with a <code>Authorization: Bearer &lt;token&gt; </code>header.</p></li><li><p>Use JWT-based tokens for managing sessions and validating users securely.</p></li></ul></li><li><p><strong>Internal APIs</strong>:</p><ul><li><p>Internal services or dashboards rely on these APIs for operations not exposed to external clients. For example, <code>GET /v1/users</code> retrieves user data for internal admin dashboards.</p></li><li><p>To ensure that they are not accessible from outside the network, block these at the CDN/API gateway level.</p></li></ul></li></ul></li></ul><div><hr></div><h3>2.c Database Design</h3><p>This step <em><strong>(Figure 7)</strong></em> involves designing the <strong>DB schema</strong> and c<strong>hoosing a DB type</strong>. We must also address concerns about query performance through <strong>normalization, partitioning, </strong>and<strong> caching</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eSsg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eSsg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 424w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 848w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 1272w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eSsg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png" width="1456" height="761" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:761,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:695249,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!eSsg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 424w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 848w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 1272w, https://substackcdn.com/image/fetch/$s_!eSsg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F22e83d0a-8234-48c3-9e87-6e727a0d3d3f_2732x1428.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Database Design</strong></figcaption></figure></div><ul><li><p><strong>Database schema</strong></p><ul><li><p><strong>Table Structure</strong> - Map out the key tables and include important fields. Clearly define the data types for each attribute. Eg: <code>user_id</code> as <code>INT</code>, <code>product_name</code> as <code>VARCHAR</code>.</p></li><li><p><strong>Primary &amp; Foreign Keys</strong> - Specify primary and foreign keys to establish relationships between tables. Eg: <code>product_id</code> as the primary key in <code>Products</code> table and foreign key in <code>Prices</code> table.</p></li><li><p><strong>Indexing</strong>: Identify columns that should be indexed for faster query performance. Eg: index <code>product_name</code> and <code>category_name</code> in an e-commerce data to improve search speed.</p></li><li><p><strong>Denormalization</strong> - Create denormalized tables to avoid complex joins in read-heavy queries. Eg: In an e-commerce app, a denormalized table combining product details, reviews, and prices reduces the need for multiple table joins.</p></li><li><p><strong>Partitioning</strong> - Partition large tables by date or range. Eg: Partition <strong>orders by month</strong> in an online store to improve query performance for recent transactions.</p></li><li><p><strong>Caching Frequently Accessed Data</strong> - Implement caching for commonly queried data like product prices or user profiles to reduce load on the database and improve performance.</p></li></ul></li><li><p><strong>Types of Databases</strong></p><ul><li><p><strong>Relational (SQL)</strong> - Store data in structured tables with rows and columns, useful in case of relations between data. Eg: MySQL, Postgres, CockroachDB (distributed SQL)</p></li><li><p><strong>Non-Relational (NoSQL)</strong> - Flexible schema design; can store various data types. Types of NoSQL DBs:</p><ul><li><p><strong>Key-value</strong> - Simple database where data is stored as key-value pairs. Eg: <strong>Redis</strong> &#8594; used for caching and real-time analytics.</p></li><li><p><strong>Column store</strong> - Store data by columns rather than rows, optimizing read-heavy queries. Eg: <strong>Cassandra</strong> &#8594; Well-suited for building metadata stores in distributed systems; <strong>Clickhouse</strong> &#8594; Optimized for high-performance analytics and data warehousing, ideal for processing large volumes of data quickly.</p></li><li><p><strong>Graph</strong> - Store data as nodes and edges, ideal for complex relationships. Eg: <strong>Neo4j</strong> &#8594; Used for social networks and recommendation engines.</p></li><li><p><strong>Document</strong> - Store data in JSON-like documents; flexible schema. Eg: <strong>MongoDB</strong> &#8594; Popular for applications requiring dynamic data structures.</p></li></ul></li></ul></li></ul><p>Before wrapping up this section, here are some useful tips to follow during the <strong>high-level Level Design</strong> step:</p><blockquote><p><strong>Tips&#128161;:</strong> </p><ol><li><p>Avoid including <strong>irrelevant components</strong>. For eg, in a payment system, there's <strong>no need</strong> to include authentication in the high-level design.</p></li><li><p>At this stage, <strong>avoid </strong>going into details about <strong>individual components</strong>. Allocate time to <strong>dive deeper later on</strong> in the discussion.</p></li><li><p>Note down <strong>key points</strong> to discuss in the next section:</p><ol><li><p>API scaling</p></li><li><p>DB scaling</p></li><li><p>Concurrency</p></li><li><p>Failure scenarios</p></li><li><p>Consistency</p></li></ol></li></ol></blockquote><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><div><hr></div><h2>Step 3: Address Key Issues in the design</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kKFS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kKFS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 424w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 848w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 1272w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kKFS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png" width="1456" height="1013" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1013,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3112629,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!kKFS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 424w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 848w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 1272w, https://substackcdn.com/image/fetch/$s_!kKFS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd5ba0d08-73eb-4f55-917f-f1f8fac9054b_3340x2324.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is the final step which involves identifying and addressing the key challenges the system is likely to face (<em><strong>Figure 8</strong></em>). This phase needs a structured approach to problem-solving. Collaborate with the interviewer to find which areas to explore in depth.</p><h4><strong>Key points to keep in mind:</strong></h4><ol><li><p><strong>Non-Functional Requirements</strong>: Emphasize scalability, performance, reliability, and other non-functional factors based on prior discussions.</p></li><li><p><strong>Collaborate on Focus Areas: </strong>Work with the interviewer to pick specific sections or problems to explore in-depth.</p></li><li><p>Follow a <strong>4-step</strong> <strong>problem-solving</strong> framework to apply to any problem in this section <strong>(</strong><em><strong>Figure 9</strong></em><strong>)</strong>:</p><ul><li><p><strong>Articulate the problem</strong>: Clearly define the challenge being addressed.</p></li><li><p><strong>Generate multiple solutions</strong>: Present at least two potential solutions.</p></li><li><p><strong>Discuss trade-offs: </strong>Compare the pros and cons of each solution. Consider cost, complexity, and scalability.</p></li><li><p><strong>Pick and deep dive</strong>: Choose the most appropriate solution and analyze it in detail with the interviewer.</p></li></ul></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sm6U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sm6U!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 424w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 848w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 1272w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sm6U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png" width="1456" height="382" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:382,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:188819,&quot;alt&quot;:&quot;system design for beginners&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="system design for beginners" title="system design for beginners" srcset="https://substackcdn.com/image/fetch/$s_!sm6U!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 424w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 848w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 1272w, https://substackcdn.com/image/fetch/$s_!sm6U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6eca5421-e514-47b5-b2ff-fd63909e4ae4_1480x388.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 9: 4-step problem solving process</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Please <strong>subscribe</strong> to receive more such system design deep dives every week!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h4><strong>Potential problem areas and solution approaches</strong>:</h4><ul><li><p><strong>Scalability</strong>:</p><ul><li><p><strong>Application Scaling</strong>:</p><ul><li><p>Discuss whether to <strong>scale horizontally</strong> (adding more servers) or <strong>vertically</strong> (upgrading CPU and memory of existing servers).</p></li><li><p><em>Example</em>: In a high-traffic e-commerce platform, horizontal scaling might be preferred to handle spikes during peak sales.</p></li></ul></li><li><p><strong>Database Scaling</strong>:</p><ul><li><p>Add <strong>indexes</strong> to critical columns to improve read performance.</p></li><li><p><strong>Partition</strong> large tables into smaller ones to reduce query time.</p></li><li><p><strong>Normalize</strong> tables to reduce data redundancy, or use denormalization to optimize read-heavy operations.</p></li></ul></li><li><p><strong>Caching</strong>:</p><ul><li><p>Identify <strong>high QPS APIs</strong> where caching can reduce load (eg: frequently accessed product pages in an e-commerce system).</p></li><li><p>Discuss <strong>cache storage options</strong> like <strong>Redis</strong> or <strong>Memcached</strong>, outlining the pros and cons of each.</p></li><li><p>Present different <strong>caching strategies</strong> (eg: write-through, write-back) and select the most appropriate one for the system&#8217;s needs.</p></li><li><p>Define <strong>cache invalidation strategies</strong>, either manual (via code) or automatic with TTL (Time To Live).</p></li></ul></li><li><p><strong>Message Queues</strong>:</p><ul><li><p>Replace <strong>asynchronous communications</strong> with <strong>message queues</strong> to handle task processing efficiently.</p></li><li><p>Discuss strategies to ensure <strong>exactly-once</strong> delivery, critical for <strong>systems</strong> like <strong>payments</strong> (Read <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-3-avoiding-double-payments">Avoid double payments</a></strong> to learn the <strong>key strategies</strong>)</p><ul><li><p><em>Example</em>: In a payment processing system, <strong>idempotency keys</strong> ensure that users are charged only once, even if the request is retried.</p></li></ul></li><li><p>Implement <strong>retry mechanisms</strong> for handling failed deliveries at both producer and consumer sides, ensuring reliability.</p></li></ul></li></ul></li><li><p><strong>Reliability</strong>:</p><ul><li><p><strong>Single Point of Failures (SPOF)</strong>: Identify and eliminate SPOFs by introducing redundancy (eg: multiple load balancers, database replicas).</p></li><li><p><strong>Failure Handling</strong>:</p><ul><li><p>Implement <strong>exponential back-off</strong> strategies for retries, preventing the system from being overwhelmed.</p></li><li><p><strong>Log persistent failures</strong> in a <strong>DB</strong> and retry later using <strong>cron jobs</strong> or background workers until successful completion.</p></li><li><p>Apply <strong>circuit breaker patterns</strong> to prevent <strong>cascading failures</strong>, protecting the system from becoming <strong>overloaded</strong>.</p></li></ul></li><li><p><strong>Data Replication &amp; Disaster Recovery (DR)</strong>:</p><ul><li><p>Discuss strategies to handle <strong>large-scale failures,</strong> such as a cloud service outage (eg: AWS going down).</p></li><li><p>Explore <strong>disaster recovery strategie</strong>s, ensuring high availability through data replication across regions or cloud providers.</p></li></ul></li></ul></li><li><p><strong>Monitoring and Alerting</strong>:</p><ul><li><p>Implement <strong>logging </strong>and<strong> metrics collection</strong> to monitor system health and performance.</p></li><li><p>Use the ELK stack (Elasticsearch, Logstash, Kibana) for <strong>log aggregation</strong> and <strong>analysis</strong>.</p></li><li><p>Discuss how to use OpenTelemetry for collecting traces, offering visibility into application performance and failure points.</p></li><li><p>Define alerting mechanisms to notify teams of critical issues in real-time.</p></li></ul></li></ul><div><hr></div><p>This <strong>3-step template</strong> (with many sub-steps&#128516;) should help. It will let you <strong>tackle</strong> any system design interview question <strong>without fear</strong>!</p><p>The goal is <strong>not</strong> to find a <strong>perfect solution</strong>. It's to show you can <strong>analyze complex problems, make design choices, </strong>and<strong> communicate your reasoning</strong>. &#128516;</p><div><hr></div><p><strong>Thanks for reading &#128591;&#127995;!</strong> </p><p>The details in this article are based on <strong>6+ yrs of practical experience</strong> that has come from designing real systems and seeing problems first hand. If you liked the deep dive, do give a &#10084;&#65039; and <strong>subscribe</strong> to encourage me to publish more such deep dives.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you like my work, please subscribe to support <strong>&#128591;&#127995;</strong></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[What Are Rate Limiters: Key algorithms for better traffic control]]></title><description><![CDATA[A complete guide on rate limiting actions and algorithms for managing API requests]]></description><link>https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/types-of-rate-limiting-algorithms-and-actions</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 11 Sep 2024 04:00:42 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In <strong><a href="https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics">what are rate limiters</a></strong>, we covered two key aspects: <strong>core concept</strong> and <strong>optimal placement</strong>.</p><p>In this post, we'll dive deeper into:</p><ol><li><p>Different types of rate-limiting actions</p></li><li><p>Key rate-limiting algorithms with their pros &amp; cons.</p></li></ol><p>Before we begin, here's a <strong>quick rate-limiting cheat sheet</strong> &#129527;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!NcxA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NcxA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 424w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 848w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 1272w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NcxA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png" width="1456" height="2279" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2279,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1986988,&quot;alt&quot;:&quot;Rate limiting algorithms cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting algorithms cheat sheet" title="Rate limiting algorithms cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!NcxA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 424w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 848w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 1272w, https://substackcdn.com/image/fetch/$s_!NcxA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F453ab841-7036-4231-b04d-88965f179d5f_3222x5044.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Rate-limiting cheat sheet (part 2)</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my s<strong>ystem design diagrams</strong> helpful, please subscribe to receive more of these and support my work &#128591;&#127995;..</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1>Types of rate-limiting actions</h1><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fOYJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fOYJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 424w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 848w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 1272w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fOYJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png" width="450" height="469.1620879120879" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1518,&quot;width&quot;:1456,&quot;resizeWidth&quot;:450,&quot;bytes&quot;:590881,&quot;alt&quot;:&quot;Types of rate limiting actions: Blocking, throttling and shaping&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Types of rate limiting actions: Blocking, throttling and shaping" title="Types of rate limiting actions: Blocking, throttling and shaping" srcset="https://substackcdn.com/image/fetch/$s_!fOYJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 424w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 848w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 1272w, https://substackcdn.com/image/fetch/$s_!fOYJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b8aaa47-5896-4533-a66a-31d475969561_1905x1986.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Types of rate-limiting actions: Blocking, Throttling and Shaping</strong></figcaption></figure></div><p>When building a rate limiter, it&#8217;s crucial to determine whether your use case requires rejecting excess requests outright or handling them with a delayed or lower-quality response. Generally, rate limiters perform one of three actions <em><strong>(Figure 2)</strong></em> on excess requests:</p><h2>What is Blocking in rate limiting?</h2><p>Excess requests exceeding the limit are <em><strong>denied access</strong></em> to the resource. It is commonly expressed as an error message such as HTTP status code 429 (Too Many Requests).</p><blockquote><p><em><strong>Examples:</strong></em></p><ol><li><p>Social media platforms like LinkedIn and Instagram return a 429 error when detecting web scraping attempts to prevent misuse.</p></li><li><p>Too many login attempts on public APIs (gmail login) are rejected to prevent brute-force (DDoS) attacks.</p></li></ol></blockquote><p></p><h2>How does Throttling happen in rate-limiting?</h2><p>At times, responses to requests from certain clients need to be <em><strong>throttled</strong></em> by redirecting them to lower-quality servers. This reduces the strain on high-performance resources while still maintaining service availability.</p><blockquote><p><em><strong>Examples:</strong></em></p><ol><li><p><strong>Streaming platforms</strong> like Hotstar and Netflix reduce stream quality for users who exceed their data cap to manage bandwidth.</p></li><li><p><strong>Cloud storage services</strong> like Google Drive slow down upload speeds for users who surpass their bandwidth limits.</p></li></ol></blockquote><p></p><h2>What is Shaping in rate limiting?</h2><p><em><strong>Shaping</strong></em> involves directing requests from certain clients to a low-priority queue, causing their processing to be intentionally delayed.</p><blockquote><p><em><strong>Examples:</strong></em></p><ol><li><p>During high traffic days, CDNs put additional users in a wait queue, whose requests are processed only after previous users are done processing</p></li><li><p>In <strong>online gaming events</strong>, free-tier users may face delays in match-making as paying subscribers are prioritized for faster access.</p></li></ol></blockquote><p></p><h1>Types of rate-limiting algorithms</h1><h2>Token Bucket Algorithm</h2><h3><strong>How does Token Bucket work?</strong></h3><ol><li><p>Tokens are added to a bucket at a constant rate.</p></li><li><p>Each incoming request consumes one token from the bucket.</p></li><li><p>Requests are denied if the bucket is empty and no tokens are available.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!unOV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!unOV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 424w, https://substackcdn.com/image/fetch/$s_!unOV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 848w, https://substackcdn.com/image/fetch/$s_!unOV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 1272w, https://substackcdn.com/image/fetch/$s_!unOV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!unOV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png" width="250" height="288.51963746223566" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:764,&quot;width&quot;:662,&quot;resizeWidth&quot;:250,&quot;bytes&quot;:97037,&quot;alt&quot;:&quot;Token Bucket Rate Limiting Algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Token Bucket Rate Limiting Algorithm" title="Token Bucket Rate Limiting Algorithm" srcset="https://substackcdn.com/image/fetch/$s_!unOV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 424w, https://substackcdn.com/image/fetch/$s_!unOV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 848w, https://substackcdn.com/image/fetch/$s_!unOV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 1272w, https://substackcdn.com/image/fetch/$s_!unOV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2e60a3b-8074-46ca-8d07-90f779b44e98_662x764.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Token bucket</strong></figcaption></figure></div><h3><strong>Pros &amp; Cons of Token Bucket</strong></h3><p>&#9989; Simple to implement and understand.<br>&#9989; Accommodates short-term request spikes up to bucket capacity.<br>&#10060; Doesn&#8217;t ensure a perfectly smooth request.</p><div><hr></div><h2>Leaky Bucket Algorithm</h2><h3><strong>How does </strong>Leaky Bucket <strong>work?</strong></h3><ol><li><p>Requests are added to a fixed-size queue and processed at a steady rate.</p></li><li><p>Requests are discarded if the queue is full.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!m7M0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!m7M0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 424w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 848w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 1272w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!m7M0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png" width="250" height="311.09324758842445" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:774,&quot;width&quot;:622,&quot;resizeWidth&quot;:250,&quot;bytes&quot;:95124,&quot;alt&quot;:&quot;Leaky Bucket Rate Limiting Algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Leaky Bucket Rate Limiting Algorithm" title="Leaky Bucket Rate Limiting Algorithm" srcset="https://substackcdn.com/image/fetch/$s_!m7M0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 424w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 848w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 1272w, https://substackcdn.com/image/fetch/$s_!m7M0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe796bca9-b8b2-4c19-a99b-bda9de9046a9_622x774.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Leaky Bucket</strong></figcaption></figure></div><p></p><h3><strong>Pros &amp; Cons of Leaky Bucket</strong></h3><p><br>&#9989; Requests are processed steadily, preventing sudden bursts from overwhelming the system.<br>&#10060; Slightly more complex to implement compared to Token Bucket.</p><div><hr></div><h2>Fixed Window Counter Algorithm</h2><h3><strong>How does Fixed Window Counter work?</strong></h3><ol><li><p>Stores the number of requests in the current window and limits them (eg: 100 requests per minute).</p></li><li><p>When a new request arrives, it's either accepted or rejected, depending on whether the current window can accommodate it.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DFGw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DFGw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 424w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 848w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 1272w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DFGw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png" width="266" height="282.5732087227414" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:682,&quot;width&quot;:642,&quot;resizeWidth&quot;:266,&quot;bytes&quot;:44651,&quot;alt&quot;:&quot;Fixed Window Counter Rate Limiting Algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Fixed Window Counter Rate Limiting Algorithm" title="Fixed Window Counter Rate Limiting Algorithm" srcset="https://substackcdn.com/image/fetch/$s_!DFGw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 424w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 848w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 1272w, https://substackcdn.com/image/fetch/$s_!DFGw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9a758e30-8cbc-4c1d-9cd4-d9805c5fdeb9_642x682.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Fixed Window Counter</strong></figcaption></figure></div><h3><strong>Pros &amp; Cons of Fixed Window Counter</strong></h3><p>&#9989; Simple to understand, and clear rate limits for each time window.<br>&#10060; Poor handling of boundary bursts; can allow twice the rate of requests at edges (shown in Figure 5).</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VkjW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VkjW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 424w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 848w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 1272w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VkjW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png" width="282" height="228.5016077170418" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:504,&quot;width&quot;:622,&quot;resizeWidth&quot;:282,&quot;bytes&quot;:57052,&quot;alt&quot;:&quot;Boundary bursting problem in fixed window counter rate limiting algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Boundary bursting problem in fixed window counter rate limiting algorithm" title="Boundary bursting problem in fixed window counter rate limiting algorithm" srcset="https://substackcdn.com/image/fetch/$s_!VkjW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 424w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 848w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 1272w, https://substackcdn.com/image/fetch/$s_!VkjW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a3608a7-7ec9-4ecc-9c27-1c28f541dddf_622x504.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Boundary bursting in Fixed Window Counter</strong></figcaption></figure></div><div><hr></div><h2>Sliding Window Log Algorithm</h2><p>This is the <strong>most commonly used</strong> rate-limiting algorithm.</p><h3><strong>How does </strong>Sliding Window Log<strong> work?</strong></h3><ol><li><p>Records the timestamp of each incoming request.</p></li><li><p>Upon a new request, expired timestamps from previous windows are cleared.</p></li><li><p>The number of requests in the current window is counted. If it&#8217;s below the allowed limit, the request is accepted and logged; otherwise, it&#8217;s denied.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CJMH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CJMH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 424w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 848w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 1272w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CJMH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png" width="282" height="278.63283582089554" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:662,&quot;width&quot;:670,&quot;resizeWidth&quot;:282,&quot;bytes&quot;:75616,&quot;alt&quot;:&quot;Sliding Window Log Rate Limiting Algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Sliding Window Log Rate Limiting Algorithm" title="Sliding Window Log Rate Limiting Algorithm" srcset="https://substackcdn.com/image/fetch/$s_!CJMH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 424w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 848w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 1272w, https://substackcdn.com/image/fetch/$s_!CJMH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5d9475a-c46e-4513-a023-54f3ad29e530_670x662.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Sliding Window Log</strong></figcaption></figure></div><h3><strong>Pros &amp; Cons of </strong>Sliding Window Log</h3><p>&#9989; Precise and smooth rate-limiting with no edge cases.<br>&#10060; Memory-intensive for high-volume APIs as it&nbsp;requires storing all timestamps.</p><div><hr></div><h2>Sliding Window Counter Algorithm</h2><h3><strong>How does </strong>Sliding Window Counter<strong> work?</strong></h3><ol><li><p>This approach combines Fixed Window Counter and Sliding Window Log algorithms.</p></li><li><p>Instead of recording every request timestamp, only the total number of requests in the previous fixed window is logged.</p></li><li><p>When a new request arrives, the &#8220;weighted number of requests&#8221; for the current window is calculated. For example (<em><strong>Figure 7</strong></em>):</p><ol><li><p>Assume<strong> Window size:</strong> 1 min, <strong>Window capacity:</strong> 4 requests/min, and a new request arrives at <strong>t=2:15</strong></p></li><li><p>The current window (1:15 - 2:15) consists of 75% of the previous window (1:15 - 2:00) and 25% of the current window (2:00 - 2:15).</p></li><li><p>Instead of tracking all timestamps, only the request count in the previous fixed window is logged.</p></li><li><p>The <strong>&#8220;weighted number of requests&#8221;</strong> is calculated as: <code>requestsInCurrentWindow + (weight * requestsInPreviousWindow) = 2 + (0.75 * 3) = 4.25</code>.</p></li><li><p>Since adding the new request would bring the total to 4.25 (exceeding the limit of 4), the request is rejected.</p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FHAO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FHAO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 424w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 848w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 1272w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FHAO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png" width="448" height="331.57588357588355" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:712,&quot;width&quot;:962,&quot;resizeWidth&quot;:448,&quot;bytes&quot;:114762,&quot;alt&quot;:&quot;Sliding Window Counter Rate Limiting Algorithm&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Sliding Window Counter Rate Limiting Algorithm" title="Sliding Window Counter Rate Limiting Algorithm" srcset="https://substackcdn.com/image/fetch/$s_!FHAO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 424w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 848w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 1272w, https://substackcdn.com/image/fetch/$s_!FHAO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e51bdfb-df57-41c6-907e-caeb2fa704e4_962x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Sliding Window Counter</strong></figcaption></figure></div><h3><strong>Pros &amp; Cons of </strong>Sliding Window Counter</h3><p>&#9989; More accurate than Fixed Window Counter<br>&#9989; More memory-efficient than Sliding Window Log because it only tracks the total number of requests per window, rather than storing individual timestamps for each request.<br>&#9989; Smooth out edges between windows.<br>&#10060; Slightly complex to implement.</p><div><hr></div><p>Thanks for reading! I hope this helped clarify the different types of actions and algorithms used in designing a rate limiter. However, two key factors to consider, based on your use case are:</p><ol><li><p>Choosing the right rate-limiting algorithm will depend on the specific behavior and needs of your system.</p></li><li><p>The data structure used to store rate limiter data should be chosen with efficiency and scalability in mind.</p></li></ol><p><br>See ya in the next one &#128075;&#127995;!</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you like my work, please subscribe to support &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[What Are Rate Limiters: Core Concepts, Benefits, and Placement Strategies]]></title><description><![CDATA[Rate limiters control API traffic. Learn key benefits, core concepts, and ideal placements for better performance]]></description><link>https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/what-are-rate-limiters-basics</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Sun, 08 Sep 2024 04:01:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1>What is Rate Limiting?</h1><p><strong>Rate limiting</strong> is like a traffic cop for the internet, controlling how often users can perform actions. It prevents server overloads and adds security by stopping malicious activity, ensuring smooth system performance.</p><p>Before moving ahead, here's a<strong> rate-limiting cheat sheet</strong> &#129527;.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iYFp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iYFp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 424w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 848w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 1272w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iYFp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png" width="1456" height="1881" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1881,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1755686,&quot;alt&quot;:&quot;Rate limiting cheat sheet&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting cheat sheet" title="Rate limiting cheat sheet" srcset="https://substackcdn.com/image/fetch/$s_!iYFp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 424w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 848w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 1272w, https://substackcdn.com/image/fetch/$s_!iYFp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F68a0d958-c959-4f11-a0d9-5c1524e004d5_3224x4164.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Rate Limiting cheat sheet (Part 1)</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my s<strong>ystem design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>If you&#8217;ve recently subscribed, these articles will help you get up to speed on system design:</p><ol><li><p><strong><a href="https://read.systemdesignblueprint.com/p/system-design-interview-crash-course">System design interview crash course</a></strong></p></li><li><p><strong><a href="https://read.systemdesignblueprint.com/p/ep-10-what-are-rate-limiters-part">A crash course on rate limiting (Part 2)</a></strong></p></li><li><p>A crash course on Caching&#8212;<strong><a href="https://systemdesignnewsletter.substack.com/p/ep-13-a-crash-course-on-caching-part">Part 1</a></strong>, <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-14-a-crash-course-on-caching-part">Part 2</a></strong> &amp; <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-15-a-crash-course-on-caching-final">Part 3</a></strong></p></li><li><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-16-how-to-use-redis-in-system">How to use Redis in system design interviews</a>?</strong></p></li></ol><h2>Benefits of Rate Limiting</h2><ul><li><p><strong>Prevent server overload:</strong> A rate limiter blocks excessive requests to protect servers from Distributed Denial of Service (DDoS) attacks.</p></li><li><p><strong>Prevent resource starvation:</strong> It ensures fair access to resources by capping the number of requests users can make, avoiding resource starvation.</p></li><li><p><strong>Reduce costs:</strong> By controlling traffic, rate limiting helps cut down on unnecessary usage, ultimately lowering operational costs.</p></li></ul><h2><strong>Designing Rate Limiters: Key Concepts and Principles</strong></h2><p>Designing a rate limiter involves grasping these essential concepts:</p><ol><li><p><strong>Identifier:</strong> Distinguishes individual users or systems, typically using <strong>unique identifiers</strong> like IP addresses or user IDs.</p></li><li><p><strong>Window:</strong> Specifies the <strong>period</strong> during which requests are counted (e.g., a minute or an hour).</p></li><li><p><strong>Limit:</strong> Sets the <strong>maximum</strong> <strong>number of requests</strong> allowed within the defined time window.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!YBDY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!YBDY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 424w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 848w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 1272w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!YBDY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png" width="1424" height="1002" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1002,&quot;width&quot;:1424,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:417374,&quot;alt&quot;:&quot;Core concept behind rate limiting system design&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Core concept behind rate limiting system design" title="Core concept behind rate limiting system design" srcset="https://substackcdn.com/image/fetch/$s_!YBDY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 424w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 848w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 1272w, https://substackcdn.com/image/fetch/$s_!YBDY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99acc0c3-b104-446b-bb6f-a66f5e48590c_1424x1002.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: The core concept of Rate Limiter design</strong></figcaption></figure></div><p><em><strong>Figure 2</strong></em> illustrates this concept with relevant examples:</p><blockquote><p><em><strong>Ex 1</strong></em><strong>:</strong> Twitter limits the <strong>number of tweets</strong> <strong>per user</strong> to <strong>300</strong> per <strong>3 hours </strong>&#8594;<strong> <br></strong>[<strong>Identifier: </strong>user_id, <strong>Window:</strong> 3 hours, <strong>Limit:</strong> 300 tweets<strong>]</strong></p></blockquote><blockquote><p><em><strong>Ex 2</strong></em><strong>:</strong> Users can create <strong>10 accounts</strong> per <strong>day</strong> per <strong>IP address</strong> &#8594; <br><strong>[Identifier:</strong> user_id_ip_address, <strong>Window:</strong> 1 day, <strong>Limit:</strong> 10 accounts<strong>]</strong></p></blockquote><h2>Where should rate limiters be placed? strategic points</h2><p>The next key question is: where should rate limiters be implemented?</p><p>This depends on the specific use case. For example, if rate limiting is needed for database queries, it would be placed at the database level. On the other hand, for blocking excessive traffic on high-traffic days, rate limiting could be applied at the CDN or API gateway. </p><p>Broadly, there are six key points in the infrastructure stack where rate limiters can be deployed. Let&#8217;s dive into each of them:</p><h3>Rate limiting at Application Servers</h3><p>This is a common rate-limiting strategy because many rate-limiting needs are custom and often require integration directly into the application code.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5s3z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5s3z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 424w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 848w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 1272w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5s3z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png" width="212" height="129.2967032967033" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:222,&quot;width&quot;:364,&quot;resizeWidth&quot;:212,&quot;bytes&quot;:11945,&quot;alt&quot;:&quot;Rate limiting at application servers&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at application servers" title="Rate limiting at application servers" srcset="https://substackcdn.com/image/fetch/$s_!5s3z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 424w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 848w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 1272w, https://substackcdn.com/image/fetch/$s_!5s3z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff6b243e6-95f7-4bfd-be5f-9a8ab35d2341_364x222.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em>1.</em> <em><strong>Social Media:</strong> </em><strong>Twitter</strong> limits the number of tweets per user to 300 per 3 hours.<br><em>2.&nbsp;<strong>Live Streaming:</strong> </em><strong>Hotstar</strong> uses custom rate limiting to prevent abuse of the free tier by restricting unlimited access.</p></blockquote><div><hr></div><h3>Rate limiting at the CDN layer</h3><pre><code><strong>&#128161;&#128161;</strong> A <strong>Content Delivery Network</strong> (<strong>CDN</strong>) is a network of distributed servers that caches and delivers web content closer to users, speeding up load times and improving performance.

<strong>Eg:</strong> When you watch a video on YouTube, it uses a CDN to deliver videos from servers near you, reducing buffering and improving streaming quality.</code></pre><p>Rate limiting at the CDN level manages traffic at the edge, blocking excessive requests before they reach your main infrastructure.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tIqP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tIqP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 424w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 848w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 1272w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tIqP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png" width="370" height="109.41309255079007" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:262,&quot;width&quot;:886,&quot;resizeWidth&quot;:370,&quot;bytes&quot;:35950,&quot;alt&quot;:&quot;Rate limiting at CDN layer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at CDN layer" title="Rate limiting at CDN layer" srcset="https://substackcdn.com/image/fetch/$s_!tIqP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 424w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 848w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 1272w, https://substackcdn.com/image/fetch/$s_!tIqP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe327c086-f26b-4a2d-bdf9-9fa2b5d8cf96_886x262.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em>1.&nbsp;<strong>E-commerce:</strong></em> Control new user onboarding during high-traffic days by placing extra users in a CDN wait queue.</p></blockquote><div><hr></div><h3>Rate Limiting at the API Gateway</h3><pre><code>&#128161;&#128161; An <strong>API gateway</strong> is a server at the front of your infrastructure that manages incoming client requests and routes them to the appropriate services within your application.

<strong>Eg:</strong> In a shopping app, the API gateway handles requests for product information, user accounts, and orders, directing them to the appropriate backend services.</code></pre><p>Rate limiting at the API gateway level controls traffic between <strong>client</strong> and <strong>backend</strong> services.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ikwq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ikwq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 424w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 848w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 1272w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ikwq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png" width="380" height="124.13965087281795" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:262,&quot;width&quot;:802,&quot;resizeWidth&quot;:380,&quot;bytes&quot;:22776,&quot;alt&quot;:&quot;Rate limiting at API gateway layer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at API gateway layer" title="Rate limiting at API gateway layer" srcset="https://substackcdn.com/image/fetch/$s_!Ikwq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 424w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 848w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 1272w, https://substackcdn.com/image/fetch/$s_!Ikwq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8483f0f3-bf55-471e-be7e-8e7ff339d2e4_802x262.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em>1. <strong>SaaS:</strong> </em>Limiting free users to 1000 API req/day, while enterprise users get 100K req/day<br><em>2.&nbsp;<strong>Social Media:</strong> </em>Instagram blocks repeated scraping attempts from same IP or location.</p></blockquote><div><hr></div><h3>Rate limiting at the Ingress Gateway level</h3><pre><code>&#128161;&#128161; An <strong>Ingress Gateway</strong> manages and routes external traffic into a Kubernetes cluster, handling tasks like load balancing and SSL termination.

It&#8217;s advantageous for efficiently directing traffic to different services, such as routing <strong>/user</strong> requests to one service and <strong>/payment</strong> requests to another.</code></pre><p>Rate limiting at Ingress Gateway controls traffic between API gateway and ingress gateway <strong>OR</strong> between internal services as shown in the diagram</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0FNO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0FNO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 424w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 848w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 1272w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0FNO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png" width="366" height="148.10894941634243" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:416,&quot;width&quot;:1028,&quot;resizeWidth&quot;:366,&quot;bytes&quot;:42123,&quot;alt&quot;:&quot;Rate limiting at ingress gateway layer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at ingress gateway layer" title="Rate limiting at ingress gateway layer" srcset="https://substackcdn.com/image/fetch/$s_!0FNO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 424w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 848w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 1272w, https://substackcdn.com/image/fetch/$s_!0FNO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8092f950-ffff-4c89-97fd-4b0556fb850e_1028x416.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em><strong>1.</strong></em><strong> </strong><em><strong>Internal dashboards:</strong></em> If both client applications and internal dashboards access the User Details API, rate limiting at the ingress gateway can <strong>prioritize</strong> requests from <strong>client-facing applications</strong> while <strong>restricting</strong> access for <strong>internal dashboards</strong>.<br><em>2. <strong>E-commerce:</strong> </em>Limits requests between Order Service and Inventory Service to prevent overload.</p></blockquote><div><hr></div><h3>Managing traffic with rate limiting at the database </h3><p>Rate limiting between Application servers and Database servers protects the DB from overload by limiting heavy query traffic.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pCyZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pCyZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 424w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 848w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 1272w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pCyZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png" width="310" height="137.09163346613545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:222,&quot;width&quot;:502,&quot;resizeWidth&quot;:310,&quot;bytes&quot;:12949,&quot;alt&quot;:&quot;Rate limiting at database layer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at database layer" title="Rate limiting at database layer" srcset="https://substackcdn.com/image/fetch/$s_!pCyZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 424w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 848w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 1272w, https://substackcdn.com/image/fetch/$s_!pCyZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f098206-a7c9-4081-b7fe-87894b0fef2d_502x222.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em>1.</em> Limiting&nbsp;the no. of <em><strong>simultaneous DB connections</strong></em> to ensure fair access for all application replicas.</p></blockquote><div><hr></div><h3>Rate limiting at Egress Gateway for outbound control</h3><pre><code>&#128161;&#128161; An <strong>Egress Gateway</strong> manages and routes outgoing traffic from a Kubernetes cluster, handling tasks like rate limiting and connection monitoring.

It&#8217;s advantageous for controlling traffic to external services, such as enforcing security policies for outbound connections and managing bandwidth usage effectively.</code></pre><p>Third-party services often enforce their rate limits, causing excess requests to fail. Rate limiting at the Egress Gateway helps prevent this by capping outgoing requests and managing network resources effectively.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!B7yu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!B7yu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 424w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 848w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 1272w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!B7yu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png" width="372" height="147.59164733178653" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:342,&quot;width&quot;:862,&quot;resizeWidth&quot;:372,&quot;bytes&quot;:29525,&quot;alt&quot;:&quot;Rate limiting at egress gateway layer&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="Rate limiting at egress gateway layer" title="Rate limiting at egress gateway layer" srcset="https://substackcdn.com/image/fetch/$s_!B7yu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 424w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 848w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 1272w, https://substackcdn.com/image/fetch/$s_!B7yu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F53bbec11-0ae0-4e9b-9c85-64f1fe103589_862x342.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><strong>Examples:</strong><br><em>1. <strong>Oauth:</strong> </em>Google&nbsp;caps the number of OAuth requests from a third party during high-traffic login periods.</p></blockquote><div><hr></div><h2>Conclusion</h2><p>Thanks for reading! In this post, we&#8217;ve covered why Rate Limiting is important, its core concepts, and where to place rate limiters in your infrastructure. It&#8217;s important to:</p><ol><li><p>Understand the scope of the rate-limiting problem to determine the appropriate infrastructure layer for implementation.</p></li><li><p>Communicate rate limits clearly to API users, ideally through response headers, to enable effective retry and back-off strategies.</p></li></ol><p>In the next post, we&#8217;ll explore key rate-limiting algorithms and how to choose the right one for your needs, accompanied by a cheat sheet &#128521;. </p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you like my work, please subscribe to support &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p><strong>Liked</strong> this article? Make sure to &#128153; click the like button.</p><p><strong>Feedback</strong> or addition? Make sure to &#128172; <strong>comment</strong>.</p><p>Know someone who would find this helpful? Make sure to &#128257; <strong>share</strong> this post.</p>]]></content:encoded></item><item><title><![CDATA[EP 9 | What are Load Balancers ⚖️?]]></title><description><![CDATA[Over the years, load balancers have become an essential pillar of system architecture, serving as the front-line controllers that intelligently distribute incoming network traffic across multiple servers.]]></description><link>https://read.systemdesignblueprint.com/p/ep-9-load-balancers-101</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-9-load-balancers-101</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 04 Sep 2024 19:37:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!6e4v!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>Over the years, load balancers have become an essential pillar of system architecture, serving as the front-line controllers that intelligently distribute incoming network traffic across multiple servers.</p><p>Similar to an air traffic controller coordinating the safe landing of planes by assigning them to available runways, load balancers regulate data flow within a system, ensuring horizontal scalability and optimal resource utilization.</p><p>Before we dive deeper, here's a <strong>quick cheat sheet</strong> summarizing today&#8217;s article &#129527;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6e4v!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6e4v!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 424w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 848w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6e4v!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg" width="1456" height="2035" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2035,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:9262991,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6e4v!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 424w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 848w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!6e4v!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbad64c55-35df-432e-aa46-9d75f9867bcc_11725x16384.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Load Balancer cheat sheet!</strong></figcaption></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Remember to save the diagram&#8212;it's a great resource for interviews! If you find my <strong>system design diagrams</strong> helpful, please <strong>subscribe</strong> to receive more of these and support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Let&#8217;s understand each component in detail:</p><h3>Functions of a load balancer</h3><ol><li><p><strong>Distribute Traffic</strong>: Load balancers evenly distribute incoming requests across multiple servers to prevent overloading any single server.</p></li><li><p><strong>Fault Tolerance</strong>: They detect server failures and redirect traffic to healthy servers, ensuring continuous service availability.</p></li><li><p><strong>Rate Limiting</strong>: Load balancers can control traffic flow by limiting the number of requests sent to servers, preventing overload.</p></li><li><p><strong>Session Persistence</strong>: They maintain session data by consistently routing a user&#8217;s requests to the same server for the duration of a session.</p></li></ol><h3>Types of Load Balancers</h3><ol><li><p><strong>Layer 4</strong>: Operates at the <strong>transport layer</strong>, distributing traffic based on <strong>IP addresses</strong> and <strong>TCP/UDP ports</strong>. Eg: HAProxy, AWS ELB</p></li><li><p><strong>Layer 7</strong>: Works at the <strong>application layer</strong>, routing traffic based on content such as <strong>HTTP headers</strong> and <strong>URLs</strong>. Eg: Nginx, Traefik, AWS ALB</p></li><li><p><strong>Cloud-Based</strong>: Load balancers hosted in the cloud, offering scalable, <strong>on-demand traffic management</strong> without hardware overhead. Eg: Cloudflare, AWS Route 53</p></li><li><p><strong>Hardware</strong>: Physical load balancers deployed <strong>on-premises, providing high-performance traffic distribution</strong> with dedicated infrastructure. Eg: F5, A10, Citrix ADC</p></li></ol><h3>Types of load balancing algorithms</h3><h4>1. Round Robin</h4><p><strong>How does it work?</strong><br>Requests are distributed sequentially across servers, looping back to the first server after reaching the last one. </p><p>In <em><strong>Figure 2</strong></em>, requests 1, 2, 3 and 4 go to servers 1, 2, 3 and 1 respectively.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!M9cK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!M9cK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 424w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 848w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 1272w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!M9cK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png" width="270" height="208.01020408163265" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e114e987-2271-4747-8013-3c7ab87a2b17_784x604.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:604,&quot;width&quot;:784,&quot;resizeWidth&quot;:270,&quot;bytes&quot;:61817,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!M9cK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 424w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 848w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 1272w, https://substackcdn.com/image/fetch/$s_!M9cK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe114e987-2271-4747-8013-3c7ab87a2b17_784x604.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Round Robin</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Simple to implement and effective for servers with equal capacity.<br>&#10060; Doesn't account for server load or processing power.</p><blockquote><p><strong>Example:</strong> Default algorithm used by <em><strong>Kubernetes</strong></em><strong>&nbsp;</strong><em><strong>Load Balancer</strong></em> for routing traffic to service pods.</p></blockquote><div><hr></div><h4>2. Sticky Round Robin</h4><p><strong>How does it work?</strong><br>The first client request goes to a random server, and all future requests stick to that server unless it's unavailable.</p><p>In <em><strong>Figure 3</strong></em>, requests 1 &amp; 2 go to server 1 while requests 3 &amp; 4 go to server 2</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IEPO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IEPO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 424w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 848w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 1272w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IEPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png" width="270" height="208.01020408163265" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:604,&quot;width&quot;:784,&quot;resizeWidth&quot;:270,&quot;bytes&quot;:61395,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IEPO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 424w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 848w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 1272w, https://substackcdn.com/image/fetch/$s_!IEPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fada66803-b8a4-48f5-99b3-1eb73c6dcfe9_784x604.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Sticky Round Robin</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Ensures session persistence, ideal for applications requiring user state retention.<br>&#10060; Not suitable for applications with short-lived sessions.<br>&#10060; If a server goes down, session data can be lost unless state is stored centrally.</p><blockquote><p><strong>Example:</strong> <em><strong>Redis Cluster</strong> </em>uses this to direct a client&#8217;s requests to the same Redis node, ensuring consistent access to cached data.</p></blockquote><div><hr></div><h4>3. Weighted Round Robin</h4><p><strong>How does it work?</strong><br>Servers are assigned weights based on their capacity, with higher-weight servers handling more requests.</p><p>In <em><strong>Figure 4</strong></em>, Out of 4 requests, 3 requests (0.8 * 4 = 3.2) go to server 1 and 1 request (0.1 * 4 = 0.4) goes to server 2.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ehlo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ehlo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 424w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 848w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 1272w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ehlo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png" width="290" height="207.7403846153846" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1043,&quot;width&quot;:1456,&quot;resizeWidth&quot;:290,&quot;bytes&quot;:5688839,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ehlo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 424w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 848w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 1272w, https://substackcdn.com/image/fetch/$s_!Ehlo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92dd8ccb-9f9c-433f-908f-b49572c5a5c5_16384x11732.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Weighted Round Robin</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Balances load based on server capacity.<br>&#9989; More efficient for systems with servers of varying strength.<br>&#10060; Doesn&#8217;t factor in real-time load (active connections).</p><blockquote><p><strong>Example:</strong> <em><strong>AWS Elastic Load Balancer</strong></em> uses this to direct more traffic to EC2 instances with higher capacity and computing power.</p></blockquote><div><hr></div><h4>4. IP Hash</h4><p><strong>How does it work?</strong><br>Calculates a hash value from the client&#8217;s IP address and uses it to determine the server to route the request.</p><p>In <em><strong>Figure 5</strong></em>, requests 1 &amp; 2 are from same IP in China, hence go to server 1 where hash(IP)=0, while requests 3 &amp; 4 from same IP in India go to server 3 where hash(IP)=2</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CJOs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CJOs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CJOs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png" width="284" height="206.75824175824175" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1060,&quot;width&quot;:1456,&quot;resizeWidth&quot;:284,&quot;bytes&quot;:6701289,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CJOs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!CJOs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ea112b-0595-4e43-90df-56cfaebcfd02_16384x11931.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 5: IP hash based LB</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Ensures consistent routing of requests from the same client to the same server.<br>&#9989; Good for session persistence without needing cookies.<br>&#10060; Can lead to uneven load distribution if many clients share similar IP addresses.<br>&#10060; IP changes can break persistence.</p><blockquote><p><strong>Example:</strong> <em><strong>E-commerce</strong></em> platforms like Amazon use this to ensure that&nbsp;the <strong>same server</strong> handles<em> <strong>all cart requests for a user</strong></em>, such as adding, viewing, or removing items from the cart.</p></blockquote><div><hr></div><h4>5. Least connections</h4><p><strong>How does it work?</strong><br>Incoming requests are directed to the server with the fewest active connections.</p><p>In <em><strong>Figure 6</strong></em>, all requests go to server 2 because it has the least no of client connections (50)</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LAPJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LAPJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LAPJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png" width="290" height="211.12637362637363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1060,&quot;width&quot;:1456,&quot;resizeWidth&quot;:290,&quot;bytes&quot;:5769435,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LAPJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!LAPJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F46e32c71-a1f3-4640-9c2a-f700f42cd003_16384x11931.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 6: Least connections based LB</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Balances real-time traffic load efficiently.<br>&#9989; Prevents servers from becoming overloaded.<br>&#10060; Doesn&#8217;t account for server capacity or request complexity.</p><blockquote><p><strong>Example:</strong> <em><strong>PgBouncer</strong></em> distributes queries across PostgreSQL instances based on <em><strong>least connections</strong></em>, preventing overload on any single instance.</p></blockquote><div><hr></div><h4>6. Least Response Time</h4><p><strong>How does it work?</strong><br>Incoming requests are routed to the server with the fastest response time.</p><p>In <em><strong>Figure 7</strong></em>, all requests go to server 1 because it has the least current response time (1ms)</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sIpY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sIpY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sIpY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png" width="290" height="211.12637362637363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1060,&quot;width&quot;:1456,&quot;resizeWidth&quot;:290,&quot;bytes&quot;:5922114,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sIpY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 424w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 848w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 1272w, https://substackcdn.com/image/fetch/$s_!sIpY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff4117720-c43c-430d-9efd-d660f526c07f_16384x11931.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 7: Least Response Time based LB</strong></figcaption></figure></div><p><strong>Pros &amp; Cons<br></strong>&#9989; Reduces latency for time-sensitive applications.<br>&#10060; May lead to instability if servers experience temporary performance spikes.</p><blockquote><p><strong>Example:</strong> <em><strong>Booking platforms</strong></em> (eg: Expedia<em>)</em>&nbsp;use this at the API/ingress gateway level to route travel booking requests to the fastest available server.</p></blockquote><div><hr></div><p>So far so good. I will publish separate deep-dive diagrams on load balancers in upcoming posts!</p><p>Also, the next post will be about something exciting and a bit more challenging &#128521;! Stay tuned and don&#8217;t forget to subscribe to support my work &#128591;&#127995;.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! If you find my work useful, please subscribe to support my work &#128591;&#127995;.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 8 | How does VISA make money?]]></title><description><![CDATA[A breakdown of how VISA makes 10 billion dollars from card transactions.]]></description><link>https://read.systemdesignblueprint.com/p/ep-8-how-does-visa-earn-from-credit</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-8-how-does-visa-earn-from-credit</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Thu, 29 Aug 2024 13:15:31 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!eclu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>Let&#8217;s talk credit cards today. In every <strong>credit card</strong> transaction, the merchant is charged a <strong>Merchant Discount Fee</strong> (MDR) of <strong>2-3%</strong>, which is divided among the issuer, acquirer, POS provider, and card networks. <em><strong>Figure 1</strong></em> illustrates how this MDR is split among the various parties involved.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eclu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eclu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 424w, https://substackcdn.com/image/fetch/$s_!eclu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 848w, https://substackcdn.com/image/fetch/$s_!eclu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 1272w, https://substackcdn.com/image/fetch/$s_!eclu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eclu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png" width="1456" height="1197" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1197,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:435614,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eclu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 424w, https://substackcdn.com/image/fetch/$s_!eclu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 848w, https://substackcdn.com/image/fetch/$s_!eclu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 1272w, https://substackcdn.com/image/fetch/$s_!eclu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F17093d46-6cfe-442d-8eb4-9020eca5258b_2024x1664.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">If you like my system design diagrams, please subscribe to receive new posts and support my work &#128591;&#127995;</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p><strong><a href="https://systemdesignnewsletter.substack.com/p/ep-7-what-happens-when-you-swipe">Previous issue</a></strong> talks about the BTS actions after you swipe your credit card at a merchant&#8217;s terminal. Today, we&#8217;ll dive into the <strong>revenue breakdown</strong>, revealing how each party involved in a credit card transaction earns from the fees.</p><ol><li><p><strong>Acquirer Markup</strong></p><ol><li><p>Typically ranges between <strong>0.5% to 0.75%</strong> of the transaction amount.</p></li><li><p>This is split between the <strong>Acquirer</strong> and the <strong>POS provider</strong>.</p></li><li><p>In cases where the POS provider is also the acquirer (eg: <strong>Paytm</strong>), the same party retains the full markup.</p></li></ol></li><li><p><strong>Interchange Fee</strong></p><ol><li><p>Usually between <strong>1% to 1.25%</strong> of the transaction value.</p></li><li><p>Paid by the <strong>Acquirer</strong> to the <strong>Card Issuer</strong>.</p></li><li><p>The <strong>Issuer</strong> earns the largest share since it assumes the risk by paying the merchant, even if the cardholder defaults.</p></li></ol></li><li><p><strong>Network and Assessment Fee</strong></p><ol><li><p>Typically around <strong>0.2%</strong> of the transaction amount.</p></li><li><p>Both the <strong>Issuer</strong> and the <strong>Acquirer</strong> pay this fee to the <strong>Card Network</strong> (eg: <strong>VISA)</strong></p></li></ol></li><li><p><strong>Interest and Late Fees</strong></p><ol><li><p>Earned by the <strong>Issuer</strong> from cardholders who fail to repay their credit card bills on time.</p></li><li><p>This forms a significant part of the issuer&#8217;s overall revenue from card transactions.</p></li></ol></li></ol><p>It&#8217;s a win-win for everyone, right? No wonder why credit card is called &#8220;<strong>the most profitable product</strong> <strong>in banks</strong>&#8221;! See you in the next one &#9996;&#127995;.<br></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Don&#8217;t forget to subscribe if you like my work. Cheers!</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 7 | What happens when you swipe a credit card 💳 ?]]></title><description><![CDATA[Today, let's dive into the mechanics of what happens behind the scenes when someone swipes their Credit Card at a merchant's Point of Sale (POS) terminal. The transaction process can be broken down into two key flows:]]></description><link>https://read.systemdesignblueprint.com/p/ep-7-what-happens-when-you-swipe</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-7-what-happens-when-you-swipe</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Tue, 27 Aug 2024 08:40:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!8m-K!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>Today, let's dive into the mechanics of what happens behind the scenes when someone <strong>swipes their Credit Card</strong> at a merchant's Point of Sale (POS) terminal. The transaction process can be broken down into two key flows:</p><ol><li><p><strong>Authorization Flow</strong> &#8211; the initial step that determines whether the transaction is approved or declined in real-time.</p></li><li><p><strong>Settlement Flow</strong> &#8211; the End of Day (EOD) process where the funds are transferred from the cardholder&#8217;s bank to the merchant&#8217;s account.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8m-K!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8m-K!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 424w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 848w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 1272w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8m-K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png" width="1456" height="1082" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1082,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:647576,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8m-K!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 424w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 848w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 1272w, https://substackcdn.com/image/fetch/$s_!8m-K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9243e66-7b21-41e1-8ec3-4f2d0c97912b_2118x1574.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Authorization flow</strong></figcaption></figure></div></li></ol><h3>Authorization Flow</h3><ol><li><p>After conducting thorough due diligence, the <strong>Issuer Bank</strong> issues credit cards to customers. This is a complex process in itself, deserving a separate post!</p></li><li><p>Transaction is initiated when a customer swipes their card at a <strong>merchant&#8217;s POS terminal &#8212; </strong>often from fintechs like Paytm or <a href="https://www.pinelabs.com/">Pinelabs</a>.</p></li><li><p>The POS machine has its own OS, which securely transmits tokenized card details, the txn amount, and encrypted merchant info to the <strong>Acquiring Bank</strong>.</p></li><li><p>The <strong>Acquiring Bank</strong> processes card payments for businesses and forwards the transaction to the relevant <strong>card network</strong> (or card scheme). In this example, <strong>HDFC</strong> Bank <strong>issued</strong> the credit card, and <strong>VISA</strong> serves as the <strong>card network</strong>.</p></li><li><p><strong>VISA</strong> performs <strong>Anti-Fraud (AF)</strong> and <strong>Anti-Money Laundering (AML)</strong> checks and forwards secure transactions to the <strong>Issuer Bank</strong> which is responsible for:</p><ol><li><p>Checking card rules (active/inactive, card limits, domestic/international) and performing AML/AF checks.</p></li><li><p>Debiting amount from credit card and doing accounting (Checkout <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-2-accounting-in-payment-systems">Accounting in payment systems</a></strong>) of amount/revenue/tax.</p></li><li><p>Notify the customer via <strong>SMS</strong> about the transaction's success or failure and relay the response back to VISA.</p></li></ol></li><li><p>VISA sends the response to the <strong>Acquirer</strong>, which then <strong>relays</strong> it to the <strong>POS terminal</strong>. The POS displays a success or failure message and generates a transaction receipt.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1dj6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1dj6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 424w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 848w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 1272w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1dj6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png" width="1456" height="1071" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1071,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:357678,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1dj6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 424w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 848w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 1272w, https://substackcdn.com/image/fetch/$s_!1dj6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c653cd7-85b7-4572-9272-fdad12eb3dc2_2134x1570.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Settlement flow</strong></figcaption></figure></div><h3>Settlement flow</h3><ol><li><p>At the end of the day, the POS machine&#8217;s software sends all transaction data to the <strong>Acquiring Bank</strong> for reconciliation. This can be done via <strong>bulk APIs</strong> or by <strong>uploading files </strong>to a <strong>file server</strong>. Check out <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-5-reconciliation-system">Reconciliation system</a></strong> to learn about designing a recon system.</p></li><li><p>After reconciling transactions, the <strong>Acquiring Bank</strong> sends the batch file to VISA, which then forwards it to the <strong>Issuer Bank</strong>.</p></li><li><p>The Issuer Bank performs the final reconciliation and follows these steps:</p><ol><li><p><strong>Successful Reconciliation:</strong> The actual funds are transferred from the Issuer Bank to the Acquiring Bank.</p></li><li><p><strong>Failed Reconciliation:</strong> The amount is refunded to the customer&#8217;s credit card.</p></li></ol></li><li><p>Finally, the funds are <strong>settled</strong> into the <strong>merchant&#8217;s bank account</strong> after deducting all applicable fees.<br></p></li></ol><h4>Important points to consider</h4><ol><li><p><strong>Customer&#8217;s Credit Card Debiting:</strong> The customer&#8217;s credit card is debited immediately at the time of the transaction.</p></li><li><p><strong>Merchant&#8217;s Account Crediting:</strong> The merchant&#8217;s bank account is credited at EOD.</p></li><li><p><strong>Revenue Share and Fees:</strong> Different parties involved receive various revenue shares (fixed or percentage), which are deducted at different stages of the process. (For details, see our upcoming post on revenue sharing.)</p></li><li><p><strong>Refund Scenarios:</strong> The amount must be refunded to the customer&#8217;s credit card in the following cases:</p><ul><li><p><strong>Pending Transactions:</strong> If a transaction is pending due to network issues during authorization, any debited amount will be refunded.</p></li><li><p><strong>Reconciliation Failure:</strong> If reconciliation for a transaction fails, a refund must be processed.</p></li></ul></li><li><p><strong>AML and Anti-Fraud Checks:</strong> AML and AF checks are specialized and must be conducted at <strong>each step</strong>: by the acquirer, card network, and issuer.</p></li></ol><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading the <strong>System Design Newsletter</strong>! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 6 | How Google search ranking works 🔎 ]]></title><description><![CDATA[Google is the undisputed leader in search, serving billions of queries daily with unparalleled accuracy and speed.]]></description><link>https://read.systemdesignblueprint.com/p/ep-6-how-google-search-ranking-works</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-6-how-google-search-ranking-works</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Sun, 18 Aug 2024 17:38:52 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!6GmT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Google is the undisputed leader in search, serving billions of queries daily with unparalleled accuracy and speed. Its dominance stems from a complex and highly sophisticated ranking algorithm that is continuously evolving to deliver the most relevant and useful results. </p><p>By analyzing hundreds of factors, including <strong>content quality</strong>, <strong>user behavior</strong>, and <strong>contextual signals</strong>, Google ensures that users find exactly what they're looking for, whether a simple fact or a comprehensive research topic.</p><p>An in-depth analysis of how Google's <strong>complex ranking system works</strong> was <strong>recently released</strong> after 100s of hours of research. Presenting a high-level view:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6GmT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6GmT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 424w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 848w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6GmT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg" width="1456" height="995" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:995,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:324104,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6GmT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 424w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 848w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!6GmT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41c4c8b9-8f1d-4c82-a9b3-6a0239c203d2_1908x1304.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Originally deep dive by <strong>Mario Fischer</strong> from <a href="https://searchengineland.com">SearchEngineLand</a> - https://searchengineland.com/how-google-search-ranking-works-445141. </p><p>I will try to break this down into important services from a <strong>system design pov</strong> and <strong>key strategies</strong> while setting up <strong>SEO</strong> for <strong>Google search</strong> in a later post.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading the <strong>System Design Newsletter</strong>! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 5 | How to design a reconciliation system 💵 💶?]]></title><description><![CDATA[Reconciliation in payment systems is the process of comparing internal systems' records with those in external systems at regular intervals to figure out and fix discrepancies. It is vital because:]]></description><link>https://read.systemdesignblueprint.com/p/ep-5-reconciliation-system</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-5-reconciliation-system</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Sat, 17 Aug 2024 10:52:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!R9eX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p><strong>Reconciliation</strong> in payment systems is the process of comparing <strong>internal systems'</strong> records with those in <strong>external systems</strong> at regular intervals to figure out and fix discrepancies. It is vital because:</p><ol><li><p>It uncovers issues in accounting systems that often go unnoticed until this critical step, ensuring errors are caught and corrected.</p></li><li><p>Effective reconciliation identifies anomalies and potential fraud by comparing internal records with external data.</p></li><li><p>Successful reconciliations enable actual fund transfers between payment systems, banks, and third-party platforms.</p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!R9eX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!R9eX!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 424w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 848w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 1272w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!R9eX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png" width="1456" height="1224" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1224,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:229542,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!R9eX!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 424w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 848w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 1272w, https://substackcdn.com/image/fetch/$s_!R9eX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e6898db-1521-4981-ab7d-7a4b7ffb7c4e_1522x1280.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Components of a recon system</strong></figcaption></figure></div><p>The process broadly involves the following steps:</p><ol><li><p><strong>Data ingestion and transformation</strong>: Workflow orchestrators like <strong><a href="https://airflow.apache.org/">Apache Airflow</a></strong> or big data processors like <strong><a href="https://spark.apache.org/">Apache Spark</a></strong> are generally used to fetch, transform, and normalize data from external sources through <strong>jobs</strong> running at regular intervals.</p><ol><li><p>Transaction statements from external services are <strong>received in CSV, excel or via APIs.</strong></p></li><li><p>All data is converted into a common format through a <strong>transformation</strong> job.</p></li><li><p>Data is <strong>normalized</strong> by aligning timestamps, converting currencies, and eliminating duplicates for accurate comparison with internal records.</p></li></ol></li><li><p><strong>Reconciliation Service</strong></p><ol><li><p>Data collection pipeline sends normalized data to reconciliation service either through streaming platforms like <strong><a href="https://kafka.apache.org/">Kafka</a></strong> or API call.</p></li><li><p>Transactions are fetched from internal accounting system (read <strong><a href="https://systemdesignnewsletter.substack.com/p/ep-2-accounting-in-payment-systems">Accounting in payment systems</a> </strong>&#128070;&#127995;&#128070;&#127995;) for comparison.</p></li><li><p>The service matches external and internal transactions, identifying <strong>discrepancies</strong> like missing entries or amount mismatches (<em><strong>Figure 1</strong></em>).</p></li></ol></li><li><p><strong>Reporting</strong>: Results can be viewed either in recon dashboards built inhouse/using <strong><a href="https://retool.com/">Retool</a></strong> or visualization platforms such as <strong><a href="https://www.metabase.com/">Metabase</a></strong>.<br></p></li></ol><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading the <strong>System Design Newsletter</strong>! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 4 | [Deep Dive] How Airbnb avoids double payments?]]></title><description><![CDATA[Ensuring eventual consistency in Airbnb's payment system was crucial as they transitioned to a Service Oriented Architecture (SOA). This migration presented numerous challenges, including:]]></description><link>https://read.systemdesignblueprint.com/p/ep-4-deep-dive-how-airbnb-avoids</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-4-deep-dive-how-airbnb-avoids</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Thu, 08 Aug 2024 07:51:29 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!tIZU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>Ensuring eventual consistency in <strong>Airbnb's</strong> payment system was crucial as they transitioned to a <strong><a href="https://medium.com/airbnb-engineering/building-services-at-airbnb-part-2-142be1c5d506">Service Oriented Architecture</a></strong> (SOA). This migration presented numerous challenges, including:</p><ol><li><p>How to handle race conditions if a user clicks &#8220;Book&#8221; twice?</p></li><li><p>How to ensure that refunds are processed only once?</p></li><li><p>What if the response was lost or the client timed out?</p></li><li><p>How would clients recover if it failed to consume the response?<br></p></li></ol><p><strong>Idempotency</strong>, as discussed in <strong><a href="https://systemdesignalmanac.substack.com/p/ep-3-avoiding-double-payments">Avoiding double payments &#129327;</a></strong> is how Airbnb successfully navigated these obstacles and achieved eventual consistency in a distributed payments system. At a high level, <em><strong>Figure 1</strong></em> illustrates a simple example scenario with duplicate requests and <strong>ideal</strong> idempotent behavior. No matter how many charge requests are made, the guest will always be charged <strong>at most once</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tIZU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tIZU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 424w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 848w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 1272w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tIZU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png" width="440" height="526.7307692307693" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1743,&quot;width&quot;:1456,&quot;resizeWidth&quot;:440,&quot;bytes&quot;:73668,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tIZU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 424w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 848w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 1272w, https://substackcdn.com/image/fetch/$s_!tIZU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feee2de72-2f2e-4252-9f73-cb37bca0ad0f_1542x1846.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Customer is always charged only once</strong></figcaption></figure></div><h2>The Problem Statement &#129488;</h2><ol><li><p>They needed a <strong>generic</strong> yet configurable idempotency solution to be used across various Payments services.</p></li><li><p>The solution should be able to classify errors as <strong>retryable</strong> and <strong>non-retryable</strong> and handle them accordingly.</p></li><li><p>The solution had to be an ultra-low latency to handle the payment scale.</p></li></ol><h2>The Solution &#9989;</h2><p>Airbnb engineering teams built <strong>&#8220;Orpheus&#8221;</strong>, a <strong>general-purpose idempotency library</strong>,<strong> </strong>to use across multiple payments services. <em><strong>Fun fact:</strong> <a href="https://en.wikipedia.org/wiki/Orpheus">Orpheus</a> is the legendary Greek mythological hero who was able to orchestrate and charm all living things &#129412;.</em></p><p>Key design principles used in Orpheus (<em><strong>Figure 2</strong></em>):</p><ol><li><p>Building a separate, stand-alone idempotency service wouldn&#8217;t be sufficient because they needed ultra-low latency. Choosing to build a library was the solution.</p></li><li><p>Idempotency design</p><ol><li><p>The client has to pass a unique <strong>idempotency key</strong> into the framework, representing a single idempotent request.</p></li><li><p>The client should ensure that <strong>mutation</strong> of request payload between retry attempts is <strong>not</strong> allowed.</p></li></ol></li><li><p>Low-level design</p><ol><li><p>Database transactions are combined in different parts of the codebase to ensure <strong><a href="https://en.wikipedia.org/wiki/Atomicity_%28database_systems%29">atomicity</a></strong>. Airbnb used <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html">Java lambdas</a> to achieve this.</p></li><li><p>At code level, every API request is separated into <strong>three</strong> distinct phases: <strong>Pre-RPC</strong>, <strong>RPC</strong>, and <strong>Post-RPC</strong>. This ensures a clear distinction between DB and network/API calls.</p></li></ol></li><li><p>Exception Handling</p><ol><li><p>Orpheus was designed to classify responses as &#8220;<strong>retryable</strong>&#8221; or &#8220;<strong>non-retryable</strong>&#8221;.<br></p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9AU6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9AU6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 424w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 848w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 1272w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9AU6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png" width="558" height="636.510333863275" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1435,&quot;width&quot;:1258,&quot;resizeWidth&quot;:558,&quot;bytes&quot;:83734,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9AU6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 424w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 848w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 1272w, https://substackcdn.com/image/fetch/$s_!9AU6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa629ca29-87d9-4344-a489-07e2c712ac7e_1258x1435.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Design principles used to guarantee idempotency</strong></figcaption></figure></div><h3>Idempotency design</h3><p>The client must own several key responsibilities when interacting with a service that uses an idempotency library like Orpheus:</p><ol><li><p>Pass in a unique idempotency key for every new request. It&#8217;s important to <strong>reuse</strong> the <strong>same idempotency key</strong> for retries.</p></li><li><p><strong>Persist</strong> these idempotency keys to the <strong>database</strong> before calling the service (to later use for retries).</p></li><li><p>Properly consume successful responses and subsequently <strong>unassign (or nullify)</strong> idempotency keys.</p></li><li><p>Ensure that <strong>mutation</strong> of the request payload between retry attempts is <strong>not</strong> allowed.</p></li><li><p>Carefully devise and configure <strong>auto-retry strategies</strong> based on business needs (using <a href="https://en.wikipedia.org/wiki/Exponential_backoff">exponential backoff</a> or <a href="https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/">randomized wait times (&#8220;jitter&#8221;)</a> to avoid the <a href="https://en.wikipedia.org/wiki/Thundering_herd_problem">thundering herd problem</a>).<br></p></li></ol><h3>Low-level design</h3><p>They wanted to <strong>avoid mixing network communication </strong>with<strong> database calls</strong> because it can result in bad things like rapid connection pool exhaustion and performance degradation. Hence, the following segregation was implemented:</p><ol><li><p><strong>Pre-RPC (</strong><em><strong><a href="https://en.wikipedia.org/wiki/Remote_procedure_call">Remote Procedure Call</a></strong></em><strong>):</strong> Details of the payment request are recorded in the <em><strong>database</strong></em>.</p></li><li><p><strong>RPC: </strong><em><strong>Network</strong></em> call to external service is made and the response is received. This is the place to do one or more idempotent computations or RPCs (for eg: query service for the status of a transaction first if it&#8217;s a retry attempt).</p></li><li><p><strong>Post-RPC:</strong> Details of the response from the external service are recorded in the <em><strong>database</strong></em>, including its success and whether a bad request is <strong>retryable</strong> or not.</p></li></ol><p>To maintain data integrity,<strong> two simple ground rules </strong>were set:</p><ol><li><p><strong>NO service interaction over networks </strong>in<strong> Pre </strong>and<strong> Post-RPC </strong>phases.</p></li><li><p><strong>NO database interactions </strong>in the<strong> RPC </strong>phases.</p></li></ol><h3><br>Exception Handling</h3><p>All validation errors, such as invalid input and states (for eg: you can&#8217;t refund a refund) are categorized as <strong>non-retryable</strong> (<strong>4XX HTTP</strong> statuses). All subsequent retries of the same request are expected to fail in the same manner. This categorization is handled by creating a <strong>custom, generic exception</strong> class. Broadly, <em><strong>Figure 3</strong></em> shows how error categorization happens.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bjxU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bjxU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 424w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 848w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 1272w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bjxU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png" width="1414" height="598" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:598,&quot;width&quot;:1414,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:237639,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bjxU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 424w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 848w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 1272w, https://substackcdn.com/image/fetch/$s_!bjxU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5896c89b-cb87-4554-b8e0-d54696ba6e39_1414x598.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Categorizing &#8220;retryable&#8221; and &#8220;non-retryable&#8221; exceptions</strong></figcaption></figure></div><h2><br>Additional considerations</h2><h3>Each API request should have an expiring lease</h3><h4><strong>Problem</strong></h4><ul><li><p>Multiple identical requests can be fired due to numerous user clicks or if the client has an aggressive retry policy. This could create <strong>race conditions</strong> on the server or <strong>double payments</strong> for Airbnb&#8217;s community.</p></li></ul><h4><strong>Solution</strong></h4><ul><li><p>Each request needs to acquire a database <strong>row-level lock</strong> on an idempotency key. This grants a lease for a given request to proceed further.</p></li><li><p>The <strong>lease expires</strong> only after the response from the third-party payment service is <strong>written back</strong> to the <strong>locked row</strong>. This also includes timeouts/network errors, etc.</p></li><li><p>Having a DB lock ensures that subsequent API calls with the same idempotency key <strong>wait before</strong> previous ones complete and hence return the already saved response ensuring that the customer is c<strong>harged only once</strong>.</p></li><li><p>A good rule of thumb is to have a <strong>higher lease expiration</strong> than the <strong>RPC timeout</strong>.</p></li></ul><h3><strong><br>Avoid Replica Databases</strong></h3><h4>Problem</h4><ul><li><p>A lag in <strong>syncing</strong> master to replica DB might result in double payments. <em><strong>Figure 4</strong></em> shows how just a few seconds of <strong>replica lag</strong> could cause a significant financial impact on Airbnb&#8217;s community.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FpbR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FpbR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 424w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 848w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 1272w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FpbR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp" width="476" height="500.1153546375682" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1348,&quot;width&quot;:1283,&quot;resizeWidth&quot;:476,&quot;bytes&quot;:66440,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FpbR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 424w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 848w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 1272w, https://substackcdn.com/image/fetch/$s_!FpbR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9a49ee2-ac95-4e91-bc08-43b1ca53b590_1283x1348.webp 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: Lag in syncing DBs can result in double payments</strong></figcaption></figure></div><h4>Solution</h4><ul><li><p>Using a <strong>single master DB</strong> for storing idempotency info. Although this solves the problem of <strong>replica lag</strong>, scaling this was another challenge.</p></li><li><p>Airbnb&#8217;s ENG team alleviated this by <strong>sharding</strong> the database by <strong>idempotency key</strong>. The idempotency keys have high cardinality and even distribution, making them effective shard keys. <em><strong>Figure 5</strong></em> shows how using a single master DB solves the problem for them.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JQGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JQGC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 424w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 848w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 1272w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JQGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp" width="500" height="379.8076923076923" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1106,&quot;width&quot;:1456,&quot;resizeWidth&quot;:500,&quot;bytes&quot;:62200,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JQGC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 424w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 848w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 1272w, https://substackcdn.com/image/fetch/$s_!JQGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6b42cbc4-92ba-4275-b85f-f969dcf9a933_1872x1422.webp 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Single Master DB solves the problem of replica lag</strong></figcaption></figure></div><h2>Final Thoughts</h2><p>In distributed systems, achieving <strong>eventual consistency</strong> introduces complexity, but solutions like <strong>Orpheus</strong> help mitigate these challenges! Orpheus is generalizable and lightweight, allowing developers to import the library for new services and keep idempotency logic abstracted.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong>! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[EP 3 | Avoiding double payments 🤯]]></title><description><![CDATA[The accuracy and reliability of transactions are paramount in a payment system. One of the most critical issues that can arise is double charging a customer for a single transaction. Avoiding double payments is essenti]]></description><link>https://read.systemdesignblueprint.com/p/ep-3-avoiding-double-payments</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-3-avoiding-double-payments</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Fri, 02 Aug 2024 14:06:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!znm0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>The accuracy and reliability of transactions are paramount in a payment system. One of the most critical issues that can arise is <strong>double charging</strong> a customer for a single transaction. Avoiding double payments is essential not only to maintain customer trust and satisfaction but also to prevent financial losses and potential legal issues for businesses.</p><p>While designing the payment system, it is important to guarantee that the payment system executes an order <strong>exactly once</strong>. This can be achieved if following conditions are satisfied:</p><ol><li><p>Payment is executed <strong>at least </strong>once - Using <strong>RETRY</strong></p></li><li><p>Payment is executed<strong> at most </strong>once - Through <strong>IDEMPOTENCY</strong></p></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!znm0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!znm0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 424w, https://substackcdn.com/image/fetch/$s_!znm0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 848w, https://substackcdn.com/image/fetch/$s_!znm0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 1272w, https://substackcdn.com/image/fetch/$s_!znm0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!znm0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png" width="886" height="1372" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1372,&quot;width&quot;:886,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:163764,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!znm0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 424w, https://substackcdn.com/image/fetch/$s_!znm0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 848w, https://substackcdn.com/image/fetch/$s_!znm0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 1272w, https://substackcdn.com/image/fetch/$s_!znm0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F742afac9-c99f-4f80-9443-f4b26cd71ced_886x1372.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: How </strong><em><strong>RETRY</strong></em><strong> and </strong><em><strong>IDEMPOTENCY</strong></em><strong> avoid &#8220;double payments&#8220;</strong></figcaption></figure></div><h3>RETRY</h3><ul><li><p>Occasionally, we need to retry a payment transaction due to network errors or timeout. Retry provides the at-least-once guarantee.</p></li><li><p>For example, as shown in Figure 1, the client tries to make a $10 payment, but the payment keeps failing due to a poor network connection. </p></li><li><p>Considering the network condition might get better, the client retries the request and this payment finally succeeds at the fourth attempt.<br></p></li></ul><h3>IDEMPOTENCY</h3><ul><li><p>From an API standpoint, idempotency means clients can make the same call repeatedly and produce the same result.</p></li><li><p>Second part of Figure 1 presents a scenario where customer pressed pay button multiple times but system charges customer only once. Here&#8217;s how:</p><ul><li><p>Client sends a unique key (<strong>itempotency_key</strong>) along with every request.</p></li><li><p>After initiating the transaction with the payment provider, itempotency_key is persisted in cache.</p></li><li><p>Since customer pressed the pay button twice, client makes API call with the same itempotency_key again. </p></li><li><p>Since this key already exists in cache, so previous success response is returned instead of initiating the payment again.</p></li></ul></li><li><p>Few characteristics of <strong>idempotency key:</strong></p><ul><li><p>A UUID is commonly used as an idempotency key and it is recommended by many tech companies such as Stripe and PayPal</p></li><li><p>A new UUID should be generated whenever the <strong>request payload</strong> changes.</p></li><li><p>The key is generated by clients and expires after a certain period of time (generally 24 hrs).</p></li><li><p>To perform an idempotent payment request, an idempotency key is generally added as a HTTP header.</p></li></ul></li></ul><p></p><p>This is how a combination of <strong>retries</strong> and <strong>idempotency keys </strong>can help in avoiding double charging the customer! </p><p>In the next issue, we&#8217;ll dive deep into how <strong>Airbnb designed their distributed payments system to avoid double payments &#128170;</strong>.<br></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading <strong>System Design Almanac</strong> &#129489;&#8205;&#128187;! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 2 | Accounting in payment systems 📗]]></title><description><![CDATA[In the previous post, we learned how to design a payment system. This post is about one of the most important pieces of a payment system, the &#8220;Ledger System&#8221; which serves the following purposes:]]></description><link>https://read.systemdesignblueprint.com/p/ep-2-accounting-in-payment-systems</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-2-accounting-in-payment-systems</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Wed, 31 Jul 2024 05:52:58 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!iynj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket</a></strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;!</p><p>In the previous post, we learned how to <a href="https://systemdesignalmanac.substack.com/p/ep-1-how-to-design-a-payment-system">design a payment system</a>. This post is about one of the most important pieces of a payment system, the &#8220;<strong>Ledger System&#8221;</strong> which serves the following purposes:</p><ol><li><p>Maintaining accounts payable and receivable</p></li><li><p>Reconciliation</p></li><li><p>Determining revenue and profitability</p></li></ol><h2>Basic ledger system</h2><p>Figure 1 shows the design of a basic ledger system that maintains the <strong>amount</strong>, <strong>txn_type</strong>, and <strong>merchant</strong> who has to be paid out. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iynj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iynj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 424w, https://substackcdn.com/image/fetch/$s_!iynj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 848w, https://substackcdn.com/image/fetch/$s_!iynj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 1272w, https://substackcdn.com/image/fetch/$s_!iynj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iynj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png" width="1300" height="620" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:620,&quot;width&quot;:1300,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:123958,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iynj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 424w, https://substackcdn.com/image/fetch/$s_!iynj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 848w, https://substackcdn.com/image/fetch/$s_!iynj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 1272w, https://substackcdn.com/image/fetch/$s_!iynj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16e82d3c-8a30-4139-b148-a1d221718f4e_1300x620.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Basic ledger system</strong></figcaption></figure></div><h3>Challenges</h3><p>Though it&#8217;s simple, the above system poses the following challenges:</p><ol><li><p><strong>Slow settlement</strong>: Calculating the <strong>balance</strong> for a merchant requires an expensive GROUP BY aggregate for each merchant, which struggles if you have to pay out <strong>millions</strong> of payments daily (think Amazon&#8217;s scale).</p></li><li><p><strong>Partial payments</strong>: It&#8217;s impossible to <strong>split a single payment</strong> into multiple payouts since there is a <strong>many-to-one</strong> relationship between payments and payouts. This makes for frustrating product limitations - if a merchant deemed risky makes a single $100,000 transaction, either the entire transaction has to be blocked or none of it.</p></li><li><p><strong>Data inconsistency</strong>: Since this is just a SQL database, nothing is preventing the payouts from <strong>becoming inconsistent</strong>. The payout_id can be ensured to be a valid foreign key, but nothing is stopping it from being nulled out.</p></li></ol><h2>Double-entry accounting</h2><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hYfq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hYfq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 424w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 848w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 1272w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hYfq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png" width="1194" height="922" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:922,&quot;width&quot;:1194,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:140917,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hYfq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 424w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 848w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 1272w, https://substackcdn.com/image/fetch/$s_!hYfq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ce080c4-7e9e-4f15-a7f3-919ef4ef2d1c_1194x922.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 2: Double Entry accounting with Books and Journals</strong></figcaption></figure></div><p>Enter <strong>Double-entry accounting</strong>. It is a battle-tested approach to modeling financials. Here&#8217;s how:</p><ol><li><p>It forces you to state not just what financial state change occurred, but <em><strong>why</strong>.</em></p></li><li><p>This is done by maintaining <strong>Books</strong> and <strong>Journal Entries</strong>:</p><ol><li><p>Each book maintains it&#8217;s own set of CREDIT/DEBIT transactions. </p></li><li><p>Each transaction should have at least two <strong>journal entries</strong> in the ledger system (hence the name <strong>double-entry</strong>) where every entry must have a <strong>source</strong> account and a <strong>target</strong> account.</p></li></ol></li><li><p>According to the accounting equation, every journal entry must balance to 0, so each cent lost is matched with a cent gained.</p></li><li><p><strong>Figure 2</strong> shows <strong>&#8220;merchant_book&#8221; </strong>and<strong> &#8220;customer_account_book&#8221; </strong>for the example we took in Figure 1.</p><ol><li><p>The first $100 transaction has two entries: a <strong>CREDIT</strong> entry in <strong>merchant_book</strong> and a corresponding <strong>DEBIT</strong> entry in <strong>customer_account_book</strong>, under the same Journal ID <strong>J01.</strong></p></li><li><p>Account balances are maintained separately in <strong>account_balance</strong> table for quick settlements. This solves the GROUP BY query challenge mentioned earlier.</p></li></ol></li></ol><p>This is how a ledger system is designed, only a real ledger is more complicated.&nbsp;<br>Applying these strict accounting rules makes reconciliation much easier!<br></p><p>Resources:</p><ul><li><p>https://developer.squareup.com/blog/books-an-immutable-double-entry-accounting-database-service.<br></p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">If you like my <strong>system design diagrams</strong>, please <strong>subscribe</strong> to receive new posts and support my work &#128591;&#127995;</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[EP 1 | How to design a payment system 💰?]]></title><description><![CDATA[Hi, Aniket here &#128075;&#127995; and welcome to my System Design Newsletter &#128522;!&#160;I am a software engineer and was among the first 5 hires at Barq (fintech super app for MENA region, close to 2M users now!)]]></description><link>https://read.systemdesignblueprint.com/p/ep-1-how-to-design-a-payment-system</link><guid isPermaLink="false">https://read.systemdesignblueprint.com/p/ep-1-how-to-design-a-payment-system</guid><dc:creator><![CDATA[Aniket Singh]]></dc:creator><pubDate>Sun, 28 Jul 2024 10:01:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!O905!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hi, <strong>Aniket</strong> here &#128075;&#127995; and welcome to my <strong>System Design Newsletter</strong> &#128522;! </p><p>I am a software engineer and am among the first 5 hires at <strong><a href="https://barq.com/">Barq</a></strong> (fintech super app for MENA region, close to 2M users now!) where I have built payment systems from scratch. Previously, I have built distributed systems at <strong><a href="https://tetrate.io/">Tetrate</a></strong> and <strong><a href="https://www.nutanix.com/">Nutanix</a>. </strong>You can find me on Linkedin as <strong><a href="https://www.linkedin.com/in/aniketsingh03/">Aniket Singh</a>.</strong></p><p>I feel a bit nervous as I embark on this writing journey &#128556;. Over the past 5 years, I&#8217;ve had the privilege of building software systems at both <strong>mid-sized</strong> companies and <strong>startups</strong>, and now I&#8217;m excited to share the things I&#8217;ve learned along the way.</p><p>Fintech has always been close to my heart, so it feels natural to kick off this newsletter with a topic that&#8217;s relevant&#8212;<strong>Payment Systems</strong>. I hope that through sharing my knowledge, I can bring value to you all &#128591;&#127995;. So, let&#8217;s begin!</p><div><hr></div><p>What is a <strong>payment system</strong>? According to <strong>Wikipedia</strong>, &#8220;a payment system is any system used to settle financial transactions through the transfer of monetary value. This includes the institutions, instruments, people, rules, procedures, standards, and technologies that make its exchange possible&#8221; [1].</p><p>Payment systems have become more important than ever nowadays and knowing the intricacies of designing an efficient payment system can surely give an edge. Let&#8217;s dive into technicals:</p><p>At a high level, the payment flow is broken down into two steps to reflect how money flows:</p><ul><li><p>Pay-in flow</p></li><li><p>Pay-out flow</p></li></ul><p>Take amazon, as an example. After a buyer places an order, the money flows into Amazon&#8217;s bank account, which is the <strong>pay-in flow</strong>. Although the money is in Amazon's bank account, Amazon does not own all of the money. The seller owns a substantial part of it and Amazon only works as the money custodian for a fee. </p><p>Later, when the products are delivered and money is released, the balance after fees then flows from Amazon&#8217;s bank account to the seller's bank account. This is the <strong>pay-out flow</strong>. </p><p>The simplified pay-in and pay-out flows are shown in Figure 1.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-cqi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-cqi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 424w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 848w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 1272w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-cqi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png" width="662" height="265" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:265,&quot;width&quot;:662,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:98645,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-cqi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 424w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 848w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 1272w, https://substackcdn.com/image/fetch/$s_!-cqi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0053971a-d998-4a7f-9149-3a08bb3948c8_662x265.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 1: Simplified pay-in and pay-out flow</strong></figcaption></figure></div><h2>Pay-in flow</h2><p>The high-level design diagram for the pay-in flow is shown in Figure 2. Let&#8217;s take a look at each component of the system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!O905!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!O905!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 424w, https://substackcdn.com/image/fetch/$s_!O905!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 848w, https://substackcdn.com/image/fetch/$s_!O905!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 1272w, https://substackcdn.com/image/fetch/$s_!O905!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!O905!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png" width="1456" height="1239" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1239,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:329837,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!O905!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 424w, https://substackcdn.com/image/fetch/$s_!O905!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 848w, https://substackcdn.com/image/fetch/$s_!O905!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 1272w, https://substackcdn.com/image/fetch/$s_!O905!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F314b7e0a-d4b1-4717-8a1b-4bee3a539ab0_1546x1316.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Figure 1</figcaption></figure></div><h3>Detailed Pay-in flow:</h3><ol><li><p><strong>Payment service</strong></p><ol><li><p>Frontend hits payment service when customer wants to initiate checkout for items in their cart.</p></li><li><p>The first thing it does is a risk check, assessing for compliance with regulations such as AML/CFT, money laundering or financing of terrorism. Risk checks are usually done through a 3rd party provider because it is complicated and highly specialised.</p></li></ol></li><li><p>Initiate and store transaction</p><ol><li><p>Generate a <strong>nonce</strong> and send it to initiate payment with the PSP.</p></li><li><p>The PSP sends a <strong>payment_uuid</strong> in response which needs to be stored in <strong>payments</strong> table with <strong>&#8220;status = INITIATED&#8221;.</strong> It&#8217;s also important to store each payment update in a <strong>payment_history</strong> table to track user drop-offs and debugging issues.</p></li></ol></li><li><p>The client displays a <strong>PSP-hosted payment </strong>page <strong>(Figure 4)</strong>. There are 2 ways to do this:</p><ol><li><p><strong>Redirect URI</strong>: Initiate in previous step returns a redirect URI which can be shown on a web view on mobile devices.</p></li><li><p><strong>PSP&#8217;s SDK integration</strong>: PSPs such as Stripe provide a JavaScript library that helps in building the payment UI, collect sensitive payment information, and call the PSP directly to complete the payment.<br></p></li></ol></li></ol><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PDTT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PDTT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 424w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 848w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PDTT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg" width="522" height="309.39972527472526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:863,&quot;width&quot;:1456,&quot;resizeWidth&quot;:522,&quot;bytes&quot;:143192,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PDTT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 424w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 848w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!PDTT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F87c0ddb1-76ba-4c4d-8e38-302c1a58185a_1600x948.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 3: Detailed PSP payment flow</strong></figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fuxw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fuxw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 424w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 848w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 1272w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fuxw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png" width="546" height="349.0693069306931" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:904,&quot;width&quot;:1414,&quot;resizeWidth&quot;:546,&quot;bytes&quot;:194253,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fuxw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 424w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 848w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 1272w, https://substackcdn.com/image/fetch/$s_!fuxw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30c6177-eca3-48ff-9d25-b77bf45c71c0_1414x904.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"><strong>Figure 4: PSP hosted page</strong></figcaption></figure></div><ol start="4"><li><p>Customer selects a payment method (<strong>credit card</strong> in our example) and sends a <strong>payment event</strong> to payment service.</p></li><li><p>A customer order might contain items from multiple merchants. Thus, a payment event contains multiple payment orders. Each payment order has be sent to a <strong>Payment Executor</strong> which executes them one by one.</p></li><li><p><strong>Payment Executor</strong></p><ol><li><p><strong>Tokenise </strong>the card details with PSP and store <strong>card_token</strong> in the DB (if customer consents). Use the <strong>payment_uuid </strong>and <strong>card_token</strong> to complete the payment with the PSP. Communication between PSP and card networks also happens through tokenised details.</p></li><li><p>Payment status is updated in the DB to something like <strong>IN_PROGRESS</strong> (in most cases, the final webhook in step 8 marks the payment as SUCCESS or FAILED).</p></li></ol></li><li><p><strong>Accounting</strong></p><ol><li><p><strong>Ledger</strong> service is responsible for lodging accounting entries for the payment. <strong>Double entry accounting</strong> which uses journals is a reliable way to implement this (separate post on this later).</p></li><li><p><strong>Wallet:</strong> If wallet (eg: Amazon pay) is selected as payment method, then wallet service is called which is responsible for deducting wallet balance and marking entries in ledger service <strong>(Figure 5)</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!T6TP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!T6TP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 424w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 848w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 1272w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!T6TP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png" width="148" height="185.1174603174603" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/efc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:788,&quot;width&quot;:630,&quot;resizeWidth&quot;:148,&quot;bytes&quot;:60284,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!T6TP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 424w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 848w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 1272w, https://substackcdn.com/image/fetch/$s_!T6TP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fefc42cba-976d-4d4b-bf49-b8c2a9901bb4_630x788.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><strong>Figure 5: Wallet</strong></figcaption></figure></div></li></ol></li><li><p>After conducting <strong>risk checks</strong> and processing the payment at their end, PSPs send a payment status (SUCCESS/FAILED) either through <strong>webhook</strong> or require apps to <strong>poll the status</strong>. In case of failure, additional steps to refund the money are required.</p></li><li><p><strong>Reconciliation:</strong> PSPs send settlement file (generally through SFTP) at the end of the day to reconcile all the payments. Reconciliation service matches all payments in settlement file with the ledger entries to validate all payments.</p></li></ol><p>This sums up the design of pay-in system. Pay out part and more on payment systems in upcoming posts &#129395;&#129395;!!</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://read.systemdesignblueprint.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Aniket&#8217;s Substack! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item></channel></rss>