Click on an ID to copy.
| Property | Value |
|---|---|
| Bit Length | 64 |
| Output Length | 18 chars |
| Encoding | decimal |
| Sortable | Yes |
| Timestamped | Yes |
| Monotonic | Yes |
| Crypto Random | No |
Snowflake ID is a 64-bit unique identifier format originally developed by Twitter (now X) in 2010 to solve the problem of generating unique identifiers across a massively distributed infrastructure. As Twitter scaled beyond a single database, the company needed IDs that could be generated independently by thousands of servers without central coordination, while still being compact enough to fit in a standard integer column and sortable enough to preserve chronological order. The engineer credited with the design, along with the broader infrastructure team at Twitter, released Snowflake as an open-source project, and the concept quickly became one of the most influential patterns in distributed systems engineering. Today, variations of the Snowflake format power identifier generation at Discord, Instagram, Sony, and countless other organizations that operate at internet scale.
The name "Snowflake" reflects the core promise of the format: like actual snowflakes, every generated ID is unique. But unlike randomly generated identifiers, Snowflake IDs carry meaningful structure. Each 64-bit integer encodes the moment of creation, the machine that created it, and a counter that prevents collisions when multiple IDs are generated within the same millisecond on the same machine. This combination of timestamp, provenance, and sequence makes Snowflake IDs extraordinarily practical for systems that need to ingest, sort, and query billions of records efficiently.
A Snowflake ID is a 64-bit signed integer whose bits are partitioned into four distinct fields. The most significant bit is unused and always set to zero, ensuring the value remains positive in languages and databases that treat 64-bit integers as signed. The remaining 63 bits are divided as follows:
The first field is a 41-bit timestamp that records the number of milliseconds elapsed since a custom epoch. Twitter's original epoch is 1288834974657 (approximately November 4, 2010), but each implementation is free to choose its own starting point. Forty-one bits of millisecond precision provide roughly 69.7 years of range before the timestamp space is exhausted, meaning an epoch set in 2010 will remain valid until approximately 2079.
The second field is a 10-bit worker or machine identifier. These 10 bits are sometimes split further into a 5-bit datacenter ID and a 5-bit machine ID, allowing up to 32 datacenters with 32 machines each, for a combined total of 1,024 unique generators. The exact subdivision varies by implementation; some systems use all 10 bits as a single worker ID assigned at startup.
The third field is a 12-bit sequence number that increments each time an ID is generated within the same millisecond on the same worker. Twelve bits allow up to 4,096 IDs per millisecond per worker. If the sequence overflows, the generator waits until the next millisecond tick before issuing new IDs. Across all 1,024 workers, this yields a theoretical maximum throughput of roughly 4.1 million IDs per millisecond, or over four billion IDs per second.
Because the timestamp occupies the most significant bits after the sign bit, Snowflake IDs are naturally time-sortable. Sorting a column of Snowflake IDs by their numeric value produces a chronological ordering that matches creation time to millisecond granularity. This property eliminates the need for a separate created-at column in many database schemas and enables efficient range queries using simple integer comparisons.
The generation algorithm is stateful but lightweight. Each worker maintains its last timestamp and current sequence counter in memory. When a new ID is requested, the generator reads the current time, compares it to the last timestamp, and either increments the sequence counter (if the timestamp matches) or resets it to zero (if time has advanced). It then assembles the 64-bit value by left-shifting and bitwise-ORing the three fields together. The entire operation completes in nanoseconds, with no disk I/O, no network call, and no locking beyond a simple mutex on the per-worker state.
Social media timelines and feeds. Snowflake IDs were born from Twitter's need to order tweets in a global timeline. Any platform that displays a chronological feed of user-generated content benefits from IDs that double as sort keys. Pagination becomes trivial: a client can request "all records with an ID greater than X" to fetch the next page, and the database can satisfy that query using a simple index range scan.
Real-time messaging and event streaming. Discord adopted a Snowflake variant for message IDs across its millions of channels. In messaging systems, the ability to extract a creation timestamp directly from the ID simplifies deduplication, ordering, and retention policies. Event streaming platforms like Apache Kafka can also leverage Snowflake-style IDs as message keys to maintain per-partition ordering without external timestamp columns.
Distributed database sharding. Because the worker bits encode the origin of each ID, Snowflake IDs carry routing information. A shard-aware application can inspect the worker or datacenter bits to determine which shard owns a given record, enabling efficient read routing without a lookup table. Instagram's variation, for instance, embeds a logical shard ID directly in the identifier.
High-throughput telemetry and logging. Observability platforms that ingest millions of log entries or metrics per second need identifiers that are fast to generate, small to store, and sortable for time-range queries. A 64-bit Snowflake ID is half the size of a 128-bit UUID, which translates into meaningful storage and memory savings at petabyte scale.
Snowflake ID and TSID share the same fundamental concept: pack a timestamp and uniqueness bits into a 64-bit integer. TSID modernizes the idea by encoding the result as a 13-character Crockford base32 string, which is more human-readable and URL-safe than a raw numeric Snowflake. TSID also replaces the rigid worker-ID allocation with a combination of randomness and optional node bits, reducing operational overhead. However, Snowflake's explicit worker and datacenter fields give operators deterministic control over ID provenance, which some large-scale deployments prefer.
Compared to UUID v7, Snowflake IDs are half the size at 64 bits versus 128 bits. UUID v7 stores a Unix timestamp in its most significant bits and fills the remainder with random data, achieving time-sortability much like Snowflake. The key trade-off is reach versus compactness: UUID v7 provides vastly more randomness (approximately 62 random bits after version and variant markers) and needs no worker-ID coordination, but it requires a 128-bit column and a 36-character string representation. Snowflake fits in a native bigint column, making it more efficient for joins, indexes, and network transfer in systems where every byte counts.
When compared to 128-bit formats like ULID or KSUID, Snowflake IDs trade collision resistance and randomness for compactness and raw speed. ULID and KSUID are better choices when coordination-free generation across anonymous nodes is a priority, because their large random components virtually eliminate collision risk without requiring worker-ID assignment. Snowflake is the stronger choice when you need the smallest possible identifier, the highest possible throughput on a known set of machines, and the ability to extract both timestamp and origin from the ID itself.
import { Snowflake } from '@sapphire/snowflake';
const epoch = new Date('2020-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch);
const id = snowflake.generate();
console.log(id.toString());A Snowflake ID is a 64-bit unique identifier originally developed by Twitter (now X) for distributed systems. It encodes a timestamp, a machine/datacenter ID, and a sequence number into a single integer, enabling high-throughput ID generation without coordination between servers.
Snowflake guarantees uniqueness by combining three components: a millisecond-precision timestamp, a worker/datacenter identifier, and an auto-incrementing sequence number. Because each worker has a unique ID, multiple machines can generate IDs simultaneously without risk of collision or need for a centralized coordinator.
Yes, Snowflake IDs are roughly sortable by creation time. The most significant bits store a millisecond timestamp, so IDs generated later will have higher numeric values. This makes them excellent for use as database primary keys where chronological ordering is beneficial.
A single Snowflake worker can generate up to 4,096 unique IDs per millisecond, which translates to over 4 million IDs per second per node. In a distributed deployment with many workers, the total system throughput scales linearly with the number of nodes.
Snowflake IDs were pioneered by Twitter and are now widely adopted across the tech industry. Discord, Instagram, and many other large-scale platforms use Snowflake-inspired ID generation schemes to handle billions of unique identifiers across globally distributed infrastructure.
ObjectId is MongoDB's 12-byte unique identifier with an embedded timestamp, represented as a 24-character hexadecimal string.
TSID (Time-Sorted ID) is a compact, time-sortable identifier inspired by Snowflake and ULID, producing 13-character Crockford base32 strings.
ULID (Universally Unique Lexicographically Sortable Identifier) is a type of identifier designed to be both globally unique and sortable in a lexicographically manner.
UUID v7 is a time-sortable UUID defined by RFC 9562, embedding a millisecond Unix timestamp with cryptographic randomness for global uniqueness.
© 2024 Carova Labs. All rights reserved