Skip to content

Switch UUID generation from v4 to v7#18

Merged
HamptonMakes merged 1 commit into
mainfrom
switch-to-uuid-v7
Feb 24, 2026
Merged

Switch UUID generation from v4 to v7#18
HamptonMakes merged 1 commit into
mainfrom
switch-to-uuid-v7

Conversation

@HamptonMakes

@HamptonMakes HamptonMakes commented Feb 24, 2026

Copy link
Copy Markdown
Collaborator

What

Switches ApplicationRecord#assign_uuid from SecureRandom.uuid (v4, random) to SecureRandom.uuid_v7 (Ruby 3.4+, time-ordered).

Why

UUID v4 primary keys sort in random order, which caused comment replies to appear out of order. UUIDv7 embeds a 48-bit millisecond timestamp in the high bits, so PKs naturally sort chronologically.

Bonus: time-ordered UUIDs improve B-tree index locality, reducing page splits and improving query performance.

Notes

  • Existing v4 UUIDs remain valid — they'll just sort before new v7 ones.
  • One-line change thanks to Ruby 3.4's built-in SecureRandom.uuid_v7.

Use SecureRandom.uuid_v7 (Ruby 3.4+) instead of SecureRandom.uuid (v4).
UUIDv7 (RFC 9562) embeds a 48-bit unix timestamp, so primary keys
naturally sort chronologically. This fixes comment reply ordering and
improves B-tree index locality.

Amp-Thread-ID: https://ampcode.com/threads/T-019c9157-b075-7443-9d10-f4aa772fec8f
Co-authored-by: Amp <amp@ampcode.com>
@HamptonMakes
HamptonMakes merged commit f8aefdd into main Feb 24, 2026
4 checks passed
@HamptonMakes
HamptonMakes deleted the switch-to-uuid-v7 branch February 24, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant