Skip to content

Why Can Two People Buy the Last Item at the Same Time?

Two customers click "Buy Now" on the last item in stock at almost the same moment — who gets it? Why transactions and row-level locking, not payment processing, are the hardest part of checkout.

By 2 min read
  • System Design Explained
  • Concurrency
  • Transactions
  • Database Architecture
  • Distributed Systems

(How Large E-commerce Systems Prevent Overselling)

Imagine there's only one laptop left in stock. Two customers click "Buy Now" at almost the exact same moment.

Both receive the request. Both see "1 item available." So... Who gets it?

If both orders succeed, you've just sold the same product twice. This is one of the hardest problems in large-scale systems.

Not because checking inventory is difficult. Because multiple users are trying to change the same data at the same time.

Why Can Two People Buy the Last Item at the Same Time?

A simplified flow looks like this:

  1. 🛒 Customer A and Customer B both place an order within the same moment.
  2. Both requests reach the Order Service.
  3. The database processes the inventory update as an atomic transaction.
  4. The first successful transaction reserves the last item.
  5. The second transaction sees updated inventory and fails gracefully.

Notice what happened.

The application didn't solve the problem. The database guaranteed consistency.

This is why concepts like transactions, row-level locking, optimistic concurrency, and inventory reservation exist.

They're not just database features. They're what prevent real-world business failures.

One lesson that's changed how I think about system design: Reading data is easy. Changing shared data safely is where architecture becomes difficult.

The next time you buy the last item online, remember...

The hardest part wasn't processing your payment. It was making sure no one else bought that same item.

What's another real-world system where concurrency creates surprising challenges?

Keep reading