How do I stop two people from booking the same seat, room or time slot?
Applications like this: hotel rooms, flight seats, appointment slots, event tickets. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.
| Dimension | This application |
|---|---|
| Writes | Spiky. A ticket sale opens and everyone tries at once. |
| Reads | Availability searches, much more often than bookings. |
| Read to write | High, and very high during an on-sale. |
| Consistency | Strict on the booking itself. Availability displays can be a few seconds stale. |
| Latency | Booking under a second, even under a spike. |
| Growth | Moderate. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.
| Data | Where it goes | Why |
|---|---|---|
| Bookings and holds | PostgreSQL or MySQL | The database rejects a double booking, whatever the application code does. |
| Availability search | A cache or a read replica | Many readers, slightly stale is fine, final check happens at booking. |
| Waiting room during on-sales | A queue in front of the booking service | Turns a spike into a steady stream the database can take. |
How each family of store behaves under traffic: Database families.
-- discrete slots: a unique constraint is enough CREATE TABLE seat_bookings ( event_id bigint, seat text, booking_id bigint NOT NULL, PRIMARY KEY (event_id, seat) ); -- time ranges in PostgreSQL: no two overlapping bookings per room CREATE EXTENSION IF NOT EXISTS btree_gist; CREATE TABLE room_bookings ( room_id bigint NOT NULL, during tstzrange NOT NULL, EXCLUDE USING gist (room_id WITH =, during WITH &&) );
Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.
| What happens | What you see | What I do |
|---|---|---|
| Check-then-insert in application code | Double bookings under load | Use the constraint. A SELECT then INSERT is a race: both sessions read 'free' and both insert. It is the same shape as a lost update, and the fix is the same: let the constraint decide. |
| Locks held during payment | Pool exhausted during an on-sale | Use a hold row with an expiry instead of an open transaction. |
| Availability computed from scratch on each search | CPU spikes on the primary during an on-sale | Cache availability and recheck at booking. |
Cost. The on-sale spike decides the size. A queue in front lets you size for the steady rate instead of the peak.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational, plus a managed queue for on-sales |
| When it grows | The same. The spike is handled by the queue, not by a bigger database |
For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.
The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.