AlgoViz

OLTP

medium 28 min

OLTP, online transaction processing, is the work of recording business events as they happen: an order, a payment, a booking.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring it up

You hearSayBecause
Book, buy, pay, transferA relational primary with transactionsSeveral rows change together
Never sell twiceConditional update or unique constraintThe database is the referee
Checkout must be quickShort transaction, key lookupsLocks are held briefly
Dashboards over all ordersA copy for analysisScans disturb short work
70 orders a secondOne database is enoughCalculate before sharding
Third-party paymentTwo short transactionsNo lock during the call

In the interview

  • Draw the tables and keys
  • Mark where the transaction begins and ends
  • Say which row is hot
  • Give the peak in transactions a second

Avoid

  • Sharding at 70 a second
  • Read, decide in code, write without a lock
  • A remote call inside a transaction
  • "ACID" as an answer without the rows

Options

Compare your moves

Two people press Buy for the last ticket. How is it decided?

BreaksRead, check, write
How
The app reads the stock, sees 1, and writes 0.
Limit
Both read 1 and both sell. The ticket is sold twice.
Choose when
Never for things that run out.
WorksLock the row first
How
SELECT FOR UPDATE, check, update, commit.
Limit
More round trips while the lock is held, so a hot row is slower.
Choose when
The decision needs several values.
StrongOne conditional update
How
Subtract where stock is at least one, and check that one row changed.
Limit
Still one row: a huge sale needs a queue or split stock.
Choose when
The rule fits in one statement.