Detailed explanation of SnowFlake algorithm for generating unique ID in PHP

墨辰丷
Release: 2023-03-28 11:00:01
Original
1547 people have browsed it

What is the snowflake algorithm? First, let me ask a question, how to generate a unique ID in a distributed system and keep the ID roughly increasing by itself? This is the most important business scenario in Twitter, so Twitter launched a snowflake algorithm.

Foreword: I recently needed to build a CMS system. Since the functions are relatively single and the requirements are flexible, I gave up a mature system like WP and built a relatively simple one myself. The article details page URL wants to be made into a pseudo-static URL format, that is, xxx.html. xxx has considered using an auto-incremented primary key directly, but I feel that this would expose the number of articles. Some students said that the initial value can be set higher, but it is still possible. The number of articles within a period of time is calculated based on the ID difference, so an algorithm that can generate unique IDs is needed.

The methods considered are

  1. Use the timestamp directly, or a series of methods derived from it

  2. Mysql comes with uuid

Both of the above two methods can be found, so I won’t explain too much

In the end, I chose Twitter’s SnowFlake algorithm

The advantage of this algorithm is very simple. It can generate about 4 million different 16-digit IDs (decimal) per second.

The principle is very simple.

ID consists of 64bit Composition

The first bit is vacant

41bit is used to store millisecond timestamp

10bit is used to store machine id

12bit is used to store self Add ID

Except for the highest bit marked as unavailable, the remaining three groups of bit positions can be floated, depending on specific business requirements. By default, the 41-bit timestamp can support the use of this algorithm until 2082, the 10-bit working machine ID can support 1023 machines, and the serial number supports 1 millisecond to generate 4095 auto-incrementing sequence IDs.
The following is the PHP source code

Copy after login

The calling method is as follows

Particle::generateParticle($machineId);//生成ID
Particle::timeFromParticle($particle);//反向计算时间戳
Copy after login

Here I am Improvements have been made. If the machine ID is passed as 0, these 10 bits will be removed because sometimes we may not use so many IDs

The above is the entire content of this article, I hope it will be helpful to everyone's learning. help.


Related recommendations:

php unique numberid method summary, php generates into idsummary

##[php]mysql globalID Scheme, phpmysql globalid

php Detailed explanation of how to become the only id

The above is the detailed content of Detailed explanation of SnowFlake algorithm for generating unique ID in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!