KatInterview

Serving Inventory From Spinning Disks With Tiny RAM

System DesignModel answer
MediumModel answerSystem Designstorage-enginecachingb-tree

Last updated

Design an inventory service on hard drives with little memory: layout records to minimize seeks, pin the index, cache hot items, batch writes, and keep reservations correct.

You are asked to build an inventory service for a machine that has rotational hard disks and only a couple of gigabytes of RAM — no SSDs, no large in-memory store. Records are keyed by item ID and hold fields like available quantity, reserved quantity, a version counter, and some variable-length descriptive metadata. Clients can read a record, update counters, and perform an atomic conditional operation such as "reserve one unit only if at least one is available." Reservations can later be committed or released, and the same request may be retried.

Explain your design end to end: how you lay data out on disk, how you index it so lookups do not cost many seeks, what you cache given the small RAM budget, what the read and write paths look like, how you make concurrent reservations safe, how you guarantee durability and crash recovery, and how you handle disks that answer only ~100–200 random I/Os per second. State your assumptions about scale with concrete numbers and justify each decision in terms of seek counts and throughput. The interviewer wants trade-offs, not slogans.

Hints

Hint 1

Start from the physics: a random disk read is 5–10 ms, so count seeks per operation and design to make that number small and constant.

Hint 2

Splitting small hot counters from bulky metadata makes each cached page carry many useful records — think about how many records fit in a 4 KB page.

Hint 3

A conditional reserve is a read-modify-write, so cheapest-write structures like LSM trees do not save you; the read dominates.

Hint 4

One fsync per write caps you near 100 writes/s; batching many writes behind a single fsync changes that by orders of magnitude.