Quick revision
The short answer
A key everyone assumed was immutable is about to change, so first we find every service that stores customer numbers as references. Then we introduce a stable surrogate or global customer id if there is none, keep the old number as an attribute with its history, publish mapping events from old to new numbers and accept lookups by both for a transition period, and invalidate caches, including Hibernate's natural id caches, because mutable natural ids need it. The lesson is to use natural ids for lookup and uniqueness, but never as foreign keys across systems.
What to remember
In this answer, we treated renumbering as a change to an identity that many systems hold. We inventoried every holder of the number, introduced a stable global id and kept old numbers as history, ran a transition with CustomerRenumbered events and lookups by both numbers, evicted caches including Hibernate's natural id caches because the natural id is now mutable, and drew the lesson that natural ids are for lookup and uniqueness, not for references between systems.
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 key everyone assumed was immutable is about to change, so first we find every service that stores customer numbers as references. Then we introduce a stable surrogate or global customer id if there is none, keep the old number as an attribute with its history, publish mapping events from old to new numbers and accept lookups by both for a transition period, and invalidate caches, including Hibernate's natural id caches, because mutable natural ids need it. The lesson is to use natural ids for lookup and uniqueness, but never as foreign keys across systems.
Why this question is asked
Renumbering customers sounds like an update statement, and in a single database with surrogate keys it nearly is. The trouble starts when the customer number has been used as the identity everywhere: as the natural id in a dozen services, as the key in events, in partner files, in caches and in URLs. Changing it then touches every system at once, and any system that misses the change silently loses the link between a customer and their orders. Interviewers ask this to see whether we can plan a change to an identity across many systems without a big-bang cut-over. In this answer, we will map the impact, introduce a stable id, run a transition with mapping events and dual lookups, handle caches, and finish with the lesson for future designs.
1Finding Everything That Holds a Customer Number
The first job is an inventory, because the number is copied in more places than anyone remembers. We search code and schemas for the column names and types used for customer numbers, check every Kafka topic schema and API contract for fields that carry them, and ask the data team which warehouse tables join on them. Outside our systems we list partner integrations, export files, printed documents such as invoices and contracts, and URLs that customers may have bookmarked.
For each place we record whether it stores the number as a reference to the customer, which is what makes renumbering hard, or only displays it. A service that keeps orders with a customer_number column and no other customer key is the most affected kind.
| Holder | How it uses the number | Change needed |
|---|---|---|
| customer-service | @NaturalId, owner of the data | add global id, keep history |
| order-service | foreign key customer_number | add customer_id, backfill |
| billing-service | natural id cache, invoices | invalidate cache, dual lookup |
| events customer.* | message key | add global id field |
| data warehouse | join key | mapping table, rebuild joins |
| partner files | reference in CSV | send both numbers for a period |
2Introducing a Stable Identifier
If customers do not yet have a surrogate or global id, we add one now, for example a UUID generated once per customer, and make it the identity that other services store. The customer number becomes an attribute again, still unique and still searchable, but no longer the thing that ties records together. We keep the old number too, with the period it was valid, so that old documents, partner references and support conversations can still be resolved.
Notice @NaturalId(mutable = true). The number is still the business lookup key, but it is no longer immutable, and Hibernate must be told so; otherwise it treats the change as an error at flush.
3Running the Transition
We avoid a big-bang switch. The customer service publishes a CustomerRenumbered event for each customer, carrying the global id, the old number and the new number, so every consumer can update its references at its own pace. During a transition period, every lookup API accepts either number and resolves old numbers through the history table, and outgoing events and files carry both the global id and the new number, plus the old number where partners still need it.
- Inventory every holder of the number
- Add a stable global idbackfill in the owner and in consumers
- Publish CustomerRenumbered eventsglobal id, old and new number
- Accept both numbers in lookupshistory table resolves old ones
- Consumers switch references to the global id
- Flush caches, then retire old referenceskeep history lookups for support
6 steps in order. Play them, or step through them one at a time.
Each consuming service follows the same steps: add the global id column, backfill it, start writing it, switch reads to it, and finally stop using the number as a reference. We track progress per service, and the transition ends only when no system needs the old numbers for references any more. Lookups by old numbers can stay for years for support and documents; that is cheap once it reads from a history table.
4Caches and Natural Id Caches
Every cache keyed by customer number becomes wrong the moment a number changes: application caches, HTTP caches of customer pages, search indexes and Hibernate's natural id resolution. Within a session, Hibernate updates the resolution for a mutable natural id at flush, but a second-level @NaturalIdCache shared across instances can still map the old number to the customer, or the new number to nothing, until it is evicted. So the renumbering job evicts the affected regions, and each consuming service evicts its own caches when it processes the renumbering event. We run the actual renumbering in batches at a quiet time, and verify afterwards by sampling lookups by old and new numbers in every service.
5The Lesson for Future Designs
The root cause was not the merger; it was using a business key as the identity between systems. Natural ids are excellent for lookup and for enforcing uniqueness: people search by customer number, and the database should reject duplicates. They are poor as foreign keys across systems, because the business can always decide to change them, as it just did. If services had referenced customers by a stable surrogate or global id from the start, renumbering would have been an update in one service plus an event for display purposes.
6Follow-up questions
Could we keep the old numbers and simply add a prefix for the acquired customers?
Sometimes that is the cheapest option, and it is worth asking the business whether renumbering is really required. If the goal is a uniform format for everyone, though, a prefix only postpones the problem, and the same plan is needed anyway.
How do we handle printed invoices that show old numbers?
They cannot change, so the history table must resolve old numbers indefinitely, with the validity period, and support tools must accept both. That is one more reason to keep former numbers as data rather than overwriting them.
What if some consumers cannot be changed during the transition?
Keep a translation at the boundary: an adapter that maps between global ids, new numbers and old numbers for that consumer, owned by the team that owns the integration, until the consumer can be updated.
7Conclusion
In this answer, we treated renumbering as a change to an identity that many systems hold. We inventoried every holder of the number, introduced a stable global id and kept old numbers as history, ran a transition with CustomerRenumbered events and lookups by both numbers, evicted caches including Hibernate's natural id caches because the natural id is now mutable, and drew the lesson that natural ids are for lookup and uniqueness, not for references between systems.
Open with
I would treat it as an identity change across many systems, not a database update. First, an inventory: every service, event schema, warehouse table, partner file and URL that holds customer numbers, and for each whether it uses the number as a reference or only displays it.
Then, if they want more
Then I would make sure there is a stable identifier. If customers do not have a surrogate or global id, I add one, a UUID per customer, and make that what other services store.
The customer number becomes an attribute again, mapped as a mutable natural id, and the old numbers are kept in a history table with their validity, because invoices and partners will keep quoting them.
The transition is gradual: the customer service publishes a CustomerRenumbered event with the global id, old and new number, lookup APIs accept both numbers for a transition period, and each consumer adds the global id column, backfills it, switches reads and then stops using the number as a reference.
Caches need attention, because every cache keyed by number goes stale, including Hibernate's natural id cache in the second-level cache, so the job and the consumers evict them as events are processed.
The lesson I would write down is that natural ids are great for lookup and uniqueness, but should never be used as foreign keys between systems, because the business can always decide to change them.
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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Customer numbers were used as natural ids in many services, and now the business wants...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/aee91026d7/