posts-go/posts/2022-07-12-uuid-or-else.md

59 lines
2.4 KiB
Markdown
Raw Normal View History

2022-07-12 08:33:38 +00:00
# UUID or else
2023-08-05 18:56:25 +00:00
There are many use cases where we need to use a unique ID. In my experience, I
2024-07-22 11:32:39 +00:00
only encounter 2 cases:
2022-07-12 08:33:38 +00:00
2023-08-05 18:56:25 +00:00
- ID to trace request from client to server, from service to service
(microservice architecture or nanoservice I don't know).
2022-07-12 08:33:38 +00:00
- Primary key for database.
In my Go universe, there are some libs to help us with this:
- [google/uuid](https://github.com/google/uuid)
- [rs/xid](https://github.com/rs/xid)
- [segmentio/ksuid](https://github.com/segmentio/ksuid)
- [oklog/ulid](https://github.com/oklog/ulid)
## First use case is trace ID, or context aware ID
2023-08-05 18:56:25 +00:00
The ID is used only for trace and log. If same ID is generated twice (because
2024-07-22 11:32:39 +00:00
maybe the possibility is too small but not 0), honestly I don't care. When I use
2023-08-05 18:56:25 +00:00
that ID to search log , if it pops more than things I care for, it is still no
harm to me.
2022-07-12 08:33:38 +00:00
2023-08-05 18:56:25 +00:00
My choice for this use case is **rs/xid**. Because it is small (not span too
much on log line) and copy friendly.
2022-07-12 08:33:38 +00:00
## Second use case is primary key, also hard choice
2023-08-05 18:56:25 +00:00
Why I don't use auto increment key for primary key? The answer is simple, I
don't want to write database specific SQL. SQLite has some different syntax from
MySQL, and PostgreSQL and so on. Every logic I can move to application layer
from database layer, I will.
2022-07-12 08:33:38 +00:00
2024-06-25 10:33:22 +00:00
The ID must be sortable (because index) and unique.
I have 3 choices:
- **google/uuid**: use UUID v7 not v4.
- **segmentio/ksuid**: handle error if you don't want panic.
- **oklog/ulid**: don't use yet.
2022-07-12 09:33:45 +00:00
## What else?
I think about adding prefix to ID to identify which resource that ID represents.
2024-06-25 10:33:22 +00:00
For example: `user:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX`.
2022-07-12 09:33:45 +00:00
## Thanks
2024-06-25 10:33:22 +00:00
- [Universally Unique IDentifiers (UUIDs)](https://datatracker.ietf.org/doc/html/rfc9562)
- [On IDs](https://0pointer.net/blog/projects/ids.html)
2022-07-12 09:33:45 +00:00
- [UUID, SERIAL OR IDENTITY COLUMNS FOR POSTGRESQL AUTO-GENERATED PRIMARY KEYS?](https://www.cybertec-postgresql.com/en/uuid-serial-or-identity-columns-for-postgresql-auto-generated-primary-keys/)
- [Identity Crisis: Sequence v. UUID as Primary Key](https://brandur.org/nanoglyphs/026-ids)
- [Generating good unique ids in Go](https://blog.kowalczyk.info/article/JyRZ/generating-good-unique-ids-in-go.html)
- [How we used Go 1.18 when designing our Identifiers](https://encore.dev/blog/go-1.18-generic-identifiers)
- [ULIDs and Primary Keys](https://blog.daveallie.com/ulid-primary-keys)
2024-06-25 10:33:22 +00:00
- [UUIDv7 in 32 languages](https://antonz.org/uuidv7/)