DEV Community

Cover image for How Does Amazon Stop Two People from Booking the Same Seat?
kavya1205
kavya1205

Posted on

How Does Amazon Stop Two People from Booking the Same Seat?

You're on BookMyShow or Amazon. You see 1 seat left. You click it. Someone else clicks it at the exact same moment.
The Problem
Imagine 1000 people are staring at the same "1 seat left" screen. When multiple people click Book Now at the same time, the server receives all those requests together.
Without any protection — all of them could "successfully" book the same seat. That's a disaster.

How it's actually solved:-

  1. Locking the seat temporarily (Optimistic / Pessimistic Lock) When you click a seat, the system locks it for you for ~10 minutes.

"Seat 12A is reserved for this session. Complete payment in 9:45"

You've seen this countdown on BookMyShow. That's a temporary lock in the database. No one else can book it while you're paying.
If you don't complete payment → lock expires → seat goes back to available.

  1. Database-level constraint The database has a rule:

"Only ONE booking record can exist per seat per show."

Even if 500 requests hit the server at the same time, the database only allows one of them to win. The rest get an error — and the user sees "Sorry, seat just got booked."
This is called a unique constraint.

  1. Queue the requests Instead of handling 1000 requests at the same time, Amazon puts them in a line (queue). First come, first served. Each request is processed one at a time for that seat. Simple and fair.

🎯 Real world analogy
Think of it like a token system at a restaurant

You take token #47
Only token #47 is called at the counter at a time
No two people handle the same counter simultaneously

Same idea.
🔑 Key terms
Pessimistic Lock : Lock the seat the moment someone clicks it
Optimistic Lock: Allow clicks, but only first one wins at save time
Unique Constraint: Database rule — no duplicates allowed
Queue: Process one request at a time, in order

Wrapping up
Next time you see that countdown timer on a booking site — you know exactly what's happening behind the scenes. The system locked that seat just for you, and is racing against your payment.
Pretty cool, right?

Top comments (0)