Sharding medium
Distributed ID generators like Twitter's 'Snowflake' create 64-bit unique IDs using a format like `[Timestamp] + [Machine ID] + [Sequence]`. Why is the Timestamp placed first (the most significant bits)?
- A To allow the IDs to be automatically compressed by specialized network protocols.
- B To ensure that IDs are perfectly random and impossible to predict for security purposes.
- C To make the IDs 'K-Sortable' (roughly ordered by time), which allows database B-Tree indexes to remain efficient by appending new records at the end rather than inserting randomly.
- D To make the ID compatible with legacy 32-bit systems that only understand time-based integers.
Answer: C. To make the IDs 'K-Sortable' (roughly ordered by time), which allows database B-Tree indexes to remain efficient by appending new records at the end rather than inserting randomly.
Traditional UUIDs are random, which causes 'Index Fragmentation' in standard databases like MySQL; the B-Tree must constantly split pages to insert random values in the middle of the index. By putting the timestamp first, Snowflake IDs are naturally chronological. This ensures that new records are mostly appended to the end of the database files, maintaining high write performance while still allowing many thousands of servers to generate unique IDs independently without a central 'ID bottleneck' service.
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)?