Sharding hard
When building a sharded banking system, a transfer requires debiting User A on Shard 1 and crediting User B on Shard 2. If 'Two-Phase Commit' is rejected due to performance, how does the 'Saga' pattern resolve this?
- A It locks both shards simultaneously using an external hardware distributed lock manager.
- B It executes each operation as a local transaction and, if a later step fails, it executes 'Compensating Transactions' (e.g., a refund) to manually restore consistency.
- C It converts the database into a single-master architecture temporarily to process the transaction.
- D It uses a blockchain ledger to ensure that all shards eventually agree on the final balance.
Answer: B. It executes each operation as a local transaction and, if a later step fails, it executes 'Compensating Transactions' (e.g., a refund) to manually restore consistency.
Sagas trade 'Isolation' for 'Availability'. In a Saga, there is no moment where both shards are locked. Instead, you subtract money from User A. If the network fails before you can add it to User B, a background process detects the failure and 'Compensates' by adding the money back to User A. This means for a few seconds, the money is 'invisible' or 'duplicated' across the system. This 'Eventual Consistency' model is complex to develop but is the only way to scale financial systems across thousands of servers without constant lock-contention crashes.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- In an E-Commerce platform, why might you choose to shard the `Orders` table by `user_id` rather than the more obvious `order_id`?
- In the context of database scaling, what is the fundamental difference between 'Vertical Partitioning' and 'Horizontal Partitioning' (Sharding)?
- If Shard A fails in a large cluster, and its incoming traffic is automatically re-routed to Shard B, what is the risk of a 'Cascading Failure'?
- In a standard 'Modulo Sharding' setup (`key % N`), what is the primary consequence of adding a new server to the cluster (increasing N from 10 to 11)?
- From an engineering perspective, when should you choose 'Vertical Scaling' (a bigger server) over 'Horizontal Scaling' (Sharding)?
- Which sharding strategy is mathematically optimized for efficient 'Range Scans' (e.g., retrieving all records created between January and March)?