Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5YFRisPHrLBR2dRu9
63 lines
3.4 KiB
Markdown
63 lines
3.4 KiB
Markdown
# 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.
|