Skip to content
/ ksid Public

k-sortable unique IDs better than UUIDs

License

Notifications You must be signed in to change notification settings

maruel/ksid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ksid: fast 63 bits unique IDs in Go

Go Reference codecov

Properties

  • 63 bits IDs that are guaranteed to be unique.
  • Native base32 encoding, at most 13 characters.
  • Both numerical and string forms are k-sortable.
  • Shardable across up to 32768 servers.
  • Valid until 2115, or 2204 using the 64th bit.

Performance

  • ksid.ID is 50% smaller (8 bytes) than UUIDs (16 bytes) in binary form.
  • ksid.ID string is at most 13 characters, 33% the size of UUIDs (36 characters).
  • ksid.ID is faster to generate, encode and decode than UUIDs.
  • Maximium generation throughput: 32768 ID/10µs and 3.27B ID/s.

Comparison with google/uuid:

  • Generation: ksid (~33 ns) is at least ~7.5x faster than both uuid v4 (~250 ns) and uuid v7 (~290 ns).
  • Encoding: ksid (~24 ns) is ~2.7x faster than uuid (~65 ns).
  • Decoding: ksid (~8 ns) is ~1.8x faster than uuid (~15 ns).
$ go test -bench=.
goos: linux
goarch: amd64
pkg: github.com/maruel/ksid
cpu: 13th Gen Intel(R) Core(TM) i7-13700
BenchmarkID/ksid-gen-24      36123418   33.15 ns/op   0 B/op  0 allocs/op
BenchmarkID/uuidv4-gen-24     4740570  248.8  ns/op  16 B/op  1 allocs/op
BenchmarkID/uuidv7-gen-24     4138654  288.5  ns/op  16 B/op  1 allocs/op
BenchmarkID/ksid-encode-24   95906893   24.37 ns/op  16 B/op  1 allocs/op
BenchmarkID/uuid-encode-24   23047898   64.62 ns/op  48 B/op  1 allocs/op
BenchmarkID/ksid-decode-24  141703341    8.42 ns/op   0 B/op  0 allocs/op
BenchmarkID/uuid-decode-24   78804513   14.89 ns/op   0 B/op  0 allocs/op

When not to use

  • Necessitate higher throughput than 1000 IDs per microsecond, or more than ten million IDs per second.
  • More than 100 shards or an unbounded number of shards.

Algorithm

I (Marc-Antoine Ruel) invented this algorithm in 2014 while working on the Chromium infrastructure. I designed the algorithm so I could generate unique IDs that would fit 63 bits integers to use as database keys.

Structure

  • Bit 63: sign (always 0, keeps int64 positive)
  • Bits 62-15: 10µs intervals since epoch 2026-01-01 (48 bits)
  • Bits 14-0: slice (15 bits = 32768 values per 10µs)

The library has 100% unit testing code coverage.

About

k-sortable unique IDs better than UUIDs

Resources

License

Stars

Watchers

Forks

Languages