I'm looking at the Instagram blog post about sharded ID generation. This blog post describes generating 64-bit identifiers. Their mechanism allocates 41 of the 64 bits to a millisecond timestamp, and they say:
- 41 bits for time in milliseconds (gives us 41 years of IDs with a custom epoch)
Is this a typo? I calculated that you can store 69 years of millisecond timestamps in 41 bits. Here's how:
So, where am I wrong?
Timestamp field summary Seconds and fraction of seconds: timestamps are represented as a 64-bit unsigned fixed-point number, in seconds relative to 00:00:00 January 1, 1970, UTC. The integer part is in the first 32 bits (Unix-style timestamp) and the fraction part in the last 32 bits.
You must always use 64 bit timestamps for milliseconds since epochs!
Unix Time in Milliseconds Another option is to represent timestamps using the number of milliseconds since the Unix epoch instead of the number of seconds.
You're not wrong about the calculation.
(2^41)-1 ms
== 2199023255.551 s
== 610839.7932086 hr
== 25451.65805036 days
== 69.6828 Julian years
== 69.6843 Gregorian Years
Which lines up closely with your result (69 years
).
However, the website you link to does say that 41 bits
gives them
41 years of IDs with a custom epoch
"Epoch" in this context is probably referring to the start date. Given that that article was published "3 years ago", or in 2012
, we can calculate that their epoch begins in 2012 + 41 - 69 == 1984
. This date was possibly chosen as a reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With