当前位置:网站首页>Redis implements a globally unique ID
Redis implements a globally unique ID
2022-06-24 09:05:00 【Bulst】
List of articles
In distributed system, because of the cross process and cross system , In some scenes , We need to generate globally unique ID, For example, the order system , Concurrent , Different systems need to generate different orders at the same time ID It is convenient for subsequent order placing and query .
Globally unique ID Generation strategy
- UUID
- Redis Self increasing
- snowflake Algorithm
- Database autoincrement
- Baidu open source UidGenerator
- Meitu comments Leaf
Redis Self increasing ID Strategy
- One a day key, It is convenient to count the order quantity
- ID structure : Time stamp + Counter

ID Component part :
- Sign bit :1bit, For ever 0, It means a positive number
- Time stamp :31bit, Maximum 2147483648 second , Probably 69 year
- Serial number :32bit, Seconds counter , Support generation per second 2^32 Different ID
utilize redis Generate globally unique ID, Actually redis The role played is a counter , Facilitate subsequent statistics .
- advantage : High performance , High concurrency , Uniqueness , Incremental , Security .
- shortcoming : Need to rely on redis To achieve
Code implementation
/** * @author issavior */
@Component
public class RedisIdWorker {
/** * Start timestamp */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * Number of digits of serial number */
private static final int COUNT_BITS = 32;
private final StringRedisTemplate stringRedisTemplate;
public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
public long nextId(String keyPrefix) {
// 1. Generate timestamps
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2. Generate serial number
// 2.1. Get current date , Accurate to the sky
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
// 2.2. Self growth
Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
// 3. Splice and return
return timestamp << COUNT_BITS | (count == null ? 0 : count);
}
/** * obtain 2022 year 1 month 1 Number 0 spot 0 when 0 Minute timestamp * @param args */
public static void main(String[] args) {
LocalDateTime startLocalTime = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
long startTime = startLocalTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(startTime);
LocalDateTime now = LocalDateTime.now();
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm"));
System.out.println(date);
}
}
边栏推荐
猜你喜欢

Spark - the number of leftouterjoin results is inconsistent with that of the left table
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

110. 平衡二叉树-递归法

“论解不了数独所以选择做个数独游戏这件事”

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

Qingcloud based "real estate integration" cloud solution

玄铁E906移植----番外0:玄铁C906仿真环境搭建

MySQL | store notes of Master Kong MySQL from introduction to advanced

数据中台:数据中台技术架构详解

What is graph neural network? Figure what is the use of neural networks?
随机推荐
PM2 deploy nuxt3 JS project
Target detection series fast r-cnn
Mba-day25 best value problem - application problem
【LeetCode】541. Reverse string II
普通人没有学历,自学编程可以月入过万吗?
One article explains in detail | those things about growth
什么是图神经网络?图神经网络有什么用?
随笔-反思
From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
Remote connection of raspberry pie without display by VNC viewer
数据中台:中台架构及概述
MySQL data (Linux Environment) scheduled backup
金仓KFS replicator安装(Oracle-KES)
What is SRE? A detailed explanation of SRE operation and maintenance system
1528. 重新排列字符串
听说你还在花钱从网上买 PPT 模板?
China chip Unicorn Corporation
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
Sword finger offer 55 - I. depth DFS method of binary tree
1704. judge whether the two halves of a string are similar