- Lower Kabete Rd, Nairobi 00100, Kenya
- Mon-Fri, 08:00am - 06:00pm
- +254 (798) 586113 +254 (728) 922269
- info@afriqsilicon.com
5 Min Read
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
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
| Metric | UUID | Auto‑increment | Custom sequential |
|---|---|---|---|
| Size per row | 16 bytes | 4 or 8 bytes | 8 bytes (typical) |
| Index fragmentation | High, random inserts | Low, append‑only | Low, append‑only if monotonic |
| Write latency | No coordination needed | Coordination to master node | Coordination 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
-
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.
-
Regulatory compliance, Guidelines do not dictate key format, but embedding a region code in a custom key can simplify audits that check data residency.
-
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). -
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:
| Question | Implication |
|---|---|
| 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.
Related reading
- Choosing a migration workbench for legacy system modernisation in Africa
- Choosing or building a core banking platform for Kenyan sacc os
- How to backup and restore a PostgreSQL database using Docker
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?
Can I switch primary key type after a system is live?
Who is responsible for generating custom sequential keys in a multi‑region setup?
How do connectivity constraints in Kenya affect the primary key choice?
Does the primary key affect data‑residency compliance?
Related Services
Working through this problem? These are the services we offer that connect to it.
Product Design & Implementation
From idea to production-ready software, built right the first time.
Custom software product design and development from Afriq Silicon. We build scalable, user-centered applications, from concept to deployment.
Explore serviceSystem Orchestration
Make your infrastructure invisible, reliably fast, quietly resilient.
IT system orchestration and infrastructure from Afriq Silicon. We design scalable, secure, integrated IT environments for growing organizations.
Explore serviceRelated
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.
June 11th, 2026
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.
January 4th, 2024
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.
November 6th, 2023
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.
November 3rd, 2026
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
Pages
- - Work
- - Services
- - Our Process
- - Contact Us
Solutions
- - Acts ML
- - Kilelehub
- - Other Projects
Legal
Contact
- - Lower Kabete Rd, Nairobi 00100, Kenya
- - Mon-Fri, 08:00am - 06:00pm
- - +254 (798) 586113
- - +254 (728) 922269
- - info@afriqsilicon.com