Sharding easy
In a sharded database, why is it typically a bad idea to rely on a standard database `AUTO_INCREMENT` column for your Primary Keys?
- A Integer-based IDs are fundamentally slower for the database to search through than text strings.
- B Managing a simple integer takes more CPU power than generating a complex UUID.
- C Each shard generates its own sequence independently, which almost guarantees 'ID Collisions' (e.g., Shard A and Shard B both assigning ID `1001` to different records).
- D Sharded database systems generally do not support any numerical data types for Primary Keys.
Answer: C. Each shard generates its own sequence independently, which almost guarantees 'ID Collisions' (e.g., Shard A and Shard B both assigning ID `1001` to different records).
If a system is spread across 5 servers, and each server starts counting from 1, you'll end up with duplicate IDs everywhere, making it impossible to aggregate or reference that data correctly. To solve this, distributed systems use 'ID Generators' like Snowflake (Twitter), UUIDs (Random), or a centralized 'Ticket Server'. These methods ensure that every single record in the entire global cluster has a unique identifier regardless of which machine created it.
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)?