The correlation identifier pattern

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


github.com/segmentio/ksuid

0pPKHjWprnVxGH7dEsAoXX2YQvU

4 bytes of time (seconds) + 16 random bytes.


github.com/rs/xid

b50vl5e54p1000fo3gh0

4 bytes of time (seconds) + 3 bytes of machine IDs + 2 bytes of process IDs + 3 random bytes.


github.com/kjk/betterguid

-Kmdih_fs4ZZccpx2Hl1

8 bytes of time (milliseconds) + 9 random bytes.


github.com/sony/sonyflake

20f8707d6000108

It's based on Twitter's design for generating IDs for tweetssimple but the least random.

6 bytes of time (10 ms) + 1 byte sequence + 2 bytes of machine ID.

To enable debugging, it is imperative to log these correlation IDs along with whatever the log needs to convey.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset