In an Event-Driven-Architecture setup, messages might flow through multiple services. In such an event, it is important that each message has a unique identifier to enable correlation and debugging in the service's code.
Golang has a variety of libraries that provide GUID-generation:
- Use time to enable entropy and to achieve time-clustering
- Fill the rest of the ID with random data
- Encode GUID as a URL-safe string in a way that allows lexicographic sorting
A summary of the various options is shown in the following table:
Package |
Sample ID |
Note |
0pPKHjWprnVxGH7dEsAoXX2YQvU |
4 bytes of time (seconds) + 16 random bytes. |
|
b50vl5e54p1000fo3gh0 |
4 bytes of time (seconds) + 3 bytes of machine IDs + 2 bytes of process IDs + 3 random bytes. |
|
-Kmdih_fs4ZZccpx2Hl1 |
8 bytes of time (milliseconds) + 9 random bytes. |
|
20f8707d6000108 |
It's based on Twitter's design for generating IDs for tweets—simple but the least random. 6 bytes of time (10 ms) + 1 byte sequence + 2 bytes of machine ID. |