Design in-memory DB with TTL and backup
Design a Versioned In-Memory Key → (Field → Value) Store with TTL, History, and Backup/Restore
You are tasked with designing an in-memory database that stores values at the granularity of (key, field). This database must support per-field Time-To-Live (TTL), maintain a full append-only history with validity intervals, and provide robust backup and restore capabilities to snapshot points.
API Specifications
All timestamps t are logical; ttl is in the same units. A ttl = null indicates no expiry.
-
set(key, field, value, t, ttl)- Upserts a field under a given key at a specified logical time
t. This operation must append to the history for the(key, field)pair. - Each
(key, field)entry should track its validity using[start, end)intervals, withendbeing optional. A deletion is represented by an optional tombstone interval.
- Upserts a field under a given key at a specified logical time
-
get(key, field, t)- Returns the value associated with
(key, field)only if its version visible at timethas not expired. Specifically,tmust be less thanstart + ttl(whenttlis set) ANDendmust be unset ortmust be less thanend. Otherwise, returnsnull.
- Returns the value associated with
-
delete(key, field, t)- Logs a tombstone at time
tthat effectively ends the current valid interval for(key, field). The full history for the entry must be preserved to support replay and restore operations.
- Logs a tombstone at time
-
scan(key, t)- Returns all
(field, value)pairs for a givenkeythat are visible and non-expired at timet. The results must be ordered lexicographically byfield.
- Returns all
-
scanPrefix(key, prefix, t)- Similar to
scan, but only returns(field, value)pairs where thefieldstarts with the specifiedprefix. Results must also be ordered lexicographically.
- Similar to
-
backup(t)- Records a snapshot marker at time
tfor the entire database. Returns the total number of non-expired(key, field)pairs visible attacross the whole database.
- Records a snapshot marker at time
-
restore(t)- Rewinds the observable state of the database to the most recent backup taken at or before time
t. Returns the count of non-expired(key, field)pairs from that backup. Deletions must be represented via tombstones so that the history remains fully replayable.
- Rewinds the observable state of the database to the most recent backup taken at or before time
Requirements and Clarifications
- TTL Granularity: TTL applies at the
(key, field)level, not at thekeylevel. Reads and scans must strictly exclude any expired entries. When an already-expired field is updated viaset, it should be treated as a fresh insert with a newstarttime. - Scan Ordering: All scan operations (
scan,scanPrefix) must produce deterministically ordered results. You must propose data structures that efficiently support ordered scans and prefix scans while coexisting with the requirements for TTL and history tracking. - History Tracking: Describe in detail how to track
[start, end)intervals, thevalue,ttl, anddeletetombstones for each(key, field)pair. This tracking mechanism must effectively support time-travel semantics, backup/restore operations, and efficient read access. - Complexity Analysis: Provide expected time and space complexities for each API. Discuss the trade-offs between different data structure choices, such as using per-key ordered maps versus a brute-force-plus-sort approach for scans, and the implications of global versus per-key expiry indexes.
- Garbage Collection: Explain your strategy for garbage-collecting fully expired history. This strategy must ensure that enough information is retained to support
restoreoperations to any prior backup point.
AI interview prep
Core dilemma
Balancing efficient, low-latency access to current, non-expired data with the memory and computational overhead of maintaining a full, versioned history and supporting point-in-time backup/restore, all within an in-memory constraint.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design in-memory DB with TTL and backup
Design a Versioned In-Memory Key → (Field → Value) Store with TTL, History, and Backup/Restore
You are tasked with designing an in-memory database that stores values at the granularity of (key, field). This database must support per-field Time-To-Live (TTL), maintain a full append-only history with validity intervals, and provide robust backup and restore capabilities to snapshot points.
API Specifications
All timestamps t are logical; ttl is in the same units. A ttl = null indicates no expiry.
-
set(key, field, value, t, ttl)- Upserts a field under a given key at a specified logical time
t. This operation must append to the history for the(key, field)pair. - Each
(key, field)entry should track its validity using[start, end)intervals, withendbeing optional. A deletion is represented by an optional tombstone interval.
- Upserts a field under a given key at a specified logical time
-
get(key, field, t)- Returns the value associated with
(key, field)only if its version visible at timethas not expired. Specifically,tmust be less thanstart + ttl(whenttlis set) ANDendmust be unset ortmust be less thanend. Otherwise, returnsnull.
- Returns the value associated with
-
delete(key, field, t)- Logs a tombstone at time
tthat effectively ends the current valid interval for(key, field). The full history for the entry must be preserved to support replay and restore operations.
- Logs a tombstone at time
-
scan(key, t)- Returns all
(field, value)pairs for a givenkeythat are visible and non-expired at timet. The results must be ordered lexicographically byfield.
- Returns all
-
scanPrefix(key, prefix, t)- Similar to
scan, but only returns(field, value)pairs where thefieldstarts with the specifiedprefix. Results must also be ordered lexicographically.
- Similar to
-
backup(t)- Records a snapshot marker at time
tfor the entire database. Returns the total number of non-expired(key, field)pairs visible attacross the whole database.
- Records a snapshot marker at time
-
restore(t)- Rewinds the observable state of the database to the most recent backup taken at or before time
t. Returns the count of non-expired(key, field)pairs from that backup. Deletions must be represented via tombstones so that the history remains fully replayable.
- Rewinds the observable state of the database to the most recent backup taken at or before time
Requirements and Clarifications
- TTL Granularity: TTL applies at the
(key, field)level, not at thekeylevel. Reads and scans must strictly exclude any expired entries. When an already-expired field is updated viaset, it should be treated as a fresh insert with a newstarttime. - Scan Ordering: All scan operations (
scan,scanPrefix) must produce deterministically ordered results. You must propose data structures that efficiently support ordered scans and prefix scans while coexisting with the requirements for TTL and history tracking. - History Tracking: Describe in detail how to track
[start, end)intervals, thevalue,ttl, anddeletetombstones for each(key, field)pair. This tracking mechanism must effectively support time-travel semantics, backup/restore operations, and efficient read access. - Complexity Analysis: Provide expected time and space complexities for each API. Discuss the trade-offs between different data structure choices, such as using per-key ordered maps versus a brute-force-plus-sort approach for scans, and the implications of global versus per-key expiry indexes.
- Garbage Collection: Explain your strategy for garbage-collecting fully expired history. This strategy must ensure that enough information is retained to support
restoreoperations to any prior backup point.