vehicle_arrived.vehicle_left and closes the session so billing is computed from actual duration.lot_id so each lot is an independent unit of scale.Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
POST /api/parking-lot
{ "lot_id": "L-001", "slots": 50, "slot_types": { "regular": 40, "ev": 6, "accessible": 4 } }
201 Created
{ "lot_id": "L-001", "slots_created": 50 }
Errors: 400 malformed body, 409 lot_id already exists, 401 unauthenticated, 429 rate-limited.
GET /api/parking-lot/{lot_id}/availability?start=2025-06-01T10:00&end=2025-06-01T12:00
{ "lot_id": "L-001", "available": 12, "price_quote": { "amount": 300, "currency": "IDR", "per_hour": 150 } }
Read path → served from the Redis counter, so it stays p95 < 200 ms. This is the call the app makes before showing "Reserve".
POST /api/reservation/hold
{ "lot_id": "L-001", "license_plate": "B1234XYZ", "start": "...10:00", "end": "...12:00" }
201 Created
{ "reservation_id": "L-001-8f3a", "spot_number": 15, "state": "HELD", "expires_at": "...10:10" }
Errors: 409 no spot available in window (return 3 alternatives), 409 plate already has an active session, 429.
Two headers that cost one line and impress: Idempotency-Key (retried hold returns the same reservation, not a second one) and If-Match/version for conflict. Note this endpoint is the write path that hits the DB transaction — the availability call above is advisory only.
POST /api/reservation/{reservation_id}/pay
{ "payment_method_id": "pm_x", "amount": 300 }
200 OK → { "reservation_id": "...", "state": "CONFIRMED", "payment_status": "PAID" } Errors: 402 payment declined → hold stays HELD until TTL, 409 hold expired (spot released), 410 reservation already cancelled.
POST /api/gate/check-in { "lot_id": "L-001", "license_plate": "B1234XYZ" } → 200 { "allowed": true, "spot_number": 15 } — p95 < 300 ms, this one is latency-critical. POST /api/gate/check-out { "lot_id": "L-001", "license_plate": "B1234XYZ" } → 200 { "state": "COMPLETED", "billed_duration_min": 118, "amount": 300 }
GET /api/parking-lot/{license_plate} → 200 { "license_plate": "B1234XYZ", "lot_id": "L-001", "spot_number": 15, "state": "CONFIRMED" } / 404 not parked.
DELETE /api/reservation/{reservation_id} → 204, releases the spot immediately.
User searches → API Gateway authenticates and rate-limits → Reservation Service reads cached capacity → user reserves a spot (state: HELD) → Transaction Service initiates payment with the PSP → PSP webhook confirms → Reservation Service marks it CONFIRMED and decrements committed capacity. At the gate, the device calls Reservation Service to validate the reservation before lifting the barrier.
Choice: relational (PostgreSQL), single logical schema. Allocation needs ACID plus a uniqueness constraint to make double-booking impossible at the storage layer. Fast "find a vacant spot" reads are served by a Redis counter and read replicas on top — not by swapping to NoSQL, which would cost you the constraint that actually solves the hard problem.
| Entity | Key attributes |
lot | lot_id PK, name, capacity, contact_info_id FK |
spot | spot_id PK, lot_id FK, floor, spot_type, status (FREE/OCCUPIED) |
vehicle | vehicle_id PK, license_plate UNIQUE, type, user_id FK |
user | user_id PK, name, contact_info_id FK |
contact_info | contact_info_id PK, phone, email |
reservation | reservation_id PK, lot_id, spot_id FK, vehicle_id FK, user_id FK, start_time, end_time, payment_status, completion_status (HELD/CONFIRMED/COMPLETED/EXPIRED) |
transaction | transaction_id PK, reservation_id FK, vehicle_id, user_id, amount, check_in, check_out, status |
I'd deep-dive three: (1) the Spot Allocation Engine, (2) the Gate Controller, (3) the Availability/Capacity Service. The interviewer spends most of their attention on #1.
The mechanism (this is the whole point of the section): the database is the arbiter, not the application. In PostgreSQL:
CREATE EXTENSION btree_gist;ALTER TABLE reservation ADD CONSTRAINT no_overlap EXCLUDE USING gist ( spot_id WITH =, tstzrange(start_time, end_time, '[)') WITH && ) WHERE (completion_status IN ('HELD','CONFIRMED'));
The transaction does SELECT ... FOR UPDATE SKIP LOCKED over candidate spots, inserts the hold, and commits. If two users pick the same spot+window, the second insert violates no_overlap (SQLSTATE 23P01) → that request gets a 409 with the next available alternatives, the first keeps the hold. Winner and loser are decided at commit, by the DB.
Check-then-commit race eliminated: the "is a spot free?" availability test happens inside the same transaction as the insert, and the constraint is the final gate. Never: read availability → return to client → insert later.
Flow for the race:
Hot-lot surge on one window (say a concert ends and 200 users hit the same lot in 10 s): partition the work by lot_id and serialize per lot with a bounded queue; the tail sheds with fast 409 + Retry-After instead of piling onto the DB. Constraint-violation retries use jittered exponential backoff with a cap of ~3 attempts, then fall back to "any spot in this lot." Allocation stays correct because contention only changes who wins, never whether two people can win.
Cache is advisory: Redis holds a per-lot free-spot counter and a spot bitmap for search/UI. It may be stale by seconds — that only causes a retry, because admission is decided by the DB write above.
Idempotency: client sends Idempotency-Key; UNIQUE(idempotency_key) on reservation. A retried reserve returns the same hold ID rather than creating a second hold.
Hold TTL: holds last 10 minutes. A sweeper every 30 s flips HELD → EXPIRED, and every commit query additionally ignores holds where now() > expires_at — belt and suspenders, so a stalled sweeper can't leak inventory. Renewal re-validates under the same exclusion constraint.
Interval semantics: half-open [start, end). 11:00–12:00 and 12:00–13:00 coexist; 11:00–12:01 overlaps. Overlap = start_a < end_b AND start_b < end_a.
Scale sanity: 500 spots/lot, 20 req/s peak per lot on a hot window, ~50 ms per commit → a single writer per lot is nowhere near saturation, which is why I don't need a distributed lock service.
Tradeoff table worth saying out loud:
| Approach | Pro | Con |
| Exclusion constraint (chosen) | Correct by construction, no app locking | Needs range/GiST support; conflict surfaces as an error |
SELECT FOR UPDATE row lock | Simple, familiar | Lock on a spot row blocks reservation inserts for adjacent windows too; deadlock risk |
| Per-lot single writer / actor | Trivial reasoning, cheap | Hot-lot throughput bounded; extra infra |
| Optimistic version column | No locks | Requires a free-slot index ritual; thundering retries |
Edge device at the barrier: camera/ALPR or QR scan → POST /api/gate/validate → opens in p95 < 300 ms, or the driver sits there honking. So the controller keeps a local signed allowlist cache of confirmed reservations for its lot (refreshed every 10 s + on push). If the cloud is unreachable, it admits against the cache and reconciles on reconnect — a 20-minute-old signed token can't be forged, and any over-admission is captured as an exception record rather than a locked barrier. Exit is always permitted (never trap a car over a payment glitch); unpaid sessions become a debt record.
Per-lot free-spot counter in Redis (INCR/DECR, not read-modify-write) plus a spot_id → status bitmap for "nearest free spot" queries, refreshed from DB changes. It serves search, dashboards, and the "lot is full" sign. It is explicitly not on the admission path — that's the whole reason it's allowed to be eventually consistent.