Skip to content
rusthacks.dev
Databases

UUIDs Versus Integer Keys in Rust Services and Their Costs

Integer keys are small and ordered, UUIDs are unguessable and generated anywhere. Compare index size, insert behavior and exposure, and see why UUID v7 changes the answer.

2 min read
Summarize in AI
Flat illustration for the post “UUIDs Versus Integer Keys in Rust Services and Their Costs”, composed around database.

The argument over primary keys is mostly about three things: size, insert pattern and what the ID reveals. Integers win the first two, UUIDs win the third and the ability to generate IDs anywhere. UUID v7 closes most of the gap on the second. This post puts the trade-offs side by side, with the Rust types for each. It is a design companion to SQLx explained.

uuid 1, sqlx 0.8, PostgreSQL 17, Rust 1.90.

The schema options

SQL
-- integer identity
CREATE TABLE orders (
    id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    total_cents BIGINT NOT NULL
);

-- UUID generated by the application
CREATE TABLE invoices (
    id UUID PRIMARY KEY,
    total_cents BIGINT NOT NULL
);

PostgreSQL 17 has no built in uuidv7() function (it arrived in a later release), so generate v7 values in Rust:

rust
use uuid::Uuid;

let id = Uuid::now_v7();
println!("{id}");

with uuid = { version = "1", features = ["v7"] }. The value is a timestamp in the first 48 bits followed by random bits, so two IDs created a second apart sort in creation order.

Cost

bigintUUID v4UUID v7
Size in the heap and index8 bytes16 bytes16 bytes
Insert patternappendrandommostly append
Guessableyesnopartly (the timestamp)
Generated bydatabaseanywhereanywhere

Every secondary index stores the primary key, so a table with four indexes pays the extra 8 bytes in five places. We do not quote a speed difference, because it depends on table size, memory and fill factor. On a table that fits in memory the difference is small; the cost shows up when the index no longer fits in the cache, where random inserts touch pages all over. To test your own case, insert a few million rows with each key type and compare index size with pg_relation_size.

Exposure

An integer ID in /orders/1042 tells a visitor roughly how many orders exist and invites trying /orders/1043. If that returns someone else's order, the problem is the missing authorization check, which no ID format fixes. Still, an unguessable ID removes a free enumeration vector and hides business volume. UUID v7 leaks a creation time, which is rarely sensitive but worth knowing.

Generation anywhere

Offline clients, event sourcing and merging data from several databases need IDs that do not collide without coordination. UUIDs do this by design. An integer sequence needs a single authority or per-node ranges.

Mixed approach

Keep id BIGINT as the internal key for joins and add public_id UUID NOT NULL UNIQUE for APIs. Typed wrappers prevent mixing the two, as in newtypes for database IDs.

When the choice does not matter

Small tables, internal tools and prototypes. Sharded or distributed databases usually require UUIDs or a ULID-like scheme. Standards: UUID v7 is defined in RFC 9562. The uuid crate documents the versions at docs.rs.

Taggeduuid
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.