Skip to main content
⚙️ Systems Primitives

What you're building

A lightweight key-value store with time-to-live (TTL) expiry — the core primitive behind Redis, Memcached, and every in-memory cache.

Requirements

Build a KVStore class:

  • set(key, value, ttlMs) — store a value. If ttlMs is provided (> 0), the entry expires after that many milliseconds. If ttlMs is omitted or 0, the entry lives forever.
  • get(key) — return the value, or undefined if missing or expired. Expired entries should be treated as absent.
  • delete(key) — remove a key. Return true if it existed (and had not expired), false otherwise.
  • has(key) — return true if the key exists and has not expired.
  • size() — return the count of non-expired keys.

Expiry is lazy: check at access time using Date.now(). No setInterval or timers.

Stretch goals

  • keys() — return an array of all non-expired keys.
  • flush() — delete all entries.

What this proves

You can model time-sensitive state, understand lazy evaluation vs. eager cleanup, and implement the exact pattern Redis uses for key expiry. Every caching layer in a web app relies on this.

⚡ 40 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
set and get a value
missing key returns undefined
delete returns true then false
size counts only live keys

Run your code to check these.