Home›Design›Bookings and reservations
Design doc · Bookings

Bookings and reservations

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.

The short answerA relational database with the rule enforced by the database itself: a unique constraint on (resource, slot), or an exclusion constraint on time ranges in PostgreSQL. Short holds with an expiry for the checkout window.
1 · The workload

What the application asks of the database

DimensionThis application
WritesSpiky. A ticket sale opens and everyone tries at once.
ReadsAvailability searches, much more often than bookings.
Read to writeHigh, and very high during an on-sale.
ConsistencyStrict on the booking itself. Availability displays can be a few seconds stale.
LatencyBooking under a second, even under a spike.
GrowthModerate.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationBookings and holdsPostgreSQL or MySQLSOURCE OF TRUTHAvailability searchA cache or a read replicaCOPYWaiting room during on-salesA queue in front of the booking serviceCAN BE LOST

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.

DataWhere it goesWhy
Bookings and holdsPostgreSQL or MySQLThe database rejects a double booking, whatever the application code does.
Availability searchA cache or a read replicaMany readers, slightly stale is fine, final check happens at booking.
Waiting room during on-salesA queue in front of the booking serviceTurns a spike into a steady stream the database can take.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

-- 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 &&)
);
4 · How it grows

Start simple, change one thing at a time

  1. One databaseConstraints do the work.
  2. On-sale spikesA waiting room or queue in front, and availability served from cache.
  3. Many venuesPartition or shard by event or venue, so one hot event stays on one shard.

Scale it: watch this design grow

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.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
Check-then-insert in application codeDouble bookings under loadUse 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 paymentPool exhausted during an on-saleUse a hold row with an expiry instead of an open transaction.
Availability computed from scratch on each searchCPU spikes on the primary during an on-saleCache availability and recheck at booking.
6 · Alternatives

What I considered and did not pick

Cost. The on-sale spike decides the size. A queue in front lets you size for the steady rate instead of the peak.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: an overlapping booking rejected by the exclusion constraint, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged relational, plus a managed queue for on-sales
When it growsThe 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.

8 · Estimate it

The numbers, worked live

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.

Part 3 of 4 · Design · 24 pagesAll pages

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.