UUID as Primary Key — Best Practices
Quick Answer: UUID primary keys provide globally unique IDs ideal for distributed systems and API-exposed resources. Use UUID v7 for time-sorted ordering (better index performance), store as BINARY(16) in MySQL or native UUID type in PostgreSQL, and never use CHAR(36).
FAQ
Do UUID primary keys hurt performance?
Random UUID v4 can cause index fragmentation. UUID v7 solves this with time-ordering. In PostgreSQL, native UUID type is 16 bytes vs 36 bytes for text storage.
Should I expose UUID primary keys in my API?
Yes. UUIDs are unguessable (unlike sequential IDs) and safe to expose publicly. They do not reveal record counts or creation order.