Skip to content

About

The Seqnum Service is a microservice that generates unique sequence numbers.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Last updated : 28th March 2023

About Seqnum Service

The Seqnum Service is a microservice that generates unique sequence numbers.

How conceptually it works ?

How it works

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

How tests works ?

  • 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.

How To ? ( with make )

git clone ...
make clean
make build 
make run
./seqnum_get.sh

How To ? ( with docker )

 git clone ..
 make docker 
 docker-compose up -d
 ./seqnum_get.sh

Help : refer build-logs

seqnum-build-logs.txt

About

The Seqnum Service is a microservice that generates unique sequence numbers.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

⌂ 换网址