About 'global unique id'


Comparison between UUID and Snowflake


When you need to ensure a globally unique ID, you can choose UUID or Snowflake (and its variants)

Among them, UUID does not rely on any third-party system and has better performance and usability;

The IDs generated by Snowflake are monotonically increasing (you can get the timestamp information when generated) and can contain certain business meanings (such as the length of registration time). It is better to use Snowflake when it comes to sharding tables.

alt

In terms of occupying database space, Snowflake divides 64-bit binary numbers into several parts. Therefore, if stored in int type, the maximum is 9223372036854775807, which does not exceed the upper limit of int64, such as 1628304129979846657


alt

The composition of uuid [1]

The UUID is a string of 32 hexadecimal digits, such as cd7460be-1593-41b1-9542-2b9a690f9dd2

snowflake algorithm Demo:

package main

import (
   "fmt"

   "github.com/bwmarrin/snowflake"
)

func main() {

   // Create a new Node with a Node number of 1
   node, err := snowflake.NewNode(1)
   if err != nil {
      fmt.Println(err)
      return
   }

   // Generate a snowflake ID.
   id := node.Generate()

   // Print out the ID in a few different ways.
   fmt.Printf("Int64  ID: %d\n", id)
   fmt.Printf("String ID: %s\n", id)
   fmt.Printf("Base2  ID: %s\n", id.Base2())
   fmt.Printf("Base64 ID: %s\n", id.Base64())

   // Print out the ID's timestamp
   fmt.Printf("ID Time  : %d\n", id.Time())

   // Print out the ID's node number
   fmt.Printf("ID Node  : %d\n", id.Node())

   // Print out the ID's sequence number
   fmt.Printf("ID Step  : %d\n", id.Step())

   // Generate and print, all in one.
   fmt.Printf("ID       : %d\n", node.Generate().Int64())
}

Output:

Int64  ID: 1628304129979846656
String ID: 1628304129979846656
Base2  ID: 1011010011000111001011111101010100101110000000001000000000000
Base64 ID: MTYyODMwNDEyOTk3OTg0NjY1Ng==
ID Time  : 1677052931672
ID Node  : 1
ID Step  : 0
ID       : 1628304129979846657

UUID Demo:

package main

import (
   "fmt"

   "github.com/satori/go.uuid"
)

func main() {
   // Creating UUID Version 4
   // panic on error
   u1 := uuid.Must(uuid.NewV4(), nil)
   fmt.Printf("UUIDv4: %s\n", u1)

   // or error handling
   u2 := uuid.NewV4()
   fmt.Printf("UUIDv4: %s\n", u2)

   // Parsing UUID from string input
   u2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
   if err != nil {
      fmt.Printf("Something went wrong: %s", err)
      return
   }
   fmt.Printf("Successfully parsed: %s", u2)
}

Output:

UUIDv4: cd7460be-1593-41b1-9542-2b9a690f9dd2
UUIDv4: 2c598a64-e685-4ab2-9021-9aa352aaefad
Successfully parsed: 6ba7b810-9dad-11d1-80b4-00c04fd430c8

UUID


alt


Universally Unique Identifier (English: Universally Unique Identifier, abbreviation: UUID) [2]


UUID is a standard defined by the Open Software Foundation (OSF), and GUID is Microsoft's implementation of the UUID standard and is currently widely adopted. There are various other implementations of UUID, not just GUID.

The generated uuid is composed of a set of 32-digit hexadecimal numbers, so the theoretical total number of uuid is 16 to the 32nd power, that is, 2 to the 128th power, which is approximately equal to 3.4 x 10 to the 38th power. In other words, if 1 trillion UUIDs are generated every nanosecond (ns), it will take 10 billion years to use up all the UUIDs.

The standard form of UUID contains 32 hexadecimal digits, divided into five segments with hyphens, and has a 32-character format of 8-4-4-4-12 . Example:

550e8400-e29b-41d4-a716-446655440000

UUID encoding rules:

  • 1)1~8位采用系统时间,在系统时间上精确到毫秒级保证时间上的惟一性;
  • 2)9~16位采用底层的IP地址,在服务器集群中的惟一性;
  • 3)17~24位采用当前对象的HashCode值,在一个内部对象上的惟一性;
  • 4)25~32位采用调用方法的一个随机数,在一个对象内的毫秒级的惟一性。

通过以上4种策略可以保证惟一性。在系统中需要用到随机数的地方都可以考虑采用UUID算法。


最知名的标准是RFC4122[3]

很有意思的是这个标准没有第二版[4]

alt

uuid现在共有5个版本,版本1不够安全,版本4有可能发生重复,但概率极低.现在大多使用uuid4

UUID 不同版本的区别及选择[5]

alt

UUID4发生碰撞的概率:

alt

go版常用实现 uuid-rs/uuid[6]

alt

rust版常用实现 uuid-rs/uuid[7]

alt
alt

Twitter的Snowflake算法


[UUID和雪花(Snowflake)算法该如何选择?](https://cloud.tencent.com/developer/article/1766264 "UUID和雪花(Snowflake "UUID和雪花(Snowflake)算法该如何选择?")算法该如何选择?")

分布式ID生成方案--雪花算法和UUID对比[8]

几种分布式id生成方式[9]


其他方案实现「分布式环境下的全局唯一发号器」


图解各路分布式ID生成算法[10]

六种方式实现全局唯一发号器[11]

面试总被问分布式ID? 美团(Leaf)了解一下[12]

美团技术分享:深度解密美团的分布式ID生成算法[13]

面试总被问分布式ID怎么办? 滴滴(Tinyid)甩给他[14]


参考资料

[1]

uuid的组成: https://www.google.com/search?q=uuid&newwindow=1&sxsrf=AJOqlzW1mf2ohYNWjOwjuSh70Di0-aYINg:1677053193032&source=lnms&tbm=isch&sa=X&ved=2ahUKEwii7Nv-1aj9AhVNNt4KHQ7rCmQQ_AUoAXoECAEQAw&biw=1920&bih=990&dpr=1.8

[2]

通用唯一识别码(英语:Universally Unique Identifier,缩写:UUID): https://zh.wikipedia.org/wiki/%E9%80%9A%E7%94%A8%E5%94%AF%E4%B8%80%E8%AF%86%E5%88%AB%E7%A0%81

[3]

RFC4122: https://segmentfault.com/a/1190000000484508

[4]

没有第二版: https://docs.python.org/zh-cn/3.11/library/uuid.html

[5]

UUID 不同版本的区别及选择: https://www.jianshu.com/p/76e3a75605ed

[6]

uuid-rs/uuid: https://github.com/satori/go.uuid

[7]

uuid-rs/uuid: https://github.com/uuid-rs/uuid

[8]

分布式ID生成方案--雪花算法和UUID对比: https://blog.csdn.net/qq_40950903/article/details/108589837

[9]

几种分布式id生成方式: https://www.cnblogs.com/kuotian/p/12869914.html

[10]

图解各路分布式ID生成算法: https://i6448038.github.io/2019/09/28/snowflake/

[11]

六种方式实现全局唯一发号器: https://blog.csdn.net/qq_20051535/article/details/120072174

[12]

面试总被问分布式ID? 美团(Leaf)了解一下: https://juejin.im/post/5e61b4f26fb9a07cb83e2eee

[13]

美团技术分享:深度解密美团的分布式ID生成算法: https://zhuanlan.zhihu.com/p/83753710

[14]

What should I do if I am always asked about distributed ID during interviews? Didi (Tinyid) dumped him: https://cloud.tencent.com/developer/article/1598569

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/techdashen/article/details/132909672