KatInterview

Multi-Level Key-Value Store with Expiration and Time Travel

System DesignModel answer
HardModel answerSystem Designdata-structurestime-seriesversioning

Last updated

Build an in-memory key-value store supporting CRUD, filtered queries, automatic expiration, and historical lookups across four progressive levels.

You are building an in-memory key-value store that will be extended across four progressive stages. Each stage adds new capabilities while maintaining compatibility with earlier features.

Stage 1: Core Operations

Implement fundamental operations for managing records and their fields:

  • Create and remove records
  • Add, update, and remove fields within a record
  • Retrieve entire records or individual field values

Define clear behavior for missing records or fields, and ensure all operations are idempotent.

Stage 2: Field Filtering

Add the ability to query which fields in a record satisfy given conditions. Support predicates including:

  • Equality and inequality checks
  • Numeric comparisons (less than, greater than, etc.)
  • String operations (substring matching, prefix checks, pattern matching)

Design a filter expression format and implement an API that returns matching field names.

Stage 3: Automatic Expiration

Support time-based expiration by allowing each record to have a time-to-live (TTL). Records should automatically become inaccessible once their TTL elapses. Address:

  • How expiration is detected and enforced
  • Behavior when reading or writing expired records
  • Efficient expiry tracking (aim for logarithmic or better complexity per operation)

Stage 4: Historical Queries

Enable querying past states of the database. Support reading field values or entire records as they existed at any previous timestamp. Consider:

  • How creates, updates, deletes, and expiration interact with historical reads
  • Storage strategy for maintaining version history
  • Trade-offs between query performance and memory usage

For each stage, specify:

  • API method signatures and return values
  • Core data structures and their rationale
  • Time and space complexity
  • Key edge cases and how they are handled
  • A brief test strategy

Make reasonable assumptions about scale (records, fields, operations), concurrency model, and time representation. State your assumptions clearly.

Hints

Hint 1

Choose data structures in Stage 1 that can accommodate versioning later without major refactoring

Hint 2

For expiration, consider a priority queue with lazy deletion rather than active polling

Hint 3

Historical queries need to distinguish 'record didn't exist' from 'field wasn't set' — track record lifetime separately from field versions