Add bookmyshow module: seat booking with hold expiry and payment timeout, naive vs pessimistic vs optimistic inventories, race and deadlock demos, JDBC strategies on H2 (8 test classes, 8 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b5YFRisPHrLBR2dRu9
This commit is contained in:
asmhatre
2026-11-17 01:03:44 +05:30
co-authored by Claude Sonnet 5.5
parent deb924febd
commit c57031aaeb
42 changed files with 2322 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
# bookmyshow
Companion code for the ankurm.com article **Low-Level Design: BookMyShow Seat Booking in Java (Handling Concurrent Bookings)**.
A seat-booking core with hold expiry and payment timeout, built three ways: a naive inventory that double-books,
a pessimistic one (per-seat locks) and an optimistic one (compare-and-set). Every figure and console line in the
article is quoted from a file in `output/`, which the JUnit tests write while asserting the same numbers. There is no
`docs/` folder: the explanations live in the article itself.
## Versions
| Component | Version | Verified against |
|---|---|---|
| JDK | Temurin 25.0.5+8 (LTS) | `java -version` |
| JUnit | 6.1.3 | `junit-bom` `maven-metadata.xml` on Maven Central (latest GA; `6.2.0-M1` is a milestone) |
| H2 (tests only) | 2.5.252 | `com.h2database:h2` `maven-metadata.xml` on Maven Central |
| Maven | 3.9.11 | `mvn -version` |
## Quickstart
```bash
cd bookmyshow
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
./scripts/run-all.sh # runs every test and regenerates output/*.txt
```
The first Maven run must be online so the plugins land in the local cache. Three transcripts contain counts that
depend on thread timing (marked "control" or "timing dependent" inside the file); they differ from run to run by design.
## The pieces
| Class | Role |
|---|---|
| `Show`, `Seat`, `SeatCategory` | the screening and its seats; price depends on the seat category |
| `SeatSlot`, `SeatState` | one seat's whole state as an immutable value: state, owner, expiry, version |
| `SeatInventory` | the one interface where seat state changes: `tryHold`, `confirm`, `release` |
| `NaiveInventory` | check-then-act on a `ConcurrentHashMap`; double-books |
| `PessimisticInventory` | one `ReentrantLock` per seat, locks taken in sorted order |
| `OptimisticInventory` | `AtomicReference<SeatSlot>` per seat, compare-and-set, roll back on conflict |
| `JdbcSeatStore` | the same question in SQL: optimistic version column, `FOR UPDATE`, one conditional `UPDATE` |
| `BookingService` | hold, pay, confirm; refund when the hold is lost during payment |
| `MutableClock`, `FakePaymentGateway` | deterministic time and a payment provider the tests can slow down |
## The transcripts
| File | Written by | What it shows |
|---|---|---|
| `output/01-happy-path.txt` | `HappyPathTest` | one user at a time: all three inventories behave identically |
| `output/02-naive-race.txt` | `RaceConditionTest` | the naive inventory granting one seat twice |
| `output/03-lock-ordering.txt` | `LockOrderingTest` | deadlock with unsorted locks, mutual refusal with unsorted optimistic holds |
| `output/04-contention.txt` | `ContentionTest` | 200 threads, one seat (timing dependent control line) |
| `output/05-random-seats.txt` | `ContentionTest` | 400 users, random seats, invariants that must hold |
| `output/06-expiry-and-payment.txt` | `ExpiryAndPaymentTest` | hold expiry, slow payment, resold seat, declined card |
| `output/07-pitfalls.txt` | `PitfallsTest` | partial holds, foreign releases, bad input, double Pay |
| `output/08-database-strategies.txt` | `DatabaseSeatTest` | the same race against H2 (timing dependent control line) |
`SeatInventory` implementations expose small test seams (`betweenCheckAndAct`, `afterFirstLock`, `afterFirstAcquire`,
`beforeRollback`) so the tests can force an interleaving on every run. They are test hooks, not part of the design.
## License
MIT, see the repository root.