OLTP
medium 28 minOLTP, 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 hear | Say | Because |
|---|---|---|
| Book, buy, pay, transfer | A relational primary with transactions | Several rows change together |
| Never sell twice | Conditional update or unique constraint | The database is the referee |
| Checkout must be quick | Short transaction, key lookups | Locks are held briefly |
| Dashboards over all orders | A copy for analysis | Scans disturb short work |
| 70 orders a second | One database is enough | Calculate before sharding |
| Third-party payment | Two short transactions | No 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.