Waiting rooms, seat locking, journey segments, payment holds, idempotency keys and retry storms

One berth, two taps

Over 37,000 tickets in one minute. That's how many bookings IRCTC recorded at 10:02 AM on 16 August 2025.

That number covers all bookings, not Tatkal alone. But it shows the shape of the problem. Thousands of people arrive in the same minute, and many of them want the same few berths.

Here's a smaller version of the problem that this article follows from start to finish.

Ananya needs to reach Patna for her sister's wedding. Rohan needs to reach Patna for a job interview. Same train. Same class. One berth left. They both tap Book.

Three questions come out of that one moment:

  • Who gets the berth?
  • What if the payment succeeds, but the page times out?
  • How can a railway sell the same berth twice without double-booking it?

Let's design the system that answers all three. If you'd rather watch, here's the animated version: https://youtu.be/q3e1lZGpGCQ

Ananya and Rohan are fictional. The waiting room, rate limits, locking, segment inventory, holds, idempotency, caching and retry controls are a conceptual design. IRCTC hasn't published its internal architecture. Every rate, timer and count that isn't sourced is illustrative.

37,410 tickets in one minute

Ananya and Rohan, one berth left

Two questions

A cliff, not a hill

A normal website gets its traffic spread across the day: a hill. A Tatkal window gets a huge share of its traffic in the first few minutes: a cliff.

A hill and a cliff

The Railways already shape that cliff:

  • Since June 2015, AC Tatkal opens at 10 AM and non-AC at 11 AM, staggered "to reduce the load on the ticketing site" (Business Standard).
  • Since July 2025, agents can't book opening-day Tatkal in the first 30 minutes of each window (Ministry of Railways).

Staggered windows

More servers help with search pages and fares. They don't create more seats. That last berth exists once, and every server has to agree on who owns it.

That's contention. The real bottleneck isn't the number of servers. It's one tiny piece of shared data that everyone wants at once.

Every server points at one row

Two gates before the counter

Before anyone touches that row, our design puts two gates in front of it.

Two gates in front of the inventory

Gate 1: screening. Ananya taps Book once. A script taps 50 times. Rate limits and bot checks can turn most of those taps away before they join the line. They reduce the problem; they don't eliminate it.

The scale is real. In 2025 alone, the Railways deactivated 3.04 crore accounts as fake or suspicious (Ministry of Railways, PIB).

Gate 2: a virtual waiting room. Think of a bank's token machine:

  • Ananya gets her number instantly.
  • The counter calls people in only as fast as it can safely serve them.
  • Her place is stored on the server, so a refresh doesn't send her to the back.

The waiting room keeps her place

Without a waiting room, the database becomes the queue: locks stack up and every request waits inside the busiest table. Databases are terrible queues.

Identity. Since July 2025, online Tatkal needs an Aadhaar-authenticated account, and in 2026 an Aadhaar OTP step was introduced on selected trains. The Ministry calls it "a uniqueness constraint". Fake accounts get much harder, but not impossible.

Identity checks

One caveat. Being called first doesn't guarantee getting the berth first.

One berth, two requests

Ananya and Rohan both reach the counter. In Tatkal you usually don't pick a seat number: you ask for a class, and the system allots one. So they're fighting over a count. Class 3A: one left.

Both requests read it. Without care, both bookings succeed: two tickets, one berth. That's a race condition, two requests grabbing the same thing at the same moment.

Without protection

Fix 1: pessimistic concurrency. The first request locks the row, re-checks that a berth is still free, and reserves it. The second request waits for the lock. It's safe, but under a crowd the waiting piles up.

-- Ananya's request (illustrative) SELECT available FROM inventory WHERE train = 12345 AND class = '3A' FOR UPDATE; -- locks the row -- re-check: 1 left, reserve it 

Pessimistic locking

Fix 2: optimistic concurrency. Don't lock while reading. Check that nothing changed before you commit:

  1. Both requests read left = 1, version = 7.
  2. Ananya's update says "set the count to 0 and the version to 8, but only if it's still version 7". It commits.
  3. Rohan's identical update matches nothing, because the version is now 8. He re-reads: 0 left, so he moves to the waitlist.
UPDATE inventory SET available = 0, version = 8 WHERE train = 12345 AND class = '3A' AND version = 7; -- 1 row for Ananya, 0 rows for Rohan 

Optimistic concurrency

The write itself may still take a brief lock, but nobody holds a lock while they think. Under extreme contention, optimistic retries can thrash, which is one more reason the waiting room matters.

The same berth, sold twice. Correctly.

So far we've treated a berth as simply available or gone. A railway is harder than any flash sale, because a train isn't one event. It's a journey with stops.

The same berth can be sold to one passenger from Delhi to Kanpur, and to another from Kanpur to Patna. Two tickets, one berth, and both are correct.

So inventory isn't just "how many berths are left". It's which berths are free on which parts of the route: inventory = berth × segment.

A booking from Delhi to Patna needs a berth that's free on every segment in between, reserved all at once, atomically, or not at all.

Journey segments

Real railways add quotas on top, but segments are the core, and they make railway booking a much harder problem than selling concert tickets.

Held, then paid

Ananya won the berth. Now she has to pay, and that takes time: an OTP, a bank page, a UPI app.

So her berth moves to a new state, held, like a shop keeping an item aside for a few minutes. From there it either becomes confirmed when the payment lands, or released back to the pool when the timer runs out. Each change is atomic, so a berth can never be held by two people for overlapping segments.

Available, held, confirmed

What if the payment and the expiry happen at the same moment? Make both transitions conditional, so only one can win:

-- payment arrives (illustrative) UPDATE reservations SET state = 'CONFIRMED' WHERE id = 'R-91' AND state = 'HELD' AND expires_at > now(); -- the hold expires UPDATE reservations SET state = 'RELEASED' WHERE id = 'R-91' AND state = 'HELD' AND expires_at <= now(); 

Whichever commits first changes the state; the other matches 0 rows. The inventory change commits with the winner.

Only one wins

If her money arrives after the hold was already released, the system can't hand her a seat that someone else now owns. It records the payment, matches it to her booking, and refunds it. That's reconciliation.

Reconciliation

Money gone, no ticket?

Now the moment every Tatkal user fears. Ananya's payment goes through, but the page times out. Money gone, no ticket on screen. So she taps again.

The server can't tell her retry from a brand new booking unless it's given a way to. That way is an idempotency key: a unique ID that lets the server recognise the same operation when it's retried. It's created when the booking starts and sent with every retry.

  • The server atomically claims the key, so only one request can start the operation.
  • If a retry arrives while it's still processing, the server answers "pending".
  • Once it's finished, every retry gets the saved result.
  • If the server crashes, another worker recovers the operation by checking what already happened (the booking and payment status), without blindly repeating it.

Claim the key if new

The payment request carries its own key, linked to the booking, so the payment provider doesn't charge twice either. Repeated taps don't have to mean repeated charges: tickets 1, charges 1.

Two keys, linked

Available 12, then waitlist

Rohan's side of the story. Just before he tapped, his screen showed "available 12". Eleven of those berths were booked by other passengers in the next few seconds, and then Ananya took the last one.

AVL 12, AVL 1, AVL 0

There's a second reason the number can mislead. Showing live counts on every refresh would hammer the busiest table in the system, so availability pages are often served from a cache, a saved copy that's a few seconds old. That's fine at 9 AM. At 10 AM, the real number moves faster than the copy refreshes.

The booking step checks the real inventory, which is why the final answer can differ from the number you saw. Fast and approximate for browsing; slow and exact for the berth.

Two read paths

The retry wave

Remember Ananya tapping again? Multiply that by thousands. The first wave isn't always the most dangerous one.

When responses slow down, timeouts start, and timeouts trigger retries: apps retry automatically and people press refresh. Now the system gets the original crowd plus a wave of retries, sometimes another. Load climbs exactly when capacity is gone. That's a retry storm, and it's how many big outages grow.

Retries pile on

The fixes work together:

  • Exponential backoff: each retry waits longer (1, 2, 4, 8 seconds).
  • Jitter: a little randomness, so retries don't all land on the same second.
  • Retry budget: the system limits how many retries it allows.
  • Circuit breaker: like a fuse, so a drowning service gets a moment to recover.

And while passengers are still in the waiting room, their sessions let them refresh without joining the queue again.

The fixes

So who gets the berth?

Here's the whole design in one picture.

The conceptual architecture

  1. Who gets the last berth? In our design, the request that reserves the inventory first, not necessarily the person who clicked first. Screening makes it harder for bots to cut in line.
  2. Paid, but the page timed out? Idempotency prevents duplicate processing, and reconciliation handles unmatched payments.
  3. Thousands tapping again? Backoff, jitter and retry budgets stop retries from becoming the outage.

And Rohan? A few minutes later, another passenger's hold expired. In our design, that berth goes to the next eligible passenger on the waitlist, and Rohan's ticket was confirmed. (That allocation policy is illustrative, not confirmed IRCTC behaviour.)

Three lessons for any system you build

Three lessons

  1. Protect the shared state. When everyone wants the same thing, more servers aren't enough.
  2. Make every retry safe. Anything that can be retried must be safe to repeat.
  3. Control your retries, or your own users become the outage.

Sources

Watch the animated version on YouTube: https://youtu.be/q3e1lZGpGCQ

Follow The Byte Mechanics

How software works, under the hood ⚙️