Software engineering notes

System Design

How can you generate a key that is guaranteed to be unique?

The possibility to get duplicates by UUIDv4

UUIDv4 generates a random id using random numbers, with 122 random bits. The chances of generating a duplicate are extremely low but not zero.

To understand the probability, consider that the number of version 4 UUIDs which can be generated is 2^122 or about 5.3x10^36.

To put these numbers into perspective, the annual risk of a given person being hit by a meteorite is estimated to be one chance in 17 billion , which means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for approximately 100 years would the probability of creating a single duplicate reach 50%.

The approaches to improve the collision issue

Another way to make sure that the generated key is unique

Types of bounds

(TODO) Cache strategy

ref: