Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Distributed ID Generation

Nathan Kleyn
February 25, 2015

Distributed ID Generation

The theory and code behind generating distributed unique IDs using Redis.

Nathan Kleyn

February 25, 2015

More Decks by Nathan Kleyn

Other Decks in Programming


  1. Hey Bob, what’s the time? It’s 1970! Back in my

    day... Fucking Bob always thinks it’s 1970. It’s clearly 1980. Look at my hair! No, Karen. It’s 1990. I’m not wearing this antiquated Disney shirt for kicks.
  2. However it has no unsigned variants.* This is no longer

    strictly true in Java 8, however it’s a trick: they’re still signed types, but now there’s a bunch of functions to do unsigned operations on them (eg. unsignedDivide).
  3. So in Java the MSB is for the sign: 1000000000000

    1 when negative and 0 when positive. So the above is a short and is -4096.
  4. So in 1 year it will be 31,536,000. Unix time

    will be ~1,456,348,092,000.
  5. Note this is the first part of the ID, so

    it has the most bearing on sorting.
  6. We allow 10 bits for this ID, so we can

    have up to 1024 ID generation machines.
  7. We give a fixed ID to each Redis server and

    it stamps its IDs with this ever after.
  8. What happens if you ask the same Redis server to

    generate multiple IDs in a millisecond?