Global Unique ID

🍀: Previously Implemented in Project

Approaches

  • UUID
  • Database (SQL)
  • Redis 🍀
  • Snowflake
  • Baidu
    • UidGeneratorDefaultUidGenerator
    • CachedUidGenerator
  • Meituan Leaf
    • Leaf-segment: Database
    • Leaf-snowflake

Redis

  • Single Thread
  • 通過 INCR(), INCRBY() ATOMIC 操作實作 GUID
  • 產生的資料是有序的,對排序業務有利

Design Considerations

First Thought

  • Might be to use a primary key with the auto_increment attribute in a traditional database.
  • Bad Idea:
    • auto_increment does not work in a distributed environment
    • a single database server is not large enough
    • generating unique IDs across multiple databases with minimal delay is challenging

Questions to Ask

  • What are the characteristics of unique IDs?
  • For each new record, does ID increment by 1?
  • Do IDs only contain numerical values?
  • What is the ID length requirement?
  • What is the scale of the system?

Use Cases

Multi-master Replication

Cons:

  • Hard to scale with multiple data centers
  • IDs do not go up with time across multiple servers
  • Does not scale well when a server is added or removed

UUID

Has a very low probability of getting collision.

Quoted from Wikipedia: “after generating 1 billion UUIDs every second for approximately 100 years would the probability of creating a single duplicate reach 50%”

Ticket Server

Reference: Ticket Servers: Distributed Unique Primary Keys on the Cheap

Cons:

  • Single Point of Failure (SPOF)

Twitter Snowflake Approach

  • Divide and conquer

Additional Talking Points

  • Clock synchronization (Network Time Protocol)
  • Section length tuning
  • High availability