Choose a CosId Strategy
Recommend one strategy from explicit requirements. Do not default to the largest benchmark number.
Workflow
- Confirm the target CosId major version. In a CosId checkout, read
gradle.properties; do not assume the current branch matches the user's application. - Collect the required ID type, ordering scope, peak and burst rate, instance count, JavaScript exposure, acceptable gaps, available infrastructure, and outage behavior.
- Recommend one primary strategy. Add a fallback only when a real constraint creates a close tradeoff.
- State the operational cost and the exact ordering guarantee.
- Hand implementation to
$cosid-spring-bootor$cosid-manual-integration.
Decision Table
| Strategy | Output | Ordering | Coordination | Main constraint |
|---|---|---|---|---|
CosIdGenerator |
String only |
Locally time-ordered; globally affected by clocks | Unique machine ID | Clock-sensitive; generate() is unsupported |
SnowflakeId |
long and converted String |
Locally monotonic; globally trend-ordered | Unique machine ID | Clock-sensitive and bounded by bit allocation |
DefaultSegmentId |
long and converted String |
Locally monotonic; globally trend-ordered by allocated ranges | IdSegmentDistributor at segment rollover |
Allocation can stall or fail when the current segment is exhausted |
SegmentChainId |
long and converted String |
Locally monotonic; globally trend-ordered by allocated ranges | IdSegmentDistributor plus background prefetch |
More lifecycle work; gaps can grow with prefetched ranges |
Benchmark results are hardware- and workload-specific. Use them only as evidence that a strategy meets a class of demand, then benchmark the selected strategy with realistic concurrency and backend latency.
Recommendation Rules
- Choose
SnowflakeIdfor a numeric primary key that should roughly encode creation time and continue generating locally after machine-ID allocation. - Choose
CosIdGeneratorwhen callers require compact structured strings and do not need along. Its default 20 machine bits provide a larger instance space than default Snowflake. - Choose
DefaultSegmentIdwhen local monotonicity matters, gaps are acceptable, and synchronous range allocation is operationally simple enough. - Choose
SegmentChainIdwhen distributor latency at rollover matters enough to justify prefetching. Prefetch reduces stalls; it does not make the distributor irrelevant after reserved ranges run out. - Return IDs as strings to JavaScript by default. Use
SafeJavaScriptSnowflakeIdonly when a numeric JavaScript-safe value is an explicit requirement. - Reuse Redis, JDBC, MongoDB, ZooKeeper, or proxy infrastructure already operated by the system. Do not add a backend solely because one benchmark is faster.
- Use manual or StatefulSet machine IDs only when instance identities and uniqueness are operationally guaranteed.
Ordering and Availability
- No listed strategy provides strict global generation-time order across concurrent instances.
- Snowflake and CosIdGenerator depend on wall clocks; clock-sync wrappers wait for bounded rollback and fail when the configured broken threshold is exceeded.
- Segment generators allocate disjoint ranges. One instance can emit a higher range while another still emits a lower range.
- Segment restart or prefetch can leave unused IDs. Increase
stepor prefetch distance only after accepting that gap tradeoff. - Manual duplicate machine IDs can produce duplicate Snowflake or CosIdGenerator IDs.
If the requirement is strict global sequencing by request time, say that CosId's distributed generators do not provide it and recommend a serialized authority such as a database sequence or dedicated sequencer.
Stable Facts to Use Carefully
- Default millisecond Snowflake layout: 41 timestamp bits, 10 machine bits, 12 sequence bits; the sign bit is reserved.
- Default second Snowflake layout: 31 timestamp bits, 10 machine bits, 22 sequence bits.
- Snowflake bit allocation must total 63.
- Default epoch:
2019-12-24T16:00:00UTC (1577203200000ms or1577203200s). - Default CosIdGenerator layout is independent of Snowflake: 44 timestamp bits, 20 machine bits, and 16 sequence bits.
Verify these values against the target version before emitting custom configuration.
Response Contract
Include:
- one recommendation and the requirements it satisfies;
- the precise ordering, gap, and failure semantics;
- the machine-ID or segment-distributor choice;
- one focused validation or benchmark;
- the correct implementation skill for the next step.