当前位置:网站首页>I heard that distributed IDS cannot be incremented globally?
I heard that distributed IDS cannot be incremented globally?
2022-06-12 17:15:00 【Structure ferry】
Click on the blue words above “ Set to star ”
Hello everyone , I am a 【 Structure ferry 】, A ten-year procedural ape . This is the eleventh article in the practical experience series , This series will share a lot of useful experience in practical work , If there is harvest , Please share it with more friends .
We have time to add an article instead of the previous one ID Sort by page , The reason is that it is connected to the distributed network ID, But distributed ID There is no guarantee of order , Only global uniqueness can be guaranteed .
So today, let's discuss , Whether we can achieve orderly distributed ID Well ?

Distributed ID How to implement
Segment mode
The number segment mode is currently used to implement distributed ID The way , The number segment mode obtains a segment range in advance , Then all in memory ID Distribution of , Extremely high performance .
There are also many open source frameworks implemented in the number segment mode , For example, meituan's Leaf, Dripping Tinyid. If you don't know the implementation principle of the number segment mode, you can check the following address for in-depth study . It is difficult to realize increment in segment mode , At the end of the article, let's talk about whether there is any way to realize .
Leaf: https://github.com/Meituan-Dianping/Leaf
Tinyid: https://github.com/didi/tinyid
Snowflake
Snowflake yes Twitter Open source distributed ID generating algorithm , It is also used more in China . For example, baidu open source uid-generator Is based on Snowflake The algorithm is improved .
uid-generator:https://github.com/baidu/uid-generator
Snowflake Generate ID Performance is very good , But also meet the increasing requirements . But it depends on the time on the machine , If the time is inconsistent, there will be a problem of repetition .
Redis Incr
Redis You can use it directly Incr Command to increment numbers , So as to realize self increasing ID. If you use Redis Implement distributed ID Need to carry out ID The persistence of , Otherwise, it will disappear after restart, and there will be repeated problems .
Since you want to persist , Then you can't avoid using AOF perhaps RDB Achieve persistence . Use RDB Data may be lost , Lead to ID It happens repeatedly . Use AOF Be sure to configure the mode of brushing data as always, Each operation record is synchronized to the hard disk , In this way, we can ensure that the data is not lost , But the performance is poor . Of course Redis 4.0 And there's hybrid mode (AOF+RDB) Can be used is also a good choice .
Whether in a stand-alone or cluster environment , Some key It must be routed to a node . although Redis Stand alone can also support 10 Ten thousand basic QPS, Suppose your ID The demand exceeds this magnitude , This will become a bottleneck , Because you have to ensure self growth , There is no way to expand horizontally .

What do you want to do in an orderly way ?
Segment mode data will not be lost , High performance , It's an excellent scheme . However, the number segment mode cannot achieve global increment ID, If you want to achieve global increment , Then you can't split , There must be a fixed node , All requests are obtained from this node ID Talent . But the segment pattern is the characteristic of segmentation , Horizontal expansion can be achieved through segmentation , Lifting performance .
Segment mode can only realize local increment , For example, my order form is connected to distributed ID, If you use segment mode , Then there will be my first order ID yes 20001, Then place the second order ,ID It could be 10003. If you use ID null , There will be a problem of out of order , This is also why the previous article used high-precision time to sort .
In this case , Is it OK to ensure that the data under the same user is incremental , This is local increment . If you want to achieve local increment, you need to consider the following issues :
Fixed route

ID A service node hangs up or restarts
Suppose the user A It's always routed to id-service-b above , here id-service-b Hang up , Then you need to find a new node to route , The problem is coming. , Route to a new node , The assumption is id-service-c.
And then id-service-c above ID Paragraph may be better than id-service-b Big , If it's big, it's ok , Or increasing . If it's small, there's a problem , So this problem needs to be solved .
How to solve the above problems ?
Personally, I don't think this problem is easy to solve , Because the users who route to a node are N, It is also impossible to the existing ID The query , Determine whether it is greater than the of the service to be routed ID paragraph . The whole logic is too complicated .
There is a simple way , When there is a fixed routing requirement ID When you want to switch routes , Lock the whole situation ( At this time, if there is a large amount of concurrency, it will affect the user's RT), Put this ID All corresponding segments are cleared , In this way, a new segment is applied for when rerouting , The new segment must be larger than the previous segment , That is, increasing .


summary
In fact, if you want to realize a demand , There are many ways behind it . How to choose a suitable , The best solution is to test your usual accumulation and breadth of technology . You need to know the principle of each scheme , What's the difference , Which one costs less , Which one can fully meet the business needs .
original : Structure ferry ( official account ID:jiagoubaiduren), Welcome to share , Reprint please keep the source .

边栏推荐
- First acquaintance with go language
- STL——函数对象
- R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
- 反馈式编译
- Sudo of uabntu
- 使用ubantu时,遇见的一些小毛病和解决方法
- 使用GCC的PGO(Profile-guided Optimization)优化整个系统
- Crazy temporary products: super low price, big scuffle and new hope
- 5-5 configuring MySQL replication log point based replication
- (5) Outputs and outputs
猜你喜欢

云开发坤坤鸡乐盒微信小程序源码

Google浏览器调试技巧

修改自己电脑连接的路由器的配置网址192.168.1.1

Qiushengchang: Practice of oppo commercial data system construction

男神女神投票源码 v5.5.21 投票源码

Saturated! Can't future programmers work anymore?

Gerrit+2 triggers Jenkins task

初识GO语言

多种Qt的开发方式,你选择哪种?

Uniapp wallpaper applet source code / double ended wechat Tiktok applet source code
随机推荐
Cloud development kunkun chicken music box wechat applet source code
Play kubernetes every 5 minutes summary
(8) Goto keyword
uabntu的sudo
快速入门scrapy爬虫框架
Alibaba cloud image station supports IPv6!
Microsoft Office MSDT Code Execution Vulnerability (cve-2022-30190) vulnerability recurrence
Advanced Qt development: a preliminary study QT + OpenGL
Download PHP source code of leaf sharing station
Memory control of node
5、Embedding
启牛开的证券账户安全吗?合法吗?
Some minor problems and solutions encountered when using ubantu
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
MySQL提权总结
Making nearly $90billion, Buffett's latest heavy stock exposure
R语言使用plot函数可视化数据散点图,使用font.axis参数指定坐标轴刻度标签的字体类型为斜体字体(italic)
2022-2028 global press dehydrator industry research and trend analysis report
Unit sshd. service could not be found
Male god goddess voting source code v5.5.21 voting source code