Recently with expected high scale projects, I started it by use UUID as the primary and foreign key of the tables. The result comes with a perfect high scale result. This methodology comes with bad and good effect. Even so, I recommend initing project with something like this if want to have easy and flexible scaling to your app in the future. Again, I really recommend this if you think of ↗️↗️↗️
Note: Tutorial below use specific between Ruby On Rails and Postgresql
Why should we use UUID?
- Moving Data: UUID mean in this world your id is unique. That means it is easier to merge, split, or move data across table or event database
- Multi location: When you million of transaction that you can not handle in one single database. So you need your database to have a master and the child nodes or place in multi-location, this will be handled when the database is synced because the primary is already unique across the database.
- Talk to different app: When we have multiple apps that need to communicate with each other. Our records will have a unique identity
- Security: No one can guess your previous or next record by decrease or increase the number because record id now is a random string
- Able to generate your own id: Without sequence, you able create your own id with any string
- No waiting insert: When you multiple insert at the same time you record don’t need to wait for next sequence. It will go directly into your database.
Bad side of it
- Performance: Imagine that the primary key with a number of
[1, 2, 3]now is changed to the stupid crazy string like
831a4357-3ac3-47bf-998e-4e4fae8d9150it effects much on query performance
- Ugly: UUID is a random string. Which it won’t look right if you include in URL or some user interactive
First what we need is enable
pgcrypto plugin to Postgresql. this required as we to use gen_random_uuid to generate UUID primary when record is created.
We generate migration by run command
In your migration file
Add config to
Now you able to use primary as UUID in your app. 👏👏👏
When create new model
Ruby on Rails Command to create new model
Your migration file will look like
When you add your foreign key it, your migration file should look like
Need more advices about your Ruby On Rails App?
Contact Ruby On Rails Expert at UpWork