•

5 Min Read

Choosing the right primary key for a distributed database

Choosing the right primary key for a distributed database

Learn the trade‑offs between UUIDs, auto‑increment integers and custom sequential keys for distributed databases, and get a decision framework that fits

UUID vs auto‑increment vs custom sequential keys in a distributed database

When you design a new system that will run across several data centres or cloud zones, the primary key you pick influences three things you care about most: performance, replication behaviour, and operational simplicity.

1. How each key type works

  • UUID (Universally Unique Identifier), a 128‑bit value that can be generated anywhere without coordination. It guarantees uniqueness across nodes, but its random nature means new rows are inserted at random positions in a B‑tree index.

  • Auto‑increment integer, a simple counter stored in the database. The database hands out the next number when a row is inserted. It gives strictly increasing keys, which keep the index ordered, but the counter lives on a single node, so every write must reach that node.

  • Custom sequential generator, a scheme that produces increasing values while allowing distributed generation. Typical designs are Snowflake‑style IDs (timestamp + machine ID + sequence) or a dedicated ID service that hands out blocks of numbers to each node.

2. Performance impact

MetricUUIDAuto‑incrementCustom sequential
Size per row16 bytes4 or 8 bytes8 bytes (typical)
Index fragmentationHigh, random insertsLow, append‑onlyLow, append‑only if monotonic
Write latencyNo coordination neededCoordination to master nodeCoordination only for block allocation

Because UUIDs are larger, they increase the size of primary‑key indexes and any secondary indexes that reference them. On a low‑bandwidth link, the extra bytes can add up, especially for high‑volume tables. Auto‑increment keys keep the index compact, but the single‑node counter can become a bottleneck if you have many write‑heavy services spread across regions. Custom generators strike a middle ground: they keep the index ordered and avoid a single point of contention, at the cost of an extra service or a more complex allocation algorithm.

3. Replication and multi‑region behaviour

  • UUID, each replica can accept writes independently because the key is guaranteed unique, which works well with databases that support multi‑master replication.
  • Auto‑increment, writes must be routed to the node that owns the counter. If you replicate across regions, you either force all writes to a single primary region (adding latency) or you accept duplicate‑key errors when two regions try to allocate the same number.
  • Custom sequential, by handing out non‑overlapping blocks to each region, you preserve the monotonic order while allowing local writes. The trade‑off is the need to manage block exhaustion and to rebuild the allocation service if you add or remove nodes.

4. Operational considerations for African deployments

  1. Connectivity, Many public‑sector projects operate on intermittent links. A key that can be generated locally (UUID or pre‑allocated block) avoids a round‑trip to a central coordinator.

  2. Regulatory compliance, Guidelines do not dictate key format, but embedding a region code in a custom key can simplify audits that check data residency.

  3. Tooling, Our stack, which includes Docker, Pulumi and GitHub Actions, can provision a Snowflake‑style ID service in a container and expose it via a stable HTTP endpoint. The same pipeline can also set up a UUID‑generating function in the database (gen_random_uuid() in PostgreSQL).

  4. Future migrations, Switching primary key type after launch means rewriting every foreign key and re‑indexing tables. That effort is proportional to the amount of data and the number of dependent services. It’s far cheaper to decide early during the discovery session of our product‑design‑implementation engagement.

5. Decision framework

Use the following checklist during your discovery phase to narrow the choice:

QuestionImplication
Do you need local write capability in every region?Favor UUID or custom sequential.
Is write throughput expected to exceed a few thousand rows per second?Avoid a single auto‑increment counter; pick UUID or custom.
How important is index size for your storage budget?Choose integer‑based keys if storage cost is critical.
Do you have strict ordering requirements (e.g., audit logs)?Custom sequential gives monotonic order; UUID does not.
Will you add new regions later?Custom sequential blocks simplify scaling; UUID works automatically.

Answering these questions gives you a clear recommendation:

  • Pick UUID when you prioritize ease of deployment, want true multi‑master writes, and can tolerate larger indexes.
  • Pick auto‑increment when you run a single‑region or tightly coupled cluster, need the smallest possible index, and can route all writes to the master node.
  • Pick a custom sequential generator when you need ordered keys, want to avoid a single write bottleneck, and are comfortable operating an extra service for ID allocation.

6. How we help you pick and implement

During the discovery session we map the existing systems, capture the write patterns and latency constraints, and then produce a proposal that includes the recommended key strategy, the stack rationale and a proof‑of‑concept validation. Our system‑orchestration service will stand up the necessary infrastructure (Docker, Pulumi, monitoring) and hand over a fully documented, monitored system that your team can operate long after launch.

If you’re already running a system with the “wrong” key type, our project‑restoration service can audit the schema, unblock the migration and bring the database back to a maintainable state.

Ready to decide which primary key fits your distributed system? Talk to our team about it.

Photo by Gustavo Fring on Pexels.


Frequently Asked Questions

Common questions on this topic, answered by the Afriq Silicon team.

What drives the cost difference between UUIDs and integer keys?
UUIDs are larger (16 bytes) so they increase storage and index size, which can raise I/O and cloud storage costs; integer keys are 4, 8 bytes and keep indexes tighter, reducing those costs.
Can I switch primary key type after a system is live?
It’s possible but requires a migration that rewrites the primary key column, updates all foreign keys and re‑indexes tables, which can be a lengthy, risky operation, plan it early in the discovery phase.
Who is responsible for generating custom sequential keys in a multi‑region setup?
The generation logic should live in a service that all nodes can call, such as a Snowflake‑style ID server or a database sequence scoped to a region, and its ownership is defined in the system‑orchestration contract.
How do connectivity constraints in Kenya affect the primary key choice?
Poor connectivity makes round‑trip latency costly; a locally generated sequential key avoids cross‑region calls, while UUIDs can be generated client‑side without coordination, which may be preferable when network latency is high.
Does the primary key affect data‑residency compliance?
The key itself does not store personal data, but if you embed region identifiers in a custom key (e.g., “KE‑2023‑…”) it can help auditors verify that records stay within the required jurisdiction.

Related

Similar Articles

Stay Informed with Our Latest Articles: Explore the most recent insights, trends, and updates from our industry experts. Dive into a wealth of knowledge to keep you ahead in the ever-evolving tech landscape.

Scalable IT Infrastructure: What It Actually Takes

June 11th, 2026

trendse-commercedesignsaa-s

Scalable IT Infrastructure: What It Actually Takes

Scalability sounds simple until you need it. Then it becomes the most expensive problem you didn't plan for.

By Titus Mwangi 3 Min Read
Keycloak Multitenancy Approaches for SaaS

January 4th, 2024

saa-sdesign

Keycloak Multitenancy Approaches for SaaS

Learn how to implement Keycloak multitenancy for SaaS platforms, with practical patterns for realms, groups, clients, and identity‑provider federation.

By Harman Kibue, Titus Mwangi 3 Min Read
Agile Development at Afriq Silicon

November 6th, 2023

projectdesign

Agile Development at Afriq Silicon

How we actually run two-week sprints, and the deployment pipeline that makes each increment something you can click on rather than a status update.

By Harman Kibue 1 Min Read
Designing and Implementing an ERP for African Public Institutions

November 3rd, 2026

project

Designing and Implementing an ERP for African Public Institutions

A step‑by‑step guide for designing and implementing an ERP in African public institutions, covering discovery, procurement, architecture, development

By Titus Mwangi 3 Min Read
noise

Let’s Build Something
Amazing Together

Afriq Silicon

We will help you turn ideas into digital reality whatever industry you want to revolutionize

Solutions

Contact

© 2026 Afriq Silicon, Inc. All rights reserved