Problem

  • Generate unique Id in distributed environment: each node can generate unique Ids.
  • Ids need to be sortable.
Snowflake
  • a service developed by Twitter for generating Id fulfilling above requirements
  • Snowflake structure: 64 bits
    • 1 unused bit: 0
    • 41 bits: timestamp in milliseconds
    • 10 bits:
      • 5 bits for datacenter id
      • 5 bits for node (server) id
    • 12 bits: sequence id, a local counter per machine that rolls over every 2^12 = 4096
id = (currentTimestamp << 22) | (nodeId << 12) | sequence
  • Performance:
    • 2^12 = 4096 id per ms per server <=> ~4 millions id per second per server
    • 41 bits for timestamp can support 2^41-1 ms ~ 69 years. Of course, we can customize our own epoch instead of using Unix epoch.
    • 5 bits for data center id: 31 data centers, each of them can deploy 31 nodes
Sonyflake
  • a distributed unique ID generator of Sony inspired by Snowflake
  • Sonyflake Id structure is a bit different:
- 39 bits for time in units of 10 msec
- 8 bits for a sequence number
- 16 bits for a machine id
  • As a result, Sonyflake has the following advantages and disadvantages:

    • The lifetime (174 years) is longer than that of Snowflake (69 years)
    • It can work in more distributed machines (2^16) than Snowflake (2^10)
    • It can generate 2^8 IDs per 10 msec at most in a single machine/thread (slower than Snowflake)

Ref: