UUID (Universally Unique Identifier), Universal Unique Identifier.
A UUID is a number generated on a machine that is guaranteed to be unique to all machines in the same spacetime
UUID consists of the following parts:
1) Current date and time
2) Clock sequence
3) Globally unique IEEE machine identification number, if there is a network card, it is obtained from the MAC address of the network card, and no network card is obtained in other ways.
Generate uuid:
public function create_guid($namespace = '') {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME'];
$data .= $_SERVER['HTTP_USER_AGENT'];
$data .= $_SERVER['LOCAL_ADDR'];
$data .= $_SERVER['LOCAL_PORT'];
$data .= $_SERVER['REMOTE_ADDR'];
$data .= $_SERVER['REMOTE_PORT'];
$hash = strtoupper(hash('md5', $uid . $guid . md5($data)));
$guid = '{' .
substr($hash, 0, 8) .
'-' .
substr($hash, 8, 4) .
'-' .
substr($hash, 12, 4) .
'-' .
substr($hash, 16, 4) .
'-' .
substr($hash, 20, 12) .
'}';
return $guid;
}
Generate logid: id generated according to the time series
public static function logid(){
$arr = gettimeofday();
$aa = ($arr['sec']*100000 + $arr['usec']/10) & 0x7FFFFFFF;
$logId = ((($arr['sec']*100000 + $arr['usec']/10) & 0x7FFFFFFF) | 0x80000000);
return $logId
}
snowflake:
In the process of migrating the storage system from MySQL to Cassandra, twitter developed a set of globally unique ID generation service: snowflake because Cassandra has no sequential ID generation mechanism. The core idea is: a long ID, using 41bit as the number of milliseconds, 10bit as the machine number, and 12bit as the serial number in milliseconds.
Summarize
algorithm |
advantage |
shortcoming |
Applicable scene |
---|---|---|---|
auto_increment |
The database is implemented by itself, which is easy to use |
There is a single point of problem and there is a write bottleneck |
Small business volume, small concurrency, absolute increase |
uuid | Local generation is time-efficient | It needs to be independently deployed and maintained. The string type is inefficient for index query, and there is a very low probability of repetition. | High concurrency and increasing trend |
snowflake | Local generation is time-efficient | Requires independent deployment and maintenance | High concurrency and increasing trend |
redis incr | Flexibility and simplicity to implement | Requires a redis call | Concurrency requirements are not high, and the trend is increasing |
idalloc | Internal maintenance service | Requires independent deployment and operation | High concurrency, increasing trend |