The Seqnum Service is a microservice that generates unique sequence numbers.
It generates unique sequence numbers based on a combination of timestamp and counter values.
It uses 41 bits for the timestamp, allowing for 69 years of unique IDs, and 22 bits for the counter. A mutex is used to ensure thread-safety during sequence number generation. If the current timestamp is the same as the last request, the counter is incremented; otherwise, the counter is reset, and the timestamp is updated. The unique sequence number is then generated by combining the timestamp and counter.
To understand why 41 bits allow for 69 years of unique IDs, we need to look at the number of unique values that can be represented with 41 bits and then convert that value into years.
A bit can have two values: 0 or 1. With 41 bits, you can represent 2^41 unique values. This is because for each bit position, there are two possibilities, and there are 41 bit positions. 2^41 = 2,199,023,255,552
The sequence number generator uses a timestamp in milliseconds. To convert the unique values into years, we need to divide by the number of milliseconds in a year:
milliseconds_per_year = 1000 (milliseconds per second) * 60 (seconds per minute) * 60 (minutes per hour) * 24 (hours per day) * 365.25 (days per year, including leap years) milliseconds_per_year = 31,557,600,000
Now, we can determine how many years can be represented by 41 bits:
years = (2^41) / milliseconds_per_year years ≈ 69.62
So, using 41 bits for the timestamp allows for approximately 69 years of unique IDs, given that the timestamp is in milliseconds.
Reference - https://en.wikipedia.org/wiki/Snowflake_ID
- Basic Test: Validates that the service is running and able to generate sequence numbers.
- Concurrency Test: This test sends multiple concurrent GET requests to the service and checks for any duplicate sequence numbers in the responses.
git clone ...
make clean
make build
make run
./seqnum_get.sh
git clone ..
make docker
docker-compose up -d
./seqnum_get.sh
seqnum-build-logs.txt
