KSUIDs: my new favorite way to do IDs

I’ve recently switched a bunch of my projects over to KSUIDs (K-Sortable Unique Identifiers), and it’s been one of those small changes that quietly improves everything. Check out the original Go library from Segment here: https://github.com/segmentio/ksuid.

A KSUID is a 27-character, base62-encoded string that combines a 32-bit timestamp (with a 2014 epoch) and 128 bits of strong randomness. The killer feature is that they’re naturally sortable by creation time. No extra created_at column or other secondary sorting–just drop them into a list, database index, or log line and they order chronologically for free.

Here are a few places I’ve been using them lately:

  • Event IDs in a real-time dashboard stream → sorting by ID = sorting by time
  • Primary keys in Postgres tables for audit logs and user actions
  • Trace/span IDs in structured logging middleware
  • Record identifiers in a couple of event-sourced side projects

One of my favorite use cases is client updates. If I have a table of data that is refreshing on a client occasionally, I can just add a &after=3ATIlO0OkwuMwLR5WEGoODyPnCn and filter on an already-indexed value for updated rows. It’s like magic!

They’re collision-resistant, URL- and terminal-friendly (no hyphens), and play nicely with JSON/Go structs. If you’re done with opaque UUIDs or the coordination hassle of Snowflake-style IDs, KSUIDs feel like the sweet spot.