Design Memory-Efficient Loading of a Large Dataset
Design a server that loads a 10 GB text file of about 40 million records and answers lookups by ID while staying inside a fixed memory budget, including during the daily reload.
Problem statement
A server has to read in a multi-million-line dataset. Loading it the obvious way (turn every line into objects in memory) needs several times more RAM than the data takes on disk.
Design how the data is loaded and laid out, and how it is served under the assumptions below, so the server's memory stays low and predictable.
For this exercise, assume the server is a read-only lookup service: callers fetch one record by its ID. An upstream job delivers a complete new text file once a day, one record per line, with about 40 million lines.
Each line holds a unique ID of up to 20 characters and 11 tab-separated fields (numbers, dates and short text with many repeated values); callers need only the ID and 5 of those fields. Each server has 16 GB of RAM and a local SSD and holds the full dataset.
You may use any storage you can justify, including an embedded store on the server or a separate database.
Lookups must keep working while a new file loads, every lookup must answer from one complete version of the file (never a mix of old and new), and a load that fails or looks wrong must leave the previous version live.
What to build
- When a new daily file arrives, the service loads it and starts answering from it only after the load finishes and passes checks.
- A caller fetches a record by its ID and gets the ID and its 5 needed fields from the live version, or a clear not-found.
- Lookups keep working while a file loads; a failed or suspicious load leaves the previous version live.
At scale
- For this exercise, assume each file has about 40 million lines of about 250 bytes each, so about 10 GB per file.
- The service process may allocate at most 8 GB of the server's 16 GB at any moment, including during a reload; the OS file cache (free RAM the operating system uses to keep recently read file pages) is outside that budget.
- Each server handles about 5,000 lookups per second with p99 latency (the time 99% of lookups beat) under 20 ms, also while a file loads.
- A new file should be live within 1 hour of its arrival.