当前位置:网站首页>Redis implements a globally unique ID

Redis implements a globally unique ID

2022-06-24 09:05:00 Bulst

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

  1. UUID
  2. Redis Self increasing
  3. snowflake Algorithm
  4. Database autoincrement
  5. Baidu open source UidGenerator
  6. 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

 Insert picture description here

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);
    }
}
原网站

版权声明
本文为[Bulst]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240728174502.html