Design Key-Value Store with Expiry (Low-Level Design)
An in-memory store with per-key expiry, later with nested transactions. Focus: Time-based expiry, then layered transactions.
| Difficulty | MEDIUM |
|---|---|
| Design focus | Time-based expiry, then layered transactions |
| Automated grading | 3 public + 8 hidden tests (11 total) |
| Phase 2 follow-up | Transactions |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Store string values by key. A value may have a time to live (ttlMs): it expires at now + ttlMs and after that behaves as if it was never set. Without ttlMs it never expires.
Time is passed in as `now` (milliseconds) so tests control the clock. Setting a key again replaces its value and its expiry.
Functional requirements
- set(key, value, now, ttlMs?); a ttlMs that is not positive throws.
- get(key, now) returns the value, or null if missing or expired (a key is expired when now ≥ its expiry).
- delete(key) returns true if a key was removed. count(now) counts keys that are not expired.
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
class KeyValueStore {
set(key: string, value: string, now: number, ttlMs?: number): void {
throw new Error("Not implemented");
}
get(key: string, now: number): string | null {
throw new Error("Not implemented");
}
delete(key: string): boolean {
throw new Error("Not implemented");
}
count(now: number): number {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 8 hidden edge-case tests in a sandbox:
Set and get
var s = new KeyValueStore();
s.set("a", "1", 0);
assertEqual(s.get("a", 5), "1");
assertEqual(s.get("b", 5), null);
Values expire
var s = new KeyValueStore();
s.set("a", "1", 100, 50);
assertEqual(s.get("a", 149), "1");
assertEqual(s.get("a", 150), null);
Bad time to live
var s = new KeyValueStore();
assertThrows(function () { s.set("a", "1", 0, 0); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- A stored value and its expiry travel together (an entry type): Keeping value and expiry in one record means they are always set, copied and rolled back together.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 11 tests, you unlock the change request: Transactions.
Pair with High-Level System Design
Ready to scale Key-Value Store with Expiry across distributed servers? Try the matching HLD simulator problem: Design Key-Value Store.
Relevant low-level design lessons
Ordering with ties
Write one comparison rule that every part of the code agrees on.
Time you can control
Handle expiry and windows without timers, so behaviour is testable.
Strategy: swap behaviour, not code
Replace an if/else on a type with interchangeable strategy classes.
More low-level design problems
Design Game Leaderboard
Add points, show the top players and anyone's rank, with ties handled consistently.
Design Expense Sharing (Splitwise)
Split shared expenses several ways, track who owes whom, and suggest the fewest payments to settle up.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Rate Limiter
Two rate-limiting algorithms behind one interface, so callers can swap them.