> For the complete documentation index, see [llms.txt](https://huy312100.gitbook.io/software-development/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://huy312100.gitbook.io/software-development/system-design/fundamental/design-a-unique-id-generator-in-distributed-systems/step-3-design-deep-dive.md).

# Step 3 - Design deep dive

In the high-level design, we discussed various options for designing a unique ID generator in distributed systems. We settled on an approach that is based on the Twitter snowflake ID generator. Let us dive deep into the design.

Datacenter IDs and machine IDs are chosen at the startup time and generally fixed once the system is up and running. Any changes in datacenter IDs and machine IDs require careful review since an accidental change in those values can lead to ID conflicts. Timestamp and sequence numbers are generated when the ID generator is running.&#x20;

### Timestamp&#x20;

The most important 41 bits make up the timestamp section. As timestamps grow with time, IDs are sortable by time. Figure 7-7 shows an example of how binary representation is converted to UTC. You can also convert UTC back to binary representation using a similar method.

<figure><img src="/files/nATiqxeJ5FBqGKhFHKbL" alt=""><figcaption></figcaption></figure>

The maximum timestamp that can be represented in 41 bits is&#x20;

2 ^ 41 - 1 = 2199023255551 milliseconds (ms), which gives us: \~ 69 years = 2199023255551 ms / 1000 seconds / 365 days / 24 hours/ 3600 seconds.&#x20;

This means the ID generator will work for 69 years and having a custom epoch time close to today’s date delays the overflow time. After 69 years, we will need a new epoch time or adopt other techniques to migrate IDs

### Sequence number&#x20;

A sequence number is 12 bits, which gives us 2 ^ 12 = 4096 combinations. This field is 0 unless more than one ID is generated in a millisecond on the same server. In theory, a machine can support a maximum of 4096 new IDs per millisecond.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huy312100.gitbook.io/software-development/system-design/fundamental/design-a-unique-id-generator-in-distributed-systems/step-3-design-deep-dive.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
