segmentio/ksuid is a server-side project on GitHub with 5.3k stars, written primarily in Go. K-Sortable Globally Unique IDs
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
ksuid is an efficient, comprehensive, battle-tested Go library for
generating and parsing a specific kind of globally unique identifier
called a KSUID. This library serves as its reference implementation.
Install
go get -u github.com/segmentio/ksuid
What is a KSUID?
KSUID is for K-Sortable Unique IDentifier. It is a kind of globally
unique identifier similar to a RFC 4122 UUID, built from the ground-up to be "naturally"
sorted by generation timestamp without any special type-aware logic.
In short, running a set of KSUIDs through the UNIX sort command will result
in a list ordered by generation time.
Why use KSUIDs?
There are numerous methods for generating unique identifiers, so why KSUID?
Even if only one of these properties are important to you, KSUID is a great
choice! :) Many projects chose to use KSUIDs just because the text
representation is copy-and-paste friendly.
For a follow up read on the topic: A brief history of UUID
1. Naturally Ordered By Generation Time
Unlike the more ubiquitous UUIDv4, a KSUID contains a timestamp component
that allows them to be loosely sorted by generation time. This is not a strong
guarantee (an invariant) as it depends on wall clocks, but is still incredibly
useful in practice. Both the binary and text representations will sort by
creation time without any special sorting logic.
While RFC 4122 UUIDv1s do include a time component, there aren't enough
bytes of randomness to provide strong protection against collisions
(duplicates). With such a low amount of entropy, it is feasible for a
malicious party to guess generated IDs, creating a problem for systems whose
security is, implicitly or explicitly, sensitive to an adversary guessing
identifiers.
To fit into a 64-bit number space, Snowflake IDs
and its derivatives require coordination to avoid collisions, which
significantly increases the deployment complexity and operational burden.
A KSUID includes 128 bits of pseudorandom data ("entropy"). This number space
is 64 times larger than the 122 bits used by the well-accepted RFC 4122 UUIDv4
standard. The additional timestamp component can be considered "bonus entropy"
which further decreases the probability of collisions, to the point of physical
infeasibility in any practical implementation.
3. Highly Portable Representations
The text and binary representations are lexicographically sortable, which
allows them to be dropped into systems which do not natively support KSUIDs
and retain their time-ordered property.
The text representation is an alphanumeric base62 encoding, so it "fits"
anywhere alphanumeric strings are accepted. No delimiters are used, so
stringified KSUIDs won't be inadvertently truncated or tokenized when
interpreted by software that is designed for human-readable text, a common
problem for the text representation of RFC 4122 UUIDs.
How do KSUIDs work?
Binary KSUIDs are 20-bytes: a 32-bit unsigned integer UTC timestamp and
a 128-bit randomly generated payload. The timestamp uses big-endian
encoding, to support lexicographic sorting. The timestamp epoch is adjusted
to May 13th, 2014, providing over 100 years of life. The payload is
generated by a cryptographically-strong pseudorandom number generator.
The text representation is always 27 characters, encoded in alphanumeric
base62 that will lexicographically sort by timestamp.
High Performance
This library is designed to be used in code paths that are performance
critical. Its code has been tuned to eliminate all non-essential
overhead. The KSUID type is derived from a fixed-size array, which
eliminates the additional reference chasing and allocation involved in
a variable-width type.
The API provides an interface for use in code paths which are sensitive
to allocation. For example, the Append method can be used to parse the
text representation and replace the contents of a KSUID value
without additional heap allocation.
All public package level "pure" functions are concurrency-safe, protected
by a global mutex. For hot loops that generate a large amount of KSUIDs
from a single Goroutine, the Sequence type is provided to elide the
potential contention.
By default, out of an abundance of caution, the cryptographically-secure
PRNG is used to generate the random bits of a KSUID. This can be relaxed
in extremely performance-critical code using the included FastRander
type. FastRander uses the standard PRNG with a seed generated by the
cryptographically-secure PRNG.
NOTE: While there is no evidence that FastRander will increase the
probability of a collision, it shouldn't be used in scenarios where
uniqueness is important to security, as there is an increased chance
the generated IDs can be predicted by an adversary.
Battle Tested
This code has been used in production at Segment for several years,
across a diverse array of projects. Trillions upon trillions of
KSUIDs have been generated in some of Segment's most
performance-critical, large-scale distributed systems.
Plays Well With Others
Designed to be integrated with other libraries, the KSUID type
implements many standard library interfaces, including:
Stringer
database/sql.Scanner and database/sql/driver.Valuer
encoding.BinaryMarshal and encoding.BinaryUnmarshal
encoding.TextMarshal and encoding.TextUnmarshal
(encoding/json friendly!)
Command Line Tool
This package comes with a command-line tool ksuid, useful for
generating KSUIDs as well as inspecting the internal components of
existing KSUIDs. Machine-friendly output is provided for scripting
use cases.
Given a Go build environment, it can be installed with the command:
There are times when you are sure your ksuid is correct. But you need to get it from bytes or string and pass it
it's to the structure. For this, there are OrNil functions that return ksuid.Nil on error and can be called
directly in the structure.
Functions:
ParseOrNil()
FromPartsOrNil()
FromBytesOrNil()
An example of using the function without OrNil:
func getPosts(before, after []byte) {
b, err := ksuid.FromBytes(before)
if err != nil {
// handle error
}
a, err := ksuid.FromBytes(after)
if err != nil {
// handle error
}
sortOptions := SortOptions{Before: b, After: a}
}
Yes — segmentio/ksuid ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/segmentio/ksuid.
What else is in the Backend space?
segmentio/ksuid is tracked by TopGit under the Backend category, alongside 6 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What is segmentio/ksuid?
segmentio/ksuid (segmentio/ksuid) is a Go project on GitHub. From the project's own README: K-Sortable Globally Unique IDs
What license does segmentio/ksuid use?
segmentio/ksuid is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where can I see segmentio/ksuid in action?
The project maintains a homepage at https://segment.com/blog/a-brief-history-of-the-uuid/. The README tab on this page also usually contains screenshots and a quickstart.
Where do I read more about segmentio/ksuid?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/segmentio/ksuid is the definitive source.
Why is segmentio/ksuid categorized under Backend?
TopGit places segmentio/ksuid in the Backend category based on its GitHub topics and description (tagged: "coordination", "go", "golang"). Categories are assigned from real repository metadata, not editorial guesswork.
Read full README in the tab above.
Still deciding about ksuid?
One click hands the question to an AI along with this page — see what it says about ksuid.