Distributed ID Generation

224884ff0b79e700e516e24fbcae6423?s=47 Nathan Kleyn
February 25, 2015

Distributed ID Generation

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

224884ff0b79e700e516e24fbcae6423?s=128

Nathan Kleyn

February 25, 2015
Tweet

Transcript

  1. 3.
  2. 5.
  3. 11.
  4. 12.
  5. 22.

    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.
  6. 25.
  7. 28.

    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).
  8. 29.

    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.
  9. 32.
  10. 36.

    So in 1 year it will be 31,536,000. Unix time

    will be ~1,456,348,092,000.
  11. 38.
  12. 44.
  13. 48.
  14. 58.

    Note this is the first part of the ID, so

    it has the most bearing on sorting.
  15. 62.

    We allow 10 bits for this ID, so we can

    have up to 1024 ID generation machines.
  16. 63.

    We give a fixed ID to each Redis server and

    it stamps its IDs with this ever after.
  17. 65.

    What happens if you ask the same Redis server to

    generate multiple IDs in a millisecond?
  18. 70.