Quick revision
The short answer
The standard would say: a bigint identity primary key for internal tables, plus a separate opaque public id (a UUID) wherever an id leaves the service; never expose sequential internal ids; real foreign keys inside a service's own database, but plain id references with event-driven consistency and reconciliation across services; a unique constraint on every natural key the business relies on and an index on every foreign key. It would be enforced with templates and CI checks, with owned exceptions, not a wiki page.
What to remember
In this answer, we built a key standard for 150 services: one primary key choice per class of table, opaque public ids for anything that leaves a service, required and indexed foreign keys inside a database, id references with events and reconciliation across services, and unique constraints on every natural key the business relies on. We finished with how to enforce it through templates, CI linters and owned exceptions instead of documentation alone.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The standard would say: a bigint identity primary key for internal tables, plus a separate opaque public id (a UUID) wherever an id leaves the service; never expose sequential internal ids; real foreign keys inside a service's own database, but plain id references with event-driven consistency and reconciliation across services; a unique constraint on every natural key the business relies on and an index on every foreign key. It would be enforced with templates and CI checks, with owned exceptions, not a wiki page.
Why this question is asked
With 150 services, key choices that look like local taste become company-wide problems: two services exchange ids of different types, an export leaks how many orders we take per day, or a merge of two datasets finds duplicate customers because nobody enforced the natural key. Interviewers ask this at senior level to see whether we can turn data modeling knowledge into a standard people actually follow. In this answer, we will go through what the standard decides about primary keys, public ids, foreign keys inside and across services, natural keys, and finally how we make it stick.
1A Quick Recap of the Choices
A surrogate key is an id generated by the system with no business meaning, such as a bigint from an identity column or a UUID. A natural key is a business value that is unique by itself, such as an ISO country code, an email address or an invoice number. A foreign key is a reference from one table to another that the database checks.
Each surrogate type has costs. A bigint is 8 bytes, sorts in insertion order and keeps B-tree indexes compact, but it reveals ordering and volume if anyone outside can see it. A random UUID (version 4) is 16 bytes, can be generated anywhere without coordination and reveals nothing, but random inserts scatter across the index. A time-ordered UUID (version 7) is generated anywhere and inserts in near order, though it does reveal the creation time.
2Primary Keys: One Choice per Class of Table
The standard should not allow every team to pick freely, because mixing types is what makes integration painful. It also should not force one type everywhere. A practical rule is to define classes of tables and one choice per class.
| Class of table | Primary key | Public id |
|---|---|---|
| Internal entity (orders, accounts) | bigint identity | UUID column, unique, exposed in APIs |
| Entity created offline or in many places | UUID v7 | Same UUID |
| Join table | Composite of the two foreign keys | None |
| Reference data (countries, currencies) | Natural code (ISO) | Same code |
| Event or log row | bigint identity or UUID v7 | Event id in payload |
The first row is the default most services will use. The internal bigint keeps joins and indexes small, and a separate public_id column with a unique constraint is what the API, URLs, events and exports carry. Notice that reference data keeps its natural key: a currency code like 'EUR' is stable, short and understood by every system, so wrapping it in a surrogate only adds a lookup.
3Why Internal Ids Stay Internal
A sequential id in a URL such as /orders/104233 tells a competitor how many orders we took this month: they place one order on the first and one on the last day and subtract. It also invites enumeration, where an attacker tries 104232, 104231 and so on, and any missing authorization check becomes a data leak. Ids also leak into partners' databases through exports, after which we can never renumber or merge our own tables.
So the standard says: ids that leave the service are opaque public ids, and the internal key is never part of a public contract. This costs one extra column and one unique index per exposed table, which is small compared to the incidents it prevents.
4Foreign Keys Inside a Service, References Across Services
Inside one service's database the standard requires foreign keys. They are cheap (one index lookup per write) and they stop orphans from scripts, migrations and bugs that the application cannot see. Every foreign key column is indexed, because PostgreSQL does not do it automatically and an unindexed foreign key makes parent deletes scan the child table.
Across services there is no shared database, so there can be no database constraint. A shipping service stores the order's public_id as a plain column. The standard then has to say how consistency is kept: the owning service publishes events (OrderCancelled, CustomerMerged), consumers react to them, and a scheduled reconciliation job compares references with the owner's data and reports or repairs drift. That is weaker than a constraint, so the standard should also say what a consumer does when it meets an id the owner does not know: keep it and flag it, never silently drop it.
5Natural Keys Still Get Unique Constraints
Choosing a surrogate primary key does not remove the natural key; it just stops it being the primary key. If the business relies on 'one customer per email' or 'one invoice per invoice number per legal entity', that rule gets a unique constraint. Without it, two concurrent requests create duplicates, and every downstream export carries them. The standard's wording can be simple: every business rule of the form 'there is only one X per Y' is a unique constraint or unique index, including partial ones such as WHERE deleted_at IS NULL.
6Making the Standard Stick
A standard that lives only on a wiki page is ignored within a quarter. What works is to make the right thing the easy thing. We provide a service template whose first migration already has the key pattern, a migration linter in CI that fails on a table without a primary key, a foreign key without an index, or an API DTO that exposes an internal id, and a short decision record explaining why. Exceptions are allowed, but each one is recorded with an owner and a reason, so they are visible and reviewed.
Existing services are not rewritten in one go. We measure them against the linter, publish the list, and fix the dangerous items first (exposed sequential ids, missing unique constraints on money-related natural keys), while new tables follow the standard from day one.
7Follow-up questions
How do you handle existing services that violate the standard?
I would run the same linter over their schemas and publish a scorecard rather than demand a rewrite. Then I rank violations by risk: a public API exposing sequential ids or a missing unique constraint on invoice numbers gets fixed this quarter, while an old table with serial instead of identity can wait until it is touched anyway. Fixes are incremental, such as adding a public_id column and supporting both ids in the API for a deprecation period. Every remaining violation is listed as an exception with an owner.
Why not use UUIDs everywhere and skip the second column?
That is a valid choice for some companies, especially with UUID v7, which avoids most of the index scattering of random UUIDs. The cost is wider keys in every foreign key and index, often 30 to 50 percent larger indexes on join-heavy tables. The two-column pattern keeps internal storage compact and makes the public id freely changeable. Either is fine as long as the standard picks one per class and does not leave it to each team.
Who owns the reconciliation between services?
The consuming service owns its own references, because only it knows what a dangling id means for its data. The platform team provides the tooling, such as a reusable job that compares a column of ids with the owner's export and reports drift as a metric.
8Conclusion
In this answer, we built a key standard for 150 services: one primary key choice per class of table, opaque public ids for anything that leaves a service, required and indexed foreign keys inside a database, id references with events and reconciliation across services, and unique constraints on every natural key the business relies on. We finished with how to enforce it through templates, CI linters and owned exceptions instead of documentation alone.
Open with
I would write a short standard with a few decisions and the tooling to enforce them. First, primary keys: internal tables use a bigint identity, join tables use a composite of their two foreign keys, reference data keeps natural codes like ISO currency codes, and data created in many places can use UUID version 7.
Then, if they want more
Second, any id that leaves the service, in APIs, URLs, events or exports, is a separate opaque public id, usually a UUID with a unique constraint. Sequential ids leak volume and invite enumeration, and once a partner stores them we can never change them. Third, inside one service's database foreign keys are mandatory and every foreign key column is indexed.
Across services there are no database constraints, so references are plain ids, the owner publishes events like cancellations and merges, and consumers run reconciliation jobs and flag unknown ids rather than dropping them. Fourth, every natural key the business depends on, like invoice number per legal entity, gets a unique constraint even though it is not the primary key.
And finally, I would not rely on a wiki: a service template with the pattern built in, a migration linter in CI, and exceptions recorded with an owner. Old services get a scorecard and the risky violations fixed first.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your platform team must set a standard for primary keys and foreign keys across 150...": https://scrivio-interview-library.netlify.app/databases/data-modeling/a338873297/