#17389·vitess

Feature Request: Snowflake ID generation

Author: DeathBornCreated Dec 13, 2024Updated Sep 30, 2026
LabelsType: Feature

Feature Description

Request

We would like to add Snowflake ID generation feature to Vitess. Here is a working example on v11 code base. It can really integrate nicely just like Vitess Sequences

Snowflake ID

  • 64 bits BIGINT constructed
    • Sign 1 bit - always 0
    • Timestamp 42 bit - in milliseconds since chosen epoch
    • Machine ID 10 bit - 1024 machines possible
    • Sequence 12-bit - 4096 max value; local counter per each machine;
  • Requires Snowflake generator servers and Zookeeper for Machine ID coordination - extra parts
  • Good enough for ~69 years - depends on chosen epoch

How to implement

  • Use Sequences code path and the same primitive
  • Store Snowflake configuration (machine_id + chosen epoch) in a table just like Sequences do.
    • It can be sharded up to 1024 shards
    • It can reload its configuration using configured interval
    • During first load, it will auto-initialise its machine_id using cross-shard TX, which is ok, because table is practically never written due to the fact that we don't need to store last generated ID - benefit of Snowflake algorithm. This will cover Reshard operation too.
    • VTablets will be responsible for advancing sequence and timestamp, and returning Snowflake ID
    • VTGates
      • random shard will receive the query select next N values from snow_table
      • will receive timestamp + sequence + chosen epoch
      • generate N required ids

I would like some comments on this before working on contribution. Can I use the same Sequence primitive but return 3 values instead of one - timestamp + sequence + chosen epoch ? This would make chosen epoch configurable.

Use Case(s)

Benefits:

  • Generated values are Time-Sortable - good for indexes
  • Secure - very hard to guess next value, so good for business
  • Nice feature - contains created_at timestamp in value!
  • Using public_id with UUID/NanoID/ULID requires complex app code changes, complex communication between service, take at least 2x more space - BIGINT is better here