Katkat

Compare Locks and MVCC in a Versioned Store

NuroSoftware Engineer · Onsite

Last updated

Tests deep understanding of concurrency control paradigms by comparing pessimistic locking against Multi-Version Concurrency Control (MVCC) in a versioned key–value storage engine, focusing on version lifecycle, garbage collection, and read/write trade-offs.

Scenario

You are designing the core storage engine for a high-throughput, versioned key-value store used to record autonomous vehicle telematics, map state overlays, and sensor calibration parameters. The system must support concurrent reads and writes, historical point-in-time snapshot reads, and atomic batch mutations.

You are asked to evaluate two concurrency control architectures: Strict Two-Phase Locking (2PL) / Reader-Writer Locks versus Multi-Version Concurrency Control (MVCC).

Requirements

Provide an in-depth comparative analysis addressing the following specific dimensions:

1. Conceptual Distinction: Logical Versions vs. Snapshot Isolation

  • Explain how a user-facing concept of "versioned keys" (e.g., fetching key K at revision 10) differs fundamentally from snapshot isolation in MVCC.
  • Define how read timestamps, write timestamps, and visibility masks are managed so readers never block writers.

2. Concurrency Under Heavy Write Contention

  • Contrast how 2PL and MVCC handle concurrent writes to the same key or overlapping key ranges.
  • Discuss conflict detection mechanisms in MVCC: First-Committer-Wins (optimistic conflict check) vs. pessimistic row locks on uncommitted versions.

3. Deletions and Version Reclamation (Garbage Collection)

  • Describe how deletions are represented (e.g., tombstone records with valid-time intervals [created_ts, deleted_ts)).
  • Detail an efficient garbage collection (GC) strategy (e.g., vacuuming, cooperative compaction, epoch-based reclamation) to prune dead versions without stalling active long-running snapshot readers.

4. Memory, Disk, and Performance Trade-offs

  • Compare the operational overhead of MVCC (index bloat, secondary index updates, write amplification, memory footprint) against lock-based designs (lock manager memory, deadlock detection, convoy effects under high read-write contention).
  • Provide concrete recommendations on when a storage engine should favor 2PL over MVCC, and vice versa.

Hints

Hint 1

Differentiate between application-level data versioning (explicit semantic versions) and internal engine MVCC timestamps used for point-in-time transaction consistency.

Hint 2

Consider what happens to long-lived read snapshots when high-frequency writes trigger continuous garbage collection—how do watermarks or epochs prevent premature deletion?

Hint 3

Think about the cost of secondary indexing in MVCC: if an in-place update creates a new row version, does every secondary index pointing to that row also need updating?

Follow-up questions

  • How does write skew manifest in MVCC snapshot isolation, and what mechanisms (e.g., Serializable Snapshot Isolation / SSI) are used to prevent it without full serial execution?
  • If a vehicle client requests a snapshot read from 24 hours ago, how do you handle historical queries when GC aggressively purges dead tuples to save disk space?
  • How do Append-Only/Out-of-Place MVCC models (like PostgreSQL or RocksDB) compare with In-Place/Undo-Log MVCC models (like MySQL InnoDB) regarding read latency and vacuum overhead?