Lesson 243 of 296
Design: booking system and payment flow
What you will learn
- Designing a booking system that never double-books
- Holding a reservation while the user pays
- Payment flows with a provider like Stripe
- Webhooks, idempotency and reconciliation
- Handling high-demand events
On this page
Requirements
A system for booking limited resources: restaurant tables, hotel rooms, appointment slots, cinema seats, or event tickets. Users search availability, choose a slot, pay, and get a confirmation. The critical rule: never sell the same slot or seat twice. Payments must never be taken twice or lost.
Data model
resources (id, tenant_id, name, capacity) e.g. a table for 4, a room, a seat
slots / inventory (resource_id, starts_at, ends_at)
bookings (id, tenant_id, resource_id, user_id, during tstzrange, status, hold_expires_at, payment_id, version)
payments (id, booking_id, provider_payment_id UNIQUE, amount_pence, currency, status)Booking statuses: held → confirmed (paid) or expired / cancelled.
Preventing double booking
The approaches, from the strongest:
- A database constraint: an exclusion constraint that forbids two active bookings of the same resource with overlapping times (Lesson 226), or for seats a unique constraint on
(event_id, seat_id)over active bookings (a partial unique index). The database guarantees correctness even under heavy concurrency. - Pessimistic locking:
SELECT ... FOR UPDATEon the resource or seat rows while checking and inserting (Lesson 225). - Optimistic locking or conditional updates:
UPDATE seats SET status = 'held', held_by = $1 WHERE id = $2 AND status = 'available', and check that exactly one row changed. - Serializable transactions with retries (Lesson 224).
"Check availability, then insert" in application code without one of these will double-book under load.
Holds while paying
The user needs a few minutes to pay. Do not keep a database transaction open that long (Lesson 223). Instead:
- Create the booking with status
heldandhold_expires_at = now() + 10 minutes. The constraint treats held bookings as taken. - The user pays.
- On successful payment: status becomes
confirmed. - If the hold expires without payment: a scheduled job (or a check at read time) marks it
expired, releasing the slot. Show a countdown timer in the UI.
The payment flow (Stripe-style)
1. Client: "Pay for booking B" → API: create a PaymentIntent with the provider
(amount from the SERVER's price, currency, metadata: bookingId; Idempotency-Key: booking-B-attempt-1)
2. API → Client: client secret
3. Client: confirms payment in the provider's own UI component (card details never touch our servers: PCI scope stays small)
(3-D Secure / Strong Customer Authentication may ask the bank to verify the customer: required for many UK and EU card payments)
4. Provider → our webhook: "payment_intent.succeeded" (signed)
5. Webhook handler: verify the signature; de-duplicate by event ID; in one transaction: payment status = succeeded, booking status = confirmed; write an outbox event
6. Outbox → confirmation email, receipt, calendar inviteKey points:
- The server decides the price. Never trust an amount sent by the client.
- Idempotency keys on every call to the payment provider (Lesson 195), so a retry never charges twice.
- Webhooks are the source of truth, not the browser redirect after payment (the user might close the tab). Handlers must verify signatures, be idempotent (providers retry and may send duplicates), and respond quickly (do heavy work asynchronously).
- Paid but hold expired (the payment succeeded just after the hold expired and someone else took the slot): detect it in the webhook and refund automatically (or offer an alternative). Every design should answer this race.
- Reconciliation: a daily job compares your payments table with the provider's records and flags mismatches. Money systems always reconcile.
- Never store card numbers; store the provider's IDs.
- Amounts in integer minor units with a currency (Lesson 30).
State machine
Model bookings and payments as explicit state machines with allowed transitions (held → confirmed, held → expired, confirmed → cancelled → refunded), enforced in code and ideally in the database (CHECK constraints, conditional updates WHERE status = 'held'). This prevents impossible states like "expired but charged".
High-demand events (concert tickets)
A million people want 50,000 seats when sales open at 10:00:
- A virtual waiting room (queue) admits users at a controlled rate, protecting the system and giving fairness.
- Inventory in a fast, atomic store (Redis with atomic decrement or Lua, or database row locks with
SKIP LOCKEDto grab seats, Lesson 225). - Short holds and strict limits per user; bot protection.
- Pre-scale servers before the start time; cache static event pages on the CDN.
How to say it in an interview
"The core rule is never double-booking, so I enforce it in the database: an exclusion constraint on overlapping time ranges, or a unique constraint on active seat bookings, or conditional updates and row locks, never check-then-insert in code. A booking starts as held with an expiry while the user pays, then becomes confirmed or expires and frees the slot. Payments use a provider like Stripe: the server sets the amount, every call has an idempotency key, the client pays in the provider's component with Strong Customer Authentication, and the signed, idempotently processed webhook confirms the booking in one transaction with an outbox event. I handle the race where payment succeeds after the hold expired with an automatic refund, model states explicitly, and reconcile with the provider daily."
Key words
| Word | Meaning |
|---|---|
| Double booking | Selling the same slot twice |
| Hold | A temporary reservation while the user pays |
| PaymentIntent | Stripe's object representing one payment attempt |
| Webhook | An HTTP call from a provider when something happens |
| Strong Customer Authentication | Extra verification required for many UK and EU card payments |
| PCI DSS | Security rules for handling card data |
| Reconciliation | Comparing your records with the provider's |
| State machine | A defined set of states and allowed transitions |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.