Concurrent caching client for byte-range file reads
I was asked this
No reports yet. Sign in to say where you were asked it.
A system design interview question asked in Databricks's recruiter screen, last asked Aug 2025. It tests caching, concurrency and file systems.
Design a high-throughput client-side cache that serves ranged reads of files from a remote storage service. The remote API can fetch a byte range by file name, offset and length, and there is also a method that downloads a whole file given its name and size. Many threads use one client at once and often ask for overlapping or adjacent ranges. Give the key data structures and pseudocode for the read path, and analyze correctness under concurrency, scalability and cost.
Requirements
- Functional:
read(filename, offset, length) -> bytesserved from the cache when possible, from the remote otherwise. - Functional: overlapping or adjacent requests, concurrent or not, share fetched data; two threads never download the same bytes at the same time.
- Functional: detect when a remote file changes and stop serving stale bytes.
- Non-functional: bounded memory (and optionally disk) with eviction; thread safety; limited concurrent remote requests (backpressure); retries and timeouts on partial failures.
Clarifying questions
- Typical file sizes and read sizes? Are reads sequential or random?
- Are files immutable, or can they change? Is there a version or ETag?
- Memory budget, and is a disk tier allowed? Must the cache survive restarts?
- Is the client read-only, or must it support writes (write-through or write-back)?
- How expensive is a remote call compared with its size (latency against bandwidth)?
What a strong answer covers
- Fixed-size chunks (for example 1 to 8 MB) as the unit of caching: a read maps to a list of chunk ids, which turns overlapping ranges into shared chunks. The chunk-size trade-off between read amplification and request count.
- An index from
(file, version, chunk)to cached data, plus an in-flight map from chunk to a future, so concurrent readers of a missing chunk wait on one download. - The read path in pseudocode: compute chunks; for each, return the cached copy, join an in-flight future, or start a fetch under a lock-protected check-and-insert; assemble the requested slice.
- Coalescing adjacent missing chunks into one remote request, sequential-read detection with prefetch, and the whole-file method for small files.
- LRU eviction by bytes, never evicting chunks being read (reference counts or pins); an optional disk tier with checksums for persistence.
- Validation: key the cache by file version or ETag; re-check on open or periodically; drop old versions.
- Backpressure with a semaphore on remote requests, per-request timeouts, retries with backoff, and failures propagated to every waiter and removed from the in-flight map.
- Locking granularity: striped or per-chunk locks instead of one global lock.
Follow-ups
- A thousand threads read the same hot 10 MB range at startup.
- Support writes. Write-through or write-back, and how do readers see them?
- Several processes on one machine share a disk cache. How do they coordinate?
- The remote file changes while a multi-chunk read is in progress. What does the reader get?
Where it's asked
- Databricks · Recruiter screen · Last asked Aug 202530 min · Call
What it tests
caching · concurrency · file systems
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
