Scaling Writes medium
You have a high-concurrency counter. Instead of using heavy database locks, you implement Optimistic Locking. How does this work?
- A It queues requests in Redis.
- B It ignores concurrency conflicts.
- C It locks the entire table.
- D It uses a version number column. Updates only succeed if the version hasn't changed since the read (e.g., `UPDATE ... SET count=6, ver=2 WHERE id=1 AND ver=1`).
Answer: D. It uses a version number column. Updates only succeed if the version hasn't changed since the read (e.g., `UPDATE ... SET count=6, ver=2 WHERE id=1 AND ver=1`).
Optimistic locking assumes conflicts are rare. If a conflict *does* occur (0 rows updated because version mismatch), the application catches the error and retries. This avoids the overhead of holding DB locks.
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?