Loading...
Loading...
Compare original and translation side by side
back-of-the-envelopeBIGINT AUTO_INCREMENTSERIALback-of-the-envelopeBIGINT AUTO_INCREMENTSERIALback-of-the-envelopeback-of-the-envelope| Option | What it solves | What it worsens | Change it when |
|---|---|---|---|
| Auto-increment / sequence | Trivial, monotonic, compact int | Serializes writes; single node caps throughput; leaks count | Writes outgrow one node, or you need client-side IDs → ticket/Snowflake |
| UUIDv4 (random) | Generate anywhere, no coordination, no leakage | 128-bit; random order kills index locality (page splits); not sortable | You need time-ordering → ULID/UUIDv7 |
| ULID / UUIDv7 | Zero coordination + time-sortable + index-friendly | Still 128-bit; only ms-sortable (not strict); leaks creation time | You need a 64-bit key or strict order → Snowflake / sequence |
| Snowflake-style (64-bit) | Compact, k-sorted, ~4M IDs/node/sec | Needs node-ID assignment + clock-skew handling; epoch/bit budget caps lifespan | Clock sync is unreliable, or you can't assign node IDs → ULID |
| DB ticket / range | Monotonic-ish ints, low coordination, simple | Allocator table is a SPOF; gaps on restart; only loosely ordered across nodes | Allocator becomes a bottleneck or SPOF → Snowflake/ULID |
| 方案 | 解决的问题 | 带来的问题 | 何时更换 |
|---|---|---|---|
| 自增/序列 | 实现简单、单调、紧凑整数 | 序列化写入操作;单一节点限制吞吐量;泄露计数 | 写入量超出单一节点处理能力,或需要客户端ID → 票据/Snowflake方案 |
| UUIDv4(随机) | 可在任意位置生成、无需协调、无泄露 | 128位长度;随机顺序破坏索引局部性(页分裂);不可排序 | 需要时间排序 → ULID/UUIDv7 |
| ULID / UUIDv7 | 无协调+可按时间排序+索引友好 | 仍为128位;仅支持毫秒级排序(非严格);泄露创建时间 | 需要64位键或严格排序 → Snowflake/序列 |
| Snowflake风格(64位) | 紧凑、k排序、单节点每秒约400万ID | 需要分配节点ID+处理时钟偏移;纪元/位预算限制生命周期 | 时钟同步不可靠,或无法分配节点ID → ULID |
| 数据库票据/范围 | 近似单调整数、低协调、实现简单 | 分配器表是单点故障;重启时会产生ID间隙;跨节点仅松散排序 | 分配器成为瓶颈或单点故障 → Snowflake/ULID |
now < last_timestamp2^seq_bitsconsistency-coordinationdata-storagenow < last_timestamp2^seq_bitsconsistency-coordinationdata-storageback-of-the-envelopeback-of-the-envelopereferences/providers/<provider>.mddata-storagedata-storageback-of-the-envelopeback-of-the-envelope[timestamp:41 | node:10 | seq:12]next(entity) -> {id, issued_at}data-storage[timestamp:41 | node:10 | seq:12]next(entity) -> {id, issued_at}data-storagereferences/providers/<provider>.mdreferences/providers/<provider>.mdarchitecture-diagram[ts | node | seq]architecture-diagram[ts | node | seq]data-storageconsistency-coordinationmessaging-streamingapi-designsystem-designdata-storageconsistency-coordinationmessaging-streamingapi-designsystem-designreferences/deep-dive.mdreferences/providers/{generic,aws,gcp}.mdreferences/deep-dive.mdreferences/providers/{generic,aws,gcp}.md