Unique ID Generator 00:00:01 An order number made from three fields A shop needs identifiers it can generate locally. In this illustrative Snowflake-style layout, a timestamp, a worker identity and a sequence occupy disjoint bit ranges. Trace the second order generated in one millisecond. A new order needs an ID. Nothing is filled in yet. This server is in datacenter 1. Five bits allow 0 to 31. Its machine number is 7, leased when it started. It is the second ID in this millisecond, so the counter is 1. Twelve bits allow 0 to 4,095. Each part is shifted to its place: time by 22, datacenter by 17, machine by 12. The result fits in a signed 64-bit integer and can be taken apart again. Keep the epoch, field widths and worker allocation policy explicit. A time-based field supports approximate ordering across workers, while disjoint worker and sequence fields protect uniqueness under their stated assumptions. 00:01:11 A worker uses every sequence value Twelve bits hold four thousand ninety-six distinct sequence values, starting at zero. Generate the last available value before the physical millisecond advances. What must the next caller wait for? The clock says 500. The counter starts at 0. The clock still says 500, so only the counter grows. After 4,096 IDs the counter stands at 4,095. The next would be 0 again. The generator waits until the clock says 501. Wrapping within the same timestamp and worker identity would repeat an identifier. This generator waits for the next millisecond, so clock resolution and burst size bound its local throughput. 00:02:01 The clock reads an earlier instant A generator last used timestamp eight hundred when its clock jumps backward by two ticks. Decide whether restarting its sequence from zero at a reused timestamp would be safe. The last used time is 800. The clock is corrected to 798. The generator waits until 800. At 800 it continues with counter 1. Nothing is repeated. The clock jumps a minute back. Waiting makes no sense: refuse, and raise an alert. Persist or otherwise protect the generator's prior state across restarts, and guarantee exclusive worker allocation. A rollback policy must preserve uniqueness even after a process pause or clock correction. 00:02:49 Lease a block of a thousand numbers Two workers acquire disjoint numbered ranges from a coordinated allocator. After that, neither needs a remote request per order. Predict what happens to unused numbers when a worker restarts. Nobody has a block. The next free number is 1. A takes 1 to 1,000 in one atomic step. B takes 1,001 to 2,000. They can never overlap. Both hand out numbers without a call. ZooKeeper is down for a while. Both continue with what they have. A restarts and takes a new block. 701 to 1,000 stay unused. Gaps can remain without causing collisions. A unique identifier service does not automatically provide gap-free invoice numbering or globally ordered commit times. 00:03:44 One insertion pattern helps a page and heats a shard Compare random and time-ordered identifiers in an index. Then reinterpret the same ordered ranges as physical shard ownership. Which advantage turns into concentrated write traffic? An index with four pages of 100 rows. Eight new UUIDv4 rows land on all four pages. Every page must be read and written. Eight new UUIDv7 rows all land on the last page. When each column is a partition on its own machine, the same thing is a hot spot. Time locality can reduce scattered page writes in a particular index. A monotonically active range can also overload its owner, so test storage layout and partitioning together.