Design an in-memory key-value database

Quick Overview

Design an in-memory key-value database evaluates requirements, scale assumptions, API/data design, architecture, trade-offs, failure modes, and rollout in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Design an in-memory key-value database

Company: OpenAI

Role: Software Engineer

Category: System Design

Difficulty: hard

Interview Round: Technical Screen

Design an in-memory key-value database. Support set/get/delete, conditional updates (e.g., compare-and-set), TTL expiration, and atomic multi-key operations. Explain data structures to achieve O( 1) average-time reads/writes, how you track expirations (e.g., timing wheel or min-heap), memory management and eviction (e.g., LRU), and persistence via append-only log with periodic snapshots. Detail concurrency control (single-threaded event loop vs locks), crash recovery, replication and sharding for scale, consistency guarantees, backpressure, and observability. Provide API definitions, schemas for on-disk records, failure modes, and metrics.

Overview: Design an in-memory key-value database evaluates requirements, scale assumptions, API/data design, architecture, trade-offs, failure modes, and rollout in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

|Home/System Design/OpenAI
OpenAI logo
OpenAI
Jul 31, 2025
hardSoftware EngineerTechnical ScreenSystem Design
41
0

Design an in-memory key-value database

Design an In-Memory Key–Value Database (Technical Screen)

Context

Build an in-memory key–value (KV) database that offers high-throughput, low-latency operations. It should keep most data in RAM, support optional persistence, and scale across CPU cores and machines.

Assume a typical deployment runs on commodity servers with SSDs and multiple CPU cores. The system must handle high QPS with O(1) average-time reads and writes.

Functional Requirements

  1. Core operations:
    • set(key, value[, ttl])
    • get(key)
    • delete(key)
    • compare-and-set (CAS): conditional update based on version or expected value
  2. TTL expiration for keys
  3. Atomic multi-key operations (within a shard)

Non-Functional Requirements

  1. O(1) average-time reads/writes
  2. Expiration tracking (e.g., timing wheel or min-heap)
  3. Memory management and eviction policy (e.g., LRU)
  4. Durability via append-only log (AOF) with periodic snapshots
  5. Concurrency control strategy (single-threaded event loop vs locks)
  6. Crash recovery procedure
  7. Replication and sharding for scale
  8. Consistency guarantees
  9. Backpressure under overload
  10. Observability: logs, metrics, tracing

Deliverables

  • API definitions (client-facing)
  • In-memory data structures and algorithms
  • On-disk record schemas (AOF and snapshot)
  • Concurrency and transaction model (incl. atomic multi-key)
  • Crash-recovery flow
  • Replication and sharding design
  • Consistency guarantees and trade-offs
  • Backpressure strategies
  • Failure modes and metrics

Clarifying Questions to Ask Guidance

  • Clarify users, core use cases, read/write patterns, scale, latency, availability, and data retention.
  • State explicit assumptions before making sizing or architecture decisions.
  • Prioritize the functional path first, then address reliability, security, observability, and rollout.

What a Strong Answer Covers Guidance

  • A scoped requirements summary with concrete non-goals and success metrics.
  • API, data model, architecture, consistency, capacity, and operations.
  • Reasoned trade-offs among simple and scalable designs, including bottlenecks and failure modes.
  • A validation, monitoring, migration, and launch plan appropriate for the risk level.

Follow-up Questions Guidance

  • What breaks first at 10x traffic or data volume?
  • How would you degrade gracefully during dependency failures?
  • What metrics and alerts would prove the design is healthy after launch?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...