Reservation Service Design: REST Endpoints, Schema, Date Inventory
System DesignModel answerLast updated
Design a REST API and database schema for booking a stay or rental, compare per-day and interval inventory models, and support extending an existing reservation.
You are designing the backend for a service where a customer reserves a single bookable resource (a room, a vehicle, a rental unit) for a span of time.
Part 1: API and data model
Design a RESTful API that lets a customer:
- Search for what is available in a given time window.
- Create a reservation.
- View and cancel a reservation.
- Pay for and confirm a reservation, if your design needs that step.
For each endpoint, describe:
- The path and HTTP method.
- The shape of the request and response.
- The important status codes, including the failure cases.
Then describe the data model: tables or collections, key fields, and the indexes you would add and why.
Part 2: Two ways to model inventory
Describe and compare two designs for tracking availability:
- Per-date inventory: one record per resource per day.
- Interval-based inventory: one record per reserved or blocked time range.
Discuss correctness under concurrent requests, storage cost, time granularity, pricing, and search performance. State which you would choose and why.
Part 3: Follow-up
Add support for extending an existing reservation, for example moving the end date later. What changes in the API and in the data model? Consider conflicts with the next reservation, pricing, payment, and concurrent edits.
State any assumptions you make, such as scale and consistency needs.
Hints
Hint 1
Decide first whether each resource is a single unit (one car, one cabin) or a pool of identical units (hotel room types). It changes how you prevent double-booking.
Hint 2
Treat reservation creation as a state machine (pending, confirmed, canceled, expired) with a hold that expires, and make mutating calls idempotent with a client-supplied key so retries do not duplicate bookings or charges.
Hint 3
For the extension, model it as a conditional change to the end of the existing range: check only the added slice for conflicts, charge only the difference, and use a version number to detect concurrent edits.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Assumptions
- Each resource is a single unit, so the central invariant is "no two active reservations overlap for the same resource."
- Reads dominate: roughly 100 search QPS on average with peaks near 1K, versus about 1 write QPS from bookings.
- Search may be eventually consistent (seconds). Booking must be strongly consistent.
- Clients retry, so every mutating call must be idempotent.
2. API
All endpoints are versioned (/v1) and authenticated. Mutating calls accept an Idempotency-Key header.
Search
GET /v1/resources/search?location=...&start=...&end=...&limit=20&cursor=... returns 200 with a list of results, each with a price quote, plus next_cursor. GET /v1/resources/{id}/availability?from=&to= returns busy intervals for a calendar view.
Create reservation (places a hold)
POST /v1/reservations with {resource_id, start, end, quote_id} returns 201 with {reservation_id, status: "PENDING_PAYMENT", hold_expires_at, price, version}.
409if the range overlaps another reservation (include the conflicting window).422for an invalid range (end not after start, below minimum stay, beyond the booking horizon).410if the quote expired.
Pay and confirm
POST /v1/reservations/{id}/payments with a payment method returns 200 and status CONFIRMED.
402if declined,410if the hold expired,409if the reservation is not pending.
View, list, cancel
GET /v1/reservations/{id}returns200, or404when it does not exist or belongs to someone else (avoids leaking IDs).GET /v1/users/me/reservations?status=upcoming&cursor=...lists with cursor pagination.POST /v1/reservations/{id}/cancelwithIf-Match: <version>returns the canceled reservation and refund. UsePOSTrather thanDELETEbecause cancellation is a state transition with side effects and the record must remain.409if already started or canceled,412on version mismatch.
3. Data model
resources(id PK, owner_id, geo, tz, base_price, min_stay, buffer_minutes, status)
INDEX GIST(geo), INDEX(owner_id)
reservations(id PK, resource_id FK, customer_id FK, start_ts, end_ts, status,
total_amount, currency, hold_expires_at, version, created_at)
INDEX(customer_id, start_ts DESC) -- "my reservations"
INDEX(resource_id, start_ts) -- owner calendar, overlap lookups
INDEX(status, hold_expires_at) -- expiry sweeper
payments(id PK, reservation_id FK, kind, amount, provider_ref, status, idempotency_key UNIQUE)
idempotency_keys(key PK, user_id, request_hash, response_body, created_at)
reservation_events(id, reservation_id, type, payload, created_at) -- audit and outbox
Search is served from a derived index (search engine or read replica) fed by events through an outbox. The authoritative availability check always happens inside the booking transaction on the primary database.
4. Inventory models
Per-date inventory
resource_day(resource_id, day, state, reservation_id, price, PK(resource_id, day)).
Booking locks the rows in [start, end) in date order, checks all are free, then marks them taken. Alternatively insert rows and let the primary key reject duplicates.
- Pros: trivial and portable conflict check (works even on key-value stores); per-day price and overrides sit on the row; "free on day X" is an equality lookup.
- Cons: row explosion (300K resources x 365 days is about 110M rows per year, pre-materialized); fixed granularity, so hourly handoffs and turnaround buffers do not fit, and going hourly multiplies rows by 24; long stays touch many rows, raising lock contention and deadlock risk (mitigate by locking in a consistent order).
Interval-based inventory
holds(id, resource_id, reservation_id, kind, period RANGE, active) with an exclusion constraint on (resource_id, period &&) where active.
In PostgreSQL the exclusion constraint makes double-booking impossible at the database level, even under concurrent inserts. Pad the range by the buffer to enforce turnaround. Because the constraint cannot reference now(), a sweeper sets active = false when holds expire.
- Pros: compact (one row per reservation or block); arbitrary time precision; changing a reservation is a single range edit; mirrors the business concept.
- Cons: needs range or interval index support, and elsewhere you must emulate it by locking the resource row and checking
existing.start < :end AND existing.end > :start; prices are computed rather than stored; "which resources are free" is an anti-join and is slower for search.
Recommendation
Use intervals as the source of truth for correctness and fine granularity, and maintain a derived per-day or per-hour bitmap in the search index for fast filtering. Search can be slightly stale because the booking transaction rechecks.
5. Booking flow and concurrency
POST /reservationsruns in one transaction: check the idempotency key, insert the reservation asPENDING_PAYMENT, insert an active interval. A constraint violation maps to409.- The hold lasts about 10 minutes. Payment authorizes the card using a provider-side idempotency key, flips status to
CONFIRMED, and emits an outbox event. Capture can happen later. - A sweeper expires stale holds, freeing the interval.
- If payment succeeds after the hold expired, void the authorization rather than confirming.
6. Follow-up: extending a reservation
API: POST /v1/reservations/{id}/extensions with {new_end} and If-Match: <version> (or PATCH with the same precondition). Response 200 with the updated reservation and an amount_due for the added time.
409if the added slice conflicts with the next reservation or a block; return the latest possible end so the client can offer it.412on a version mismatch,422ifnew_endis not after the current end or violates limits,409if the reservation is not in an extendable state.
Data model changes:
- With intervals, extend by growing the range in place. The exclusion constraint validates only the new slice against others, atomically. In a per-date model, you must lock and insert the extra day rows.
- Bump
version. - Record an
extensionrow (or an event) holding old end, new end, price delta, and payment reference, so the history is auditable and a failed payment can be rolled back. - Price only the delta using the rates for the added slice, which may differ from the original rate.
Flow and edge cases: extend the interval tentatively, authorize the delta, then commit. If payment fails or times out, shrink the range back. Make it idempotent with a key. Two simultaneous extension requests are serialized by the version check and the constraint. Cancellation after an extension should refund according to the policy applied to each segment.
7. Common mistakes
- Checking availability in the search index and trusting it at booking time.
- Using
DELETEfor cancellation and losing the record. - Missing idempotency, causing double charges on retries.
- Treating an extension as a brand new booking, which can lose the original slot while the new one is checked.
- Ignoring time zones and buffer time between reservations.