SteelReed

Search

Search posts, questions…

4. Database & SQL

Designing an airline booking database: relationships, seat inventory and overbooking

Airline data-model case study, part 1: writing relationships as sentences, the ER model, evolving v1 into v2, reference data, per-seat rows versus a counter for seat inventory, the correct overbooking formula, and indexes for the main queries.

18 Nov 20267 min readSenior

–

Selling airline tickets packs most of the hard parts of data modelling into one small domain: reference data, scarce seats, selling more seats than exist, money and audit. This post goes from v1 to v2 and fixes a few common mistakes; the SQL runs on PostgreSQL 16 (the MySQL 8.4 differences were run too). Read first: normalization, indexes and EXPLAIN, transactions and locking.

1. The problem

A 100-seat flight, 10% overbooking. Version 1 accepts a booking when booked_seats < total_seats * overbook_rate. With 109 sold, a group of 3 gets through: 112 tickets sold against a limit of 110.

2. Key idea

~400 flights/day × ~180 seats is ~26 million seat rows a year. Write load is modest, but at peak it piles onto a few rows of one flight.

Write relationships as sentences before drawing. "A flight has many bookings" sounds right; read backwards, "a booking belongs to one flight" is wrong for round trips. A sentence that needs "it depends" signals a junction table (booking_direction).

Common mistake Fix
Repeated columns departure_flight_id, return_flight_id + type a booking_direction table, one row per direction
A passengers column holding a list a booking_passenger table (1NF)
Bookings referencing the current price table snapshot the price onto the booking at purchase
Local flight times timestamptz; Hanoi → Tokyo stored locally gives a 7-hour flight instead of 5

2.1 Availability: one row per seat or a counter?

Criterion One row per seat Counter per flight and class
Choosing a specific seat built in needs a separate seat-assignment step
Peak contention spread over ~180 rows piled onto one hot row
Overbooking no: the primary key forbids selling 12A twice natural: sell_limit > capacity
Use when seats are chosen at booking, no overbooking selling by class, with overbooking

Real airlines combine them: sell from the counter, use the seat map for paid seat selection and check-in.

2.2 The correct overbooking formula

sell_limit = floor(capacity × (1 + overbook_rate))
accept q seats  ⇔  booked + q ≤ sell_limit

The v1 formula is wrong either way you read it: with rate 0.1 it sells only 10 seats; with rate 1.1 it compares before adding, so the group slips through.

3. In Java

JAVA
@Modifying
@Query(value = """
    UPDATE flight_inventory
    SET    booked = booked + :q
    WHERE  flight_id = :flightId AND fare_class = :fareClass
      AND  booked + :q <= sell_limit
    """, nativeQuery = true)
int reserve(long flightId, String fareClass, int q);
 
// sell_limit is a generated column: GENERATED ALWAYS AS (floor(capacity * (1 + overbook_rate))) STORED
// CHECK (booked <= sell_limit) is the last safety net
// reserve(flight, "Y", 3) with booked = 109, sell_limit = 110 -> 0 rows: rejected

Other languages: the UPDATE is identical.

Indexes for the main queries
Query Index
Search flights by route and date flight (departure_airport, arrival_airport, departure_time): equality first, range last
Count free seats per class, auto-assign partial (flight_id, fare_class, seat_code) WHERE status = 'AVAILABLE'
Job releasing expired holds partial (held_until) WHERE status = 'HELD'
A user's bookings, newest first booking (user_id, created_at DESC)

Partial indexes are small and fast, but each status change moves the row in or out of the index and loses HOT updates; for bookings the cost is small. Index every foreign key used by queries or deletes yourself.

4. Working with AI

Give the AI: DDL from your list of relationship sentences, sample data, EXPLAIN for the main queries.

Check in AI-written code:

  • Every relationship reads correctly both ways; no repeated columns like flight_1_id, flight_2_id.
  • Flight times are timestamptz; airports carry an IANA time_zone.
  • Overbooking is checked in the UPDATE's WHERE, using booked + q.
  • CHECKs exist: origin ≠ destination, arrival after departure, booked <= sell_limit.

5. Exercises

Exercise 1: Connecting flights

A passenger flies Hanoi → Da Nang → Phu Quoc on two flights with one itinerary ticket. The AI suggests adding a connecting_flight_id column to booking_direction. Write the relationship sentences both ways and give the right design.

Solution to Exercise 1
  • "A direction has one or more segments"; "a segment belongs to exactly one direction": 1–N.
  • "A segment uses exactly one flight"; "a flight is a segment of many bookings": N–1.

A connecting_flight_id column breaks at the first three-leg trip. Add a booking_segment (bd_id, seq_no, flight_id) table and move seat holds and coupons to the segment level.

Exercise 2: A group of 3

168 economy seats, 5% overbooking, 175 sold. A group of 3 books. What do the v1 formula (factor 1.05) and the correct one return?

Solution to Exercise 2

sell_limit = floor(168 × 1.05) = 176. V1 checks 175 < 176.4: true, selling 178, 2 over. The correct formula checks 175 + 3 ≤ 176: false, rejected; the UPDATE returns 0 rows. Partially accepting a group is a product decision (usually no).

Summary

  • Write relationships as sentences and read both ways before drawing.
  • Store flight times as absolute instants, zones on the airport table.
  • One row per seat or a counter: real airlines combine both.
  • Overbooking: booked + q ≤ floor(capacity × (1 + rate)) in the UPDATE's WHERE.
  • PostgreSQL doesn't index foreign keys automatically.

Read more: Holding seats under concurrent booking.

Practice interview questions on this topic

All Data Modeling & Scaling questions →

Loading comments…