Should you use UUIDs as the primary key in your database? You might have heard they are terrible for performance, which is often true for traditional UUIDv4.
However, the introduction of UUIDv7 fixes many of the previous issues of UUIDv4. Let’s therefore explore what they are and why it might be a good idea to use them.
What is UUIDv7?
UUIDv7 is a relatively new type of Universally Unique Identifier (UUID). It was introduced to Postgres in version 18 to mitigate performance issues associated with using traditional UUIDs, UUIDv4, as database primary keys.
Unlike the traditional UUIDv4, which is completely random, UUIDv7 incorporates a timestamp as the most significant part of its 128-bit structure, allowing for natural sortability based on the creation time.
This has several advantages we will cover in the post, but rather than just read about it, you can try it out for yourself using the commands below.
Creating our Crab store
Let's create a new free Aiven for PostgreSQL® service for our demo Crab store using the Aiven CLI.
avn service create -t pg --cloud do-nyc --plan pg:free-1-1gb 'crab-store' -c 'pg_version=18'
Once that's done, we can create two tables, with the only difference being that one table uses UUIDv4 and the other uses UUIDv7.
... continue reading