<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suchirreddy on Medium]]></title>
        <description><![CDATA[Stories by Suchirreddy on Medium]]></description>
        <link>https://medium.com/@suchirreddy31?source=rss-a6a9ddc008b7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z6tTG9I1Pp0dzC_k</url>
            <title>Stories by Suchirreddy on Medium</title>
            <link>https://medium.com/@suchirreddy31?source=rss-a6a9ddc008b7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:53:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suchirreddy31/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[️ Designing a Concurrency-Safe Ticket Booking System from Scratch]]></title>
            <link>https://medium.com/@suchirreddy31/%EF%B8%8F-designing-a-concurrency-safe-ticket-booking-system-from-scratch-6922db1b6401?source=rss-a6a9ddc008b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6922db1b6401</guid>
            <category><![CDATA[idempotency]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Suchirreddy]]></dc:creator>
            <pubDate>Mon, 20 Oct 2025 05:47:05 GMT</pubDate>
            <atom:updated>2025-10-20T05:47:05.830Z</atom:updated>
            <content:encoded><![CDATA[<h3>🧩 Defining the Problem</h3><p>Imagine thousands of users rushing to book seats for a movie premiere. Everyone clicks “Book” at the same time.</p><p>Now the question: <strong>how do you make sure that two users don’t end up booking the same seat?</strong></p><p>That’s the <em>heart</em> of the ticket booking problem.</p><p>It’s deceptively simple but extremely tricky in practice because:</p><ul><li>Seat availability changes in real time.</li><li>Multiple users may request the same seats concurrently.</li><li>Payments, cancellations, and timeouts all modify seat status.</li></ul><p>In short — you need a system that handles <strong>high concurrency</strong>, <strong>strong consistency</strong>, and <strong>low latency</strong>, all at once.</p><h3>🧮 Naive Approaches (and Why They Fail)</h3><h4>1. Database Transactions Only</h4><p>You might think: “Just use SQL transactions and row-level locks.”</p><p><strong>Example:</strong><br> When a user books seat A1, you lock that row in the seats table until checkout completes.</p><p><strong>Pros:</strong></p><ul><li>Simple, easy to reason about.</li><li>Works for small scale.</li></ul><p><strong>Cons:</strong></p><ul><li>Heavy locks block concurrent reads.</li><li>Can’t handle flash-sales (thousands of users → DB bottleneck).</li><li>Lock contention kills performance.</li><li>Doesn’t scale horizontally.</li></ul><p>Result: great for a classroom project, terrible for production scale.</p><h4>2. Optimistic Concurrency in the Database</h4><p>Instead of locking rows, use a version number or timestamp.<br> On update, check if the version changed — if yes, reject.</p><p><strong>Pros:</strong></p><ul><li>No DB locks → better throughput.</li><li>Easy to implement.</li></ul><p><strong>Cons:</strong></p><ul><li>Still DB-bound for every operation.</li><li>Conflicts detected <em>after the fact</em>, not prevented.</li><li>Users see “seat unavailable” errors after checkout → bad UX.</li></ul><h4>3. Cache-First Approach (Non-Atomic)</h4><p>Move seat availability to Redis or Memcached to reduce latency.</p><p><strong>Pros:</strong></p><ul><li>Super fast reads.</li><li>Reduces DB load.</li></ul><p><strong>Cons:</strong></p><ul><li>If you update multiple seats (say A1, A2, A3), there’s no atomicity.</li><li>Two users could read “available” before one updates → race conditions.</li><li>Eventually, DB and cache drift apart.</li></ul><h3>🚧 The Real Challenge</h3><p>All three methods hit the same wall:<br> <strong>no atomicity across multiple seat updates</strong> and <strong>no distributed transaction</strong> that’s fast enough for real-time UX.</p><p>We need something that’s:</p><ul><li><strong>Faster than a DB transaction</strong></li><li><strong>Safer than non-atomic cache writes</strong></li><li><strong>Scalable across many users and shows</strong></li></ul><h3>⚡ Incrementally Improving the Approach</h3><h4>Step 1: Introduce a Two-Stage Model</h4><p>Instead of booking directly, break it into:</p><ol><li><strong>Reserve (lock)</strong> — short-term hold on seats</li><li><strong>Confirm (book)</strong> — after successful payment</li></ol><p>This separation allows us to:</p><ul><li>Release locks automatically after TTL expiry</li><li>Handle payment failures gracefully</li><li>Avoid permanent DB locks</li></ul><p>But — we still need atomic operations to “lock” seats safely.</p><p><strong>Final Architecture</strong></p><p>Building a concurrency-safe booking system isn’t really about picking the right framework — it’s about <strong>understanding the principles of consistency, isolation, and distributed design</strong>.</p><p>At its core, the architecture brings together three powerful components, each solving a different layer of the problem:</p><p><strong>1. Redis for Atomic Seat Locks</strong><br> Redis acts as the real-time control layer for seat availability. By storing each seat’s state (AVAILABLE, LOCKED, BOOKED) and using Lua scripts to update multiple seats atomically, we ensure that no two users can ever book the same seat simultaneously. The single-threaded nature of Redis guarantees that every locking operation is executed atomically, making it ideal for high-concurrency environments.</p><p><strong>2. PostgreSQL for Durability</strong><br> While Redis manages the volatile, in-memory state of the system, PostgreSQL serves as the <strong>system of record</strong> — the permanent storage for all confirmed bookings, payment records, and transaction history. This separation of responsibilities lets us keep user interactions fast (through Redis) while ensuring data consistency and durability through a robust relational database.</p><p><strong>3. Message Queues for Reliability and Scalability</strong><br> To handle slow or asynchronous operations — such as persisting data, sending notifications, or releasing expired seat locks — a message queue acts as a decoupling layer between the real-time booking service and background processes. Events like BookingConfirmed or LockExpired are pushed into the queue, and background workers consume these messages reliably, even if a service crashes or restarts. This guarantees smooth recovery, retry mechanisms, and horizontal scalability.</p><p>When combined, these three layers form a <strong>balanced and fault-tolerant architecture</strong>:</p><ul><li><strong>Redis</strong> keeps performance and atomicity at the edge,</li><li><strong>PostgreSQL</strong> ensures consistency and durability at the core, and</li><li><strong>The Queue</strong> provides elasticity, reliability, and resilience between them.</li></ul><p>The result is a system that’s <strong>fast, safe, and production-ready — even without any managed cloud services</strong>.<br> This architecture not only prevents double booking but also lays the foundation for scaling to millions of transactions with minimal latency and zero data loss.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6922db1b6401" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>