Compare

Optimistic vs Pessimistic locking

OptimisticCheck the version when you save
RCB vs CSKM. Chinnaswamy Stadium · Sat, 7:30 PM

Stand P · ₹2,400

D111213141516
E111213141516
F111213141516
G111213141516
H111213141516

Pitch this way

  • Free
  • Held
  • Sold

Select a seat

Rajatopens the map
Bookingversion check
seatsversion check
  • F12v7 · free
  • F13v2 · free
  • F14v3 · free
PessimisticLock the seat when you pick it
RCB vs CSKM. Chinnaswamy Stadium · Sat, 7:30 PM

Stand P · ₹2,400

D111213141516
E111213141516
F111213141516
G111213141516
H111213141516

Pitch this way

  • Free
  • Held
  • Sold

Select a seat

Rajatopens the map
Bookinglocks first
seatsrow locks
  • F12free
  • F13free
  • F14free

BookMyShow selling seats for RCB’s next home match: two fans pick the same seat, one walks away mid-payment, then the sale opens. Illustrative prices, versions and counts.

Optimistic or Pessimistic locking?

When two people rarely touch the same thing, like editing your own profile, use optimistic: no locks, just a check when you save. When many fight over the same thing, like the last seats, lock it first, and keep the lock short.

DifferenceOptimisticPessimistic
When a clash is caughtWhen you save: the version has moved onWhen you pick: the row is already locked
What the loser seesRefused at the end, after filling everything in“Held” at once, and picks another
What it costsWasted work when many want the same thingA held seat blocks everyone while its holder thinks
Pick it forRare clashes: profiles, settings, cartsFierce contention: last seats, stock, money

Optimistic vs Pessimistic locking, explained step by step

1. Two fans, one seat

One BookMyShow seat, booked two ways. On the left, optimistic locking: nothing is locked, and when you save, the database checks nobody changed the seat since you read it. On the right, pessimistic locking: the seat is locked the moment you pick it, and nobody else can take it until you’re done. Let’s sell tickets for RCB’s next home match.

First, Rajat and DK both open the seat map and pick seat F12. The database gives each of them the seat at version seven.

Both fill in their details. Nothing is locked, so neither knows about the other.

Rajat pays first. The database checks: still version seven? Yes. The seat is his, and it becomes version eight.

DK pays a moment later. Still version seven? No, it’s eight now. His booking is refused: this seat was just taken.

Now the same two fans with pessimistic locking. Rajat taps F12 first, and the database locks the seat for him.

DK taps F12 a moment later. It’s locked, so he’s told at once: this seat is held. He picks F13 instead.

Rajat pays, the seat is his, and the lock is let go.

2. Rajat walks away

Next, Rajat picks F14, then takes a phone call with the payment page still open.

Nothing is locked, so DK can still buy F14, and he gets it. When Rajat comes back, his save is refused.

With pessimistic locking, F14 is locked the moment Rajat picks it.

DK wants it too, but it stays held all through Rajat’s call. Nobody can buy it until he pays, or his ten-minute hold runs out.

3. The sale opens

Finally, the sale opens: ten thousand fans for the last fifty seats. With optimistic locking, they all pick seats freely.

Most pick the same few seats, and only the first save on each one wins. Thousands fill in their details, only to be refused at the end, and try again.

With pessimistic locking, each tap locks a seat for one fan. Everyone else is told at once that it’s held, and picks another.

No one fills in their details for a seat they can’t have.

In short

So which should you pick? When two people rarely touch the same thing, like editing your own profile, use optimistic: no locks, just a check when you save. When many fight over the same thing, like the last seats, lock it first, and keep the lock short.