Scaling Writes hard
Why do Log-Structured Merge (LSM) Trees (used in Cassandra/RocksDB) offer significantly higher write throughput than B-Trees (used in PostgreSQL)?
- A They transform random database updates into sequential writes (Append-Only) in memory and on disk, deferring the expensive sorting/merging to a background process.
- B They do not use a transaction log.
- C They compress the data before writing.
- D B-Trees require locking the entire table for every insert.
Answer: A. They transform random database updates into sequential writes (Append-Only) in memory and on disk, deferring the expensive sorting/merging to a background process.
B-Trees often require random I/O to update leaf nodes in place. LSM Trees write everything sequentially to a MemTable and then flush to an SSTable. Sequential I/O is vastly faster, especially on HDDs, but also reduces write amplification on SSDs initially.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- What does the 'Atomicity' property in a database transaction guarantee regarding writes?
- Your database is struggling with write traffic. You decide to 'Scale Up' (Vertical Scaling) by buying a bigger server. What is the eventual hard limit of this strategy?
- A developer sets up a 'Fire-and-Forget' write pattern (sending data without waiting for a database confirmation) to speed up the app. What is the major risk?
- Why is inserting 1,000 rows in a single 'Batch' query faster than executing 1,000 separate `INSERT` queries?
- In the Raft consensus algorithm, what mechanism prevents a 'Split Vote' (where no leader gets a majority) from stalling the cluster indefinitely?
- Your 'Logs' table has 10 billion rows and writes are getting slow. You barely access logs older than 1 year. What is the simplest strategy to improve performance?