<!doctype html><metacharset=utf-8><metaname=viewportcontent="width=device-width,initial-scale=1"><linkrel=preconnecthref=https://fonts.googleapis.com><linkrel=preconnecthref=https://fonts.gstatic.comcrossorigin><linkhref="https://fonts.googleapis.com/css2?family=Recursive:wght,CASL,MONO@300..800,0..1,0..1&display=swap"rel=stylesheet><linkrel=stylesheethref=styles.css><ahref=index>Index</a><h1>UUID or else</h1><p>There are many use cases where we need to use a unique ID.<br>In my experience, I only encouter 2 cases:<ul><li>ID to trace request from client to server, from service to service (microservice architecture or nanoservice I don't know).<li>Primary key for database.</ul><p>In my Go universe, there are some libs to help us with this:<ul><li><ahref=https://github.com/google/uuid>google/uuid</a><li><ahref=https://github.com/rs/xid>rs/xid</a><li><ahref=https://github.com/segmentio/ksuid>segmentio/ksuid</a><li><ahref=https://github.com/oklog/ulid>oklog/ulid</a></ul><h2>First use case is trace ID, or context aware ID</h2><p>The ID is used only for trace and log.<br>If same ID is generated twice (because maybe the possibilty is too small but not 0), honestly I don't care.<br>When I use that ID to search log , if it pops more than things I care for, it is still no harm to me.<p>My choice for this use case is <strong>rs/xid</strong>.<br>Because it is small (not span too much on log line) and copy friendly.<h2>Second use case is primary key, also hard choice</h2><p>Why I don't use auto increment key for primary key?<br>The answer is simple, I don't want to write database specific SQL.<br>SQLite has some different syntax from MySQL, and PostgreSQL and so on.<br>Every logic I can move to application layer from database layer, I will.<p>In the past and present, I use <strong>google/uuid</strong>, specificially I use UUID v4.<br>In the future I will look to use <strong>segmentio/ksuid</strong> and <strong>oklog/ulid</strong> (trial and error of course).<br>Both are sortable, but <strong>google/uuid</strong> is not.<br>The reason I'm afraid because the database is sensitive subject, and I need more testing and battle test proof to trust those libs.<h2>What else?</h2><p>I think about adding prefix to ID to identify which resource that ID represents.<h2>Thanks</h2><ul><li><ahref=https://www.cybertec-postgresql.com/en/uuid-serial-or-identity-columns-for-postgresql-auto-generated-primary-keys/>UUID, SERIAL OR IDENTITY COLUMNS FOR POSTGRESQL AUTO-GENERATED PRIMARY KEYS?</a><li><ahref=https://brandur.org/nanoglyphs/026-ids>Identity Crisis: Sequence v. UUID as Primary Key</a><li><ahref=https://blog.kowalczyk.info/article/JyRZ/generating-good-unique-ids-in-go.html>Generating good unique ids in Go</a><li><ahref=https://encore.dev/blog/go-1.18-generic-identifiers>How we used Go 1.18 when designing our Identifiers</a><li><ahref=https://blog.daveallie.com/ulid-primary-keys>ULIDs and Primary Keys</a><li><ahref=https://0pointer.net/blog/projects/ids.html>On IDs</a></ul><ahref=mailto:hauvipapro+posts@gmail.com>Feel free to ask me via email</a>