Video in progress
The business split one concept into two (for example, "customer" is now "payer" and "user"). How do you evolve a model that many services use?
First I define the new concepts clearly in domain language and introduce them as new types side by side, without deleting the old model right away. Then expand–migrate–contract: add the new model, move consumers over step by step, deprecate the old model and finally remove it.
📖 Answer
The situation: Customer is everywhere: payments, profile, notifications. Now corporate customers have appeared: the company pays (Payer), the employees use the system (User). One customerId now means two different things.
Mistakes I've seen in practice:
- adding a
typeflag toCustomerand anif (customer.isCorporate())everywhere: the concept wasn't split, there was just more confusion; - renaming in all services overnight: one consumer gets forgotten and breaks in prod.
A plan that works:
- Language. A glossary with the domain experts: who pays, who logs in, who gets messages. Without this step the code just gets the old confusion with new names.
- Expand. In the owning service, new types and IDs:
PayerId,UserId. The oldcustomerIdstays and there's a mapping table. New fields are added to the API and events, while the old ones are still emitted. - Migrate. Consumers move to the concept that fits their context: billing to
Payer, notification toUser. Each at its own pace. I watch through metrics or logs who is still using the old field. - Contract. Once nobody reads it, the old field is deprecated and removed in the next major version.
At the OOP level: the new types are separate value objects (PayerId, UserId), so the compiler won't let you mix them up. If you leave a plain Long, IDs getting swapped during the migration is almost inevitable.
This work usually takes several quarters. The staff engineer's job is to keep the system working at every stage and make sure the final "contract" step isn't forgotten.
Sign up to mark studied questions and save your progress.