Design a Unique ID generator

vipul pachauri
3 min readJul 13, 2023

Hi Guys, let’s discuss how we can design a unique ID generator in a distributed environment. We will look for the design as a discussion between a candidate and a interviewer. Okay, let’s start.

Interviewer: How would you design a unique ID generator. Any questions before we proceed?

Candidate: Yes, what are the characteristics of the unique IDs ?

Interviewer: The IDs must be unique and sortable. For each new record, the ID should increment by one. However, the increment can be based on time rather than a fixed value. IDs should only contain numerical values. The ID length should fit into 64 bits.

Candidate: What is the expected scale of the system?

Interviewer: The system should be capable of generating 10,000 IDs per second.

Candidate: Great, based on these requirements, let’s summarize them. The IDs must be unique, numerical, and fit into 64 bits. They should be ordered by date and time. The system should generate over 10,000 unique IDs per second. Have I captured the requirements correctly?

Interviewer: Yes, that’s an accurate summary. Understanding the requirements is crucial before proceeding with the design.

Candidate: Excellent. Now let’s propose a high-level design for the unique ID generator and gather your feedback. There are multiple options we can explore, such as a multi-master application or using UUIDs. Let’s discuss each approach in detail.

Interviewer: Sure, let’s start with the multi-master application. This approach involves using the auto-increment feature in a traditional database. However, it becomes challenging to implement in a distributed environment. The incrementing process across multiple databases with minimal delay is complex.

UUID

Candidate: Understood. Another option is to use UUIDs. UUIDs are 128-bit numbers that provide a low probability of collisions. They can be generated independently by each web server without coordination between them.

Interviewer: That’s correct. UUIDs offer simplicity and scalability as each web server generates its own IDs. However, there are drawbacks, such as the length of the IDs exceeding our 64-bit requirement and the absence of a time correlation between the ID and the time of generation.

Twitter Snowflake ID generator algo

Candidate: I see. Another approach we can consider is the Twitter Snowflake ID generator. It divides the ID into different sections, including a timestamp and data center ID. It provides unique IDs that are sortable by time and offers scalability.

Interviewer: Yes, the Snowflake generator seems promising. It addresses our requirements and can handle distributed environments effectively. We can deep dive into its design and implementation.

Candidate: Agreed. After considering all the options, I recommend settling on the Twitter Snowflake ID generator. It supports our use cases and provides scalability in a distributed environment.

Interviewer: That sounds like a solid decision. The Snowflake generator is a reliable choice. Additionally, we should ensure clock synchronization, consider the impact of low concurrency, and prioritize high availability for the mission-critical system.

Candidate: Absolutely. Clock synchronization is assumed in our design, but we can explore options to optimize low-concurrency scenarios. High availability is crucial for the ID generator, and the system should be designed accordingly.

Interviewer: Well done. You’ve comprehensively discussed the design options and made informed decisions based on the requirements. This concludes our discussion on designing a unique ID generator.

Candidate: Thank you. It was a great learning experience to analyze and propose a solution for the unique ID generator problem.

Reference : Alex Xu System Design Interview Book

--

--