Snapshot của segmentio/ksuid: 5.3k★ · Go · Backend. K-Sortable Globally Unique IDs
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
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}
}
segmentio/ksuid thuộc nhóm Backend trên TopGit, cùng 6 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về segmentio/ksuid ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/segmentio/ksuid là nguồn chính thức.
segmentio/ksuid có phải mã nguồn mở không?
Có — segmentio/ksuid phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/segmentio/ksuid.
segmentio/ksuid có trang demo không?
Dự án có trang chủ ở https://segment.com/blog/a-brief-history-of-the-uuid/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
segmentio/ksuid dùng license gì?
segmentio/ksuid phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
segmentio/ksuid là gì?
segmentio/ksuid (segmentio/ksuid) là dự án Go trên GitHub. Theo mô tả gốc: K-Sortable Globally Unique IDs
Vì sao segmentio/ksuid được xếp vào nhóm Backend?
TopGit xếp segmentio/ksuid vào nhóm Backend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "coordination", "go", "golang"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc ksuid có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về ksuid.