Auto-incrementing integer IDs in Rails expose business metrics and break down in distributed systems, while UUIDs are slow to index and ugly in URLs. Snowflake IDs — 64-bit integers encoding a timestamp, worker ID, and sequence number — offer a middle ground: globally unique, time-ordered, and as fast as a bigint in PostgreSQL. The post walks through implementing Snowflake IDs in Rails 8 using the `snowflake_id` gem, a reusable `HasSnowflakeId` concern with a `before_create` callback, and a migration that disables auto-increment in favor of a `bigint` primary key. Key caveats include returning large Snowflake IDs as strings in JSON APIs to avoid JavaScript precision loss.

4m read timeFrom norvilis.com
Post cover image
Table of contents
What is a Snowflake ID?STEP 1: The Ruby ImplementationSTEP 2: Making it the Primary Key in RailsSTEP 3: The MigrationWhy this is a Win for Solo DevsSummary

Sort: