Quick revision
The short answer
A unique index only guarantees uniqueness inside one database or one shard, so once data is split, each global rule needs a single place that decides it. Either a small identity or registry service owns the unique claims, with its own unique index, and is called synchronously at sign-up, or user data is sharded by a hash of the normalised email so all candidates for one value meet on one shard's unique index. Identifiers become globally unique by design, such as UUIDv7 or Snowflake-style ids, and we document which rules are hard guarantees and which are checked eventually, with reconciliation jobs for the latter.
What to remember
In this answer, we saw that unique indexes only guarantee uniqueness inside one database or shard, so splitting a monolith removes global uniqueness unless we design for it. A registry service with its own unique index can own the claims for any number of attributes, called synchronously at sign-up with pending claims and cleanup, while sharding by a hash of the normalised email works for a single attribute. Globally unique ids such as UUIDv7 or Snowflake-style ids replace per-database sequences. We document which rules are hard guarantees and which are eventually checked with reconciliation, and migrate by backfilling the registry from the monolith first.
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.
A unique index only guarantees uniqueness inside one database or one shard, so once data is split, each global rule needs a single place that decides it. Either a small identity or registry service owns the unique claims, with its own unique index, and is called synchronously at sign-up, or user data is sharded by a hash of the normalised email so all candidates for one value meet on one shard's unique index. Identifiers become globally unique by design, such as UUIDv7 or Snowflake-style ids, and we document which rules are hard guarantees and which are checked eventually, with reconciliation jobs for the latter.
Why this question is asked
In a monolith database, uniqueness is free: one unique index on users.email and the database guarantees it for every insert. Splitting the database by service and then sharding quietly removes that guarantee, and the first sign is usually two accounts with the same username, months later. Interviewers ask this design question at senior level because the answer is a company-wide policy: which rules must stay hard guarantees, which component owns each one, and how ids are generated. In this answer, we will see exactly why unique indexes stop being enough, look at the two main designs for global uniqueness, choose an id strategy, separate hard from eventual rules, and plan the migration.
1Why Unique Indexes Stop Being Enough
A unique index checks a new key against the keys in the same index. On a single database, every insert of an email goes through the same index, so two concurrent inserts of ann@x.io meet and one fails. Once users are spread across shards, by user id for example, ann@x.io can be inserted on shard 3 and on shard 7 at the same moment. Each shard's index is satisfied, because each sees only its own rows. The same happens when two services each keep their own users table.
Per-database sequences break in the same way. Two databases that both start their id sequence at 1 will both produce user 1001. So both uniqueness of attributes and identity itself need a new design.
2Option A: A Registry Service That Owns the Claims
The first design gives each global rule a single owner. A small identity or registry service keeps a table of claims, such as (kind, normalised value) -> owner id, with a unique index, in one database that is not sharded or is sharded by the claim value. Sign-up calls it synchronously: claim the email and the username first, and only then create the user in the user's own shard. If the claim fails, the sign-up fails with 'already taken'.
6 messages in order. Play them, or step through them one at a time.
If the user creation fails, a job releases pending claims after a timeout.
Notice the status column. The claim and the user creation happen in different databases, so they cannot share a transaction. We claim as pending, create the user, then mark the claim active. If the user creation fails, a cleanup job releases pending claims older than a few minutes. Changing an email is a claim of the new value followed by release of the old one. The cost of this design is one more synchronous dependency in sign-up and email change: the registry must be highly available and fast, but its data is small and its operations simple.
3Option B: Shard by the Unique Attribute
The second design avoids a separate service by choosing the shard key so that all candidates for the same unique value meet in one place. If user records are sharded by a hash of the normalised email, every insert of ann@x.io goes to the same shard, and that shard's ordinary unique index enforces the rule. This works well for one unique attribute. It breaks down when there are two, say email and username, because a shard key can only follow one of them. It also makes changing the email a cross-shard move, and lookups by user id need a mapping from id to shard.
So option B fits systems with one dominant unique attribute that rarely changes. Option A fits systems with several unique attributes or frequent changes, and most companies end up with a registry for identity data.
| Strategy | Guarantee | Main cost |
|---|---|---|
| Registry service with unique index | hard, for any number of attributes | synchronous dependency at sign-up |
| Shard by hash of the unique value | hard, for one attribute | changing it moves data between shards |
| Per-shard check plus reconciliation | eventual, duplicates found later | cleanup and user-facing fixes |
| Globally unique ids (UUIDv7, Snowflake) | unique identity everywhere | larger keys, generator design |
4Identity: Globally Unique Ids
Per-database sequences are replaced by ids that are unique without coordination. UUIDv7 puts a millisecond timestamp in the first 48 bits and random bits after, so any service can generate one locally, collisions are practically impossible, and new ids are roughly in time order, which keeps B-tree inserts at the right edge. Snowflake-style ids pack a timestamp, a worker id and a sequence into 64 bits, which is smaller and also time ordered, but needs a scheme to assign unique worker ids. We pick one standard for the company and provide a shared library, so every service creates ids the same way.
5Hard Guarantees vs Eventual Checks
Not every uniqueness rule deserves a synchronous global check. We document each rule and decide its class. Hard guarantees, such as login email and public username, are enforced by a registry or by shard design before the write succeeds. Other rules, such as 'one loyalty card number per customer' imported from partners, can be checked per shard on write and verified globally by a reconciliation job that scans for duplicates, alerts and either merges automatically or opens a task for support. The documentation states the owner, the class and the repair process for each rule, so teams do not have to rediscover them.
6Planning the Migration
Before splitting, we build the registry and backfill it from the monolith's existing unique values, where uniqueness is still guaranteed. Then sign-up and email change write to both the monolith and the registry for a while, and a comparison job checks that they agree. Once they do, the registry becomes the authority and the services move to their own databases. Ids switch to the new scheme for new rows, while old ids stay valid, since they were already unique within the monolith.
7Follow-up questions
What happens if the registry service is down?
Sign-up and email changes fail or queue, while everything else, such as login and normal use, keeps working, because only claims need the registry. We make it highly available with a replicated database and multiple instances, keep it small and simple, and give it a clear SLO. Some teams accept a degraded mode where sign-ups are queued and confirmed later, at the cost of telling some users afterwards that their username was taken.
Why normalise values before claiming them?
Because uniqueness is only as good as the definition of equal. Ann@X.io and ann@x.io, or a username with a lookalike Unicode character, should count as the same. The registry stores a normalised form, such as lowercased, trimmed and Unicode NFKC-normalised, and all services use one shared normalisation function, so the same value always produces the same claim key.
8Conclusion
In this answer, we saw that unique indexes only guarantee uniqueness inside one database or shard, so splitting a monolith removes global uniqueness unless we design for it. A registry service with its own unique index can own the claims for any number of attributes, called synchronously at sign-up with pending claims and cleanup, while sharding by a hash of the normalised email works for a single attribute. Globally unique ids such as UUIDv7 or Snowflake-style ids replace per-database sequences. We document which rules are hard guarantees and which are eventually checked with reconciliation, and migrate by backfilling the registry from the monolith first.
Open with
The key point is that a unique index only sees its own database or shard. Once users are split across services and shards, two inserts of the same email on different shards both succeed.
Then, if they want more
So every global rule needs one place that decides it. My default would be a small identity or registry service that owns the claims, with a table of kind and normalised value and a unique index on it, called synchronously at sign-up and email change.
Because the claim and the user record live in different databases, I would claim as pending, create the user, then activate the claim, with a job that releases stale pending claims.
The alternative is to shard user data by a hash of the normalised email, so all inserts of the same email land on one shard and its ordinary unique index enforces the rule; that works for one attribute but not for email and username together, and email changes become cross-shard moves.
For identity, I would replace per-database sequences with UUIDv7 or Snowflake-style ids, through one shared library. Then I would document each uniqueness rule: which ones are hard guarantees enforced before the write, and which can be checked per shard and reconciled later by a job that finds and repairs duplicates.
For the migration, I would backfill the registry from the monolith while uniqueness is still guaranteed, run both in parallel with a comparison, and then make the registry the authority.
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 company is splitting a monolith database into service-owned databases and some will...": https://scrivio-interview-library.netlify.app/databases/indexing/5cc2539210/