Scalable Global ID Generator Design

此为某国外大公司系统设计面试题目 :)

参考: http://stackoverflow.com/questions/2671858/distributed-sequence-number-generation

这里将其中关于 Twitter Snowflake 的部分摘出来:

 

Snowflake is a:

  • Networked service, i.e. you make a network call to get a unique ID;
  • which produces 64 bit unique IDs that are ordered by generation time;
  • and the service is highly scalable and (potentially) highly available; each instance can generate many thousand IDs per second, and you can run multiple instances on your LAN/WAN;
  • written in Scala, runs on the JVM. 

The main ideas are:

  • The most significant 40 or so bits: A timestamp; the generation time of the ID. (We're using the most significant bits for the timestamp to make IDs sort-able by generation time.)

  • The next 14 or so bits: A per-generator counter, which each generator increments by one for each new ID generated. This ensures that IDs generated at the same moment (same timestamps) do not overlap.

  • The last 10 or so bits: A unique value for each generator. Using this, we don't need to do any synchronization between generators (which is extremely hard), as all generators produce non-overlapping IDs because of this value.

 

posted @ 2016-01-12 14:23  william-cheung  阅读(403)  评论(0编辑  收藏  举报