Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
Where it shows up
The same pattern appears across everyday software.
PostgreSQL: SKIP LOCKED
`SELECT ... FOR UPDATE SKIP LOCKED` passes over rows that others have locked. The manual recommends it for several consumers of a queue-like table, which is a free list of spaces and several gates.
HikariCP connection pool
Its `ConcurrentBag` holds database connections. A connection is borrowed with `compareAndSet(STATE_NOT_IN_USE, STATE_IN_USE)`: the claim of this page, with connections as spaces.
Linux buddy allocator
Free memory is kept in lists by block size. If the wanted size is out, a larger block is split. A fallback upwards, with one idea more: the rest goes back to the lists.
Java Semaphore
The class documentation uses a pool of 100 items as its example: a permit first, then the item from a structure with its own lock. The same split as capacity and free list.
Stripe: idempotent requests
A request can carry an idempotency key, so a retry after a broken connection does not charge twice. This is the `key` in `Payments.charge`.
Stripe: amounts in minor units
Amounts are whole numbers in the currency's minor unit: 1000 is 10 US dollars, and 10 is 10 yen, because yen has no decimals. Money as integers, with the currency beside it.
San Francisco parking prices
SFMTA adjusts meter and garage prices to demand from time to time. Several rules by place and time of day: the reason why pricing sits behind an interface.
Redis: SET with NX and EX
`SET key value NX EX seconds` sets a key only if it does not exist, and lets it expire. One line for a hold that ends by itself, as in a reservation.