Build a Crash-Safe Append-Only Key-Value Store for Mobile
Last updated: November 29, 2026
Quick Overview
Implement a small persistent key-value store for app settings that appends every write to a log file, recovers correctly if the app is killed mid-write, and compacts the file when it grows too large.
Tencent
November 29, 20260
0
0 solved
Implement a small persistent key-value store for app settings that appends every write to a log file, recovers correctly if the app is killed mid-write, and compacts the file when it grows too large.
Tencent open-sourced MMKV, the mmap-backed key-value store WeChat uses because the platform defaults were too slow or unsafe under heavy writes. A client infrastructure onsite might ask for a simplified version to probe file I/O, record framing, checksums, crash recovery and atomic file replacement.
What the Interviewer Expects
- Designs a self-describing record format with lengths and a checksum
- Replays the log on startup and stops at the first torn or corrupt record
- Truncates the corrupt tail so later appends are valid
- Represents deletes with tombstones
- Compacts by writing a new file and atomically renaming it
- Discusses fsync cost versus durability on mobile
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would mmap change the write path and what new failure modes appear?
- How would you make this safe for two processes (app and extension) at once?
- How would you encrypt values without losing the ability to recover a torn tail?
- How do you avoid the O(n) live-size scan on every put?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
API: put(key, bytes), get(key), delete(key). Data must survive process restarts. If the process is killed in the middle of a write, the store must reopen with every fully written record intact a...
High-Level Design
Keep an in-memory dict as the source of truth for reads and an append-only log on disk for durability.
Record layout: crc32 (4 bytes) | key_len (2) | value_len (4) | key | value. A value_len of `...