Sharding medium
In an E-Commerce platform, why might you choose to shard the `Orders` table by `user_id` rather than the more obvious `order_id`?
- A Because it is significantly easier for the database to index strings like 'UserIDs' than random sequence integers.
- B To achieve 'Colocation', ensuring that all of a specific user's orders reside on the same physical shard as their main user profile for high-performance retrieval.
- C To prevent the 'Order ID' sequence from running out of available unique numbers across multiple servers.
- D Because `order_id` is typically protected by data privacy laws that prevent it from being used in routing algorithms.
Answer: B. To achieve 'Colocation', ensuring that all of a specific user's orders reside on the same physical shard as their main user profile for high-performance retrieval.
This is a classic 'Data Design' trade-off. If you shard by `order_id` (the most unique key), you get perfect write balancing, but if a user wants to view their order history, the database must query ALL 10 shards (Scatter-Gather) to find them. If you shard by `user_id`, you gain massive efficiency for user-specific pages because all those records are on one machine. The risk, however, is that if one user places millions of orders (a 'Whale' account), you will create a massive 'Hotspot' on the shard that holds that user's data.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- 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)?
- In a sharded architecture, what does the 'Scatter-Gather' query pattern imply for application performance?