Service, distributed has become the first choice for system development, high concurrency operation in the data storage, need a set of ID generator services to ensure the distribution of global uniqueness, to ensure that the system's order creation, transaction payments and other scenarios under the unique data, otherwise it will cause immeasurable losses. based on time stamp
For example, the serial number rule is as follows: Xx-yyyymmdd-n bit random number, this is the enterprise application development commonly used rules. This serial number is friendly to people, the recognition is high, but the capacity is limited by the following random number, and the data quantity is bigger, the difficulty is higher when it is generated. The first three parts of the daily flow number is basically fixed, after the generation of n-bit random numbers, the need to verify that there is no previous, can rely on the set mechanism of Redis, the random number every day is written to a set set of more than 4.2 billion of the set easily, fully enough to, after the rebuild to be compared with the set set up, To ensure its uniqueness. Do not repeat within one day, and then combine the date to ensure its uniqueness.
When the n-bit random number is generated, it can be generated from the system time stamp and a large number modulo. Uuid/guid
The simplest form of direct violence, while guaranteeing the uniqueness of IDs, does not meet the many other features required by the business system, such as the rough ordering of time, and the ability to reverse and manufacture. In addition, the UUID produced by using full time data, performance is poor, and the UUID is longer, occupy a large space, indirectly resulting in database performance degradation, more importantly, the UUID is not orderly. The system can be used when the capacity is small, and it is not recommended to use this method when it becomes larger. Vesta
GitHub Address: Https://github.com/robertleepeak/vesta-id-generator
Vesta is a general ID generator, the internet is commonly known as a unified generator, it has a globally unique, rough and orderly, and can be reversed and can be manufactured, and so on, it supports three publishing modes: Embedded publishing mode, central server release mode, rest release mode, according to the business performance requirements, It can produce two types of maximum peak and minimum granularity of IDs, its implementation of architecture so that it has high performance, high availability and scalability, such as the quality of Internet products required attributes, is a general-purpose high-performance hair device products. Provides 4 types of application deployment, depending on scenario: Rest Publishing Mode (Netty) Rest Publishing mode (TOMCAT) central server publishing mode embedded publishing mode Twitter-snowflake
GitHub Address: Https://github.com/twitter/snowflake
The background of the Twitter-snowflake algorithm is quite simple, and in order to satisfy Twitter's request for tens of thousands of messages per second, each message must be assigned a unique ID, which requires some sort of approximate order (convenient for client ordering), And the IDs produced by different machines in the distributed system must be different.
The structure of the snowflake is as follows (used-apart): 0-0000000000 0000000000 0000000000 0000000000 0-00000-00000-000000000000
The first bit is unused, the next 41 bits are milliseconds (41-bit length can be 69 years), then 5-bit Datacenterid and 5-bit Workerid (10-bit length supports deployment of 1024 nodes), The last 12 digits are counted within milliseconds (12-bit counting sequence numbers support each node to produce 4,096 ID numbers per millisecond)
Add up to just 64 digits, a long type. (Convert to string length 18)
The IDs generated by Snowflake are sorted by time, and the entire distributed system does not generate ID collisions (differentiated by datacenter and workerid) and is more efficient. It is said that snowflake can produce 260,000 IDs per second. Distributed ID Generator based on Redis
GitHub Address: Https://github.com/hengyunabc/redis-id-generator
Relies on Redis's Eval,evalsha two commands to generate a unique ID on each node through the Lua script, using Redis's Lua script execution function. The generated ID is 64 bits: Use the bit to store time, accurate to milliseconds, and can be used for 41 years. Using the bit to store the logical fragment ID, the maximum fragment ID is 4095 using a bit to store the growth ID, meaning that each node can generate up to 1024 IDs per millisecond
Redis provides the time command to obtain the number of seconds and microseconds on the Redis server. Because some LUA scripts return a four-tuple. Second, microsecond, partition, SEQ
The client processes itself to generate the final ID. ((second * 1000 + microsecond/1000) << (+)) + (Shardid <<) + seq;
In the Redis-id-generator-java directory, there are example and benchmark codes that provide Java client generation mode, and other languages can support the Redis Evalsha command as long as they are available. MongoDB's Objectid
Each document in the MongoDB collection must have a "_id" key, and the value of the key can be of any type and, by default, a Objectid object. MongoDB's obejctid production idea is worth our reference in many aspects, especially in the development of large-scale distributed, how to build lightweight production, how to transfer the load of production, how to improve the maximum optimization of production by space in exchange for time and so on.
Online has an article analysis can also recommend to everyone http://www.blogjava.net/dongbule/archive/2011/06/12/352138.html.