当前位置:网站首页>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 .

边栏推荐
- Su directly switches to super administrator mode, so that many error reports can be avoided
- The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti
- How to view, modify, and delete SSH
- The R language uses the plot function to visualize the data scatter chart, and uses font The axis parameter specifies that the font type of the axis scale label is italic
- (7) Loop statement for
- Advanced Qt development: a preliminary study QT + OpenGL
- C#期末复习编程题(老师猜的)
- 5-5配置Mysql复制 基于日志点的复制
- Cloud development kunkun chicken music box wechat applet source code
- 新媒体运营素材网站分享,让你创作时事半功倍
猜你喜欢

su直接切换到超级管理员模式,这样很多报错都可以避免了
Memory control of node

Microsoft Office MSDT代码执行漏洞(CVE-2022-30190)漏洞复现

Gerrit触发Jenkins SonarQube扫描

Volcano engine held a video cloud technology force summit and released a new experience oriented video cloud product matrix

Dongfeng Yueda Kia, Tencent advertising and hero League mobile game professional league cooperate to build a new E-sports ecology

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

Fiddler抓包几种常用功能介绍(停止抓包、清空会话窗内容、过滤请求、解码、设置断点......)

JVM内存模型与本地内存

How to view, modify, and delete SSH
随机推荐
Interesting LD_ PRELOAD
每天5分钟玩转Kubernetes | 汇总
JVM memory model and local memory
Selenium element positioning
Gerrit触发Jenkins SonarQube扫描
布局管理中的sizePolicy的策略问题
Sizepolicy policy in layout management
5、Embedding
JS using RSA encryption and decryption
Making nearly $90billion, Buffett's latest heavy stock exposure
Su directly switches to super administrator mode, so that many error reports can be avoided
Some minor problems and solutions encountered when using ubantu
软件工程 学生信息管理系统 结构化的需求分析
启牛开的证券账户安全吗?合法吗?
写技术博客的意义
redis. clients. jedis. exceptions. JedisDataException: NOAUTH Authentication required
(八)goto关键字
Recommend 6 open source projects of yyds
记录使用yolov5进行旋转目标的检测
Feedback compilation