SteelReed

Search

Search posts, questions…

5. Spring Boot

Requirements analysis and data design through a hotel booking example

Using a hotel booking example to turn a vague brief into a verifiable spec (open questions, scope, BDD, measurable NFRs), then designing the entities, reviewing the design, the ERD and the DDL, seen through a senior engineer's eyes, then applying the method to the E-Learning pet project.

17 Nov 20268 min readMid

–

1. The problem

The brief: "users search hotels by location, guests and dates; view details; then book". A dev opens the IDE and adds an available_rooms column to the room type. A week later, December bookings eat January's rooms, and two people both book the last room.

2. Key idea

A specification is the blueprint before building. In a pet project you decide yourself and write down the assumptions.

Step What to do Example
Open questions list what's unclear is "location" a city or coordinates? does price vary by date? what if two people book the last room?
Scope write down "out of scope" too, to stop scope creep out: real payments, cancellations/refunds, multiple currencies
BDD Given / When / Then covering happy path, edge cases, errors see section 3
NFRs with numbers a number + a load + how to measure search p95 < 300 ms at 50 req/s; never overbook
Design nouns become entities, then review where it will break the 5 spots below

2.1 Five places the first design breaks

# First design Fix
1 One available_rooms column room_inventory: room type × night, total rooms, booked, that night's price
2 Bookings without a status PENDING → CONFIRMED → COMPLETED, or CANCELLED/EXPIRED
3 Images and amenities as comma-separated text hotel_image, amenity tables + a join table
4 Reviews not tied to a stay a unique booking_id: no "fake" reviews
5 Money as FLOAT/DOUBLE DECIMAL; the booking stores the price at booking time, not a join to the current price
Enforcing rules Use when Don't use when
DB constraints (CHECK, UNIQUE, FK) invariants that must never break: no overbooking, no duplicate bookings business rules that change often (coupons, promotions)
Checks in code friendly error messages, complex rules as the only safety net

3. Worked example

GHERKIN
Scenario: Two people race for the last room
  Given Deluxe has exactly 1 room left on 2026-12-02
  When two requests to book 1 room arrive at the same time
  Then exactly one gets 201 and the other gets 409
 
Scenario: Resending the same request
  Given I booked successfully with Idempotency-Key "abc"
  When I resend exactly that request with the same key
  Then I get the original booking back, with no second booking
SQL
CREATE TABLE room_inventory (
  room_type_id BIGINT NOT NULL,
  stay_date    DATE   NOT NULL,
  total_rooms  INT    NOT NULL,
  booked_rooms INT    NOT NULL DEFAULT 0,
  price        DECIMAL(12,2) NOT NULL,                        -- price per night
  PRIMARY KEY (room_type_id, stay_date),
  CONSTRAINT chk_no_overbooking CHECK (booked_rooms BETWEEN 0 AND total_rooms)  -- MySQL 8.0.16+
);
-- booking: ... status VARCHAR(20), final_price DECIMAL(12,2), idempotency_key VARCHAR(64),
--          CONSTRAINT uk_booking_idem UNIQUE (user_id, idempotency_key)

Before 8.0.16, MySQL accepts CHECK syntax and ignores it. Each BDD scenario becomes a test; the "never overbook" NFR becomes a concurrency test plus a constraint.

Applying it to E-Learning
  • Open questions: how many teachers per course; what "lesson completed" means (90% of the video watched? the student clicks?); when an enrolment moves START → PROCESSING → DONE; can you rate before finishing.
  • BDD: enrol in a course; duplicate enrolment (409); finishing the last lesson marks the course DONE; you can't mark a lesson from another course.
  • NFRs: "my courses" p95 < 300 ms with 50 enrolments per student; progress updates p95 < 150 ms and idempotent.
  • Trap: user_course.status and user_course_lesson.status are two sources for one truth. Update both in one transaction, write down the rule for adding lessons to a DONE course, and run a reconciliation query regularly.

4. Working with AI

Delegate to AI: listing open questions from the brief, drafting BDD, generating the ERD and DDL from the entity list.

Check yourself:

  • There's an "out of scope" list and written assumptions.
  • Every NFR has a number, a load and a measurement method.
  • There are scenarios for concurrent contention and resent requests.
  • Inventory is per night, not one total column; money is DECIMAL.
  • Key invariants have DB constraints, not just code checks.

5. Exercises

Exercise 1: Design a messaging app's database

Users chat 1–1 or create group rooms. AI proposes two message tables: direct_message(sender, receiver, …) and group_message(group_id, …). How would you design it?

Solution to Exercise 1
  • Treat a 1–1 chat as a conversation with exactly two members: one conversation table, one conversation_member(conversation_id, user_id, last_read_message_id), one message(conversation_id, sender_id, content, created_at, deleted_at). Two message tables double the queries and code.
  • A direct_key column = "lower id:higher id" + unique: two people only ever have one 1–1 chat, even when both click "message" at once.
  • last_read_message_id instead of a per-message read table: one row per person per room instead of per person per message.
  • Load older messages with keyset pagination: WHERE conversation_id = :c AND id < :beforeId ORDER BY id DESC LIMIT 50, not OFFSET.
  • Billions of messages: partition/shard by conversation_id.

Exercise 2: Review AI-written NFRs

AI wrote: "The system must be fast, stable, secure and easy to scale". Rewrite it as 4 verifiable NFRs for hotel booking.

Solution to Exercise 2
  • Fast: search p95 < 300 ms at 50 req/s with 10,000 hotels; verified with a k6 load test.
  • Stable: 99.9% monthly availability (~43 minutes downtime); an uptime monitor.
  • Correct: rooms booked per night ≤ total rooms; a concurrency test + CHECK. Bookings idempotent by Idempotency-Key for 24 hours.
  • Secure: only the booking's owner or an admin can view it; authorization tests. Drop "easy to scale", or turn it into a threshold ("handles 5x current load by adding instances").

Summary

  • Open questions → scope → BDD → NFRs with numbers → design.
  • The most skipped question: two people booking the last room.
  • Inventory per room type × night; bookings store a price snapshot.
  • Money is DECIMAL; invariants get CHECK and UNIQUE in the DB.
  • Write down assumptions and the out-of-scope list.

Next: Part 2: APIs and overbooking.

Practice interview questions on this topic

All Data Modeling & Scaling questions →All System Design questions →All Behavioral questions →

Loading comments…