Kernel Compartido
Escrito por Darjus Kazakevic · revisado el
Two contexts co-own one small piece of model, and nobody changes it alone.
In an online shop, Sales and Billing both need a Money type and the same customer ID format. Writing them twice feels wasteful, and two copies of rounding rules will drift. A shared kernel is the explicit answer: one small package that both contexts import and both teams own together.
The cost is not the code but the coordination. Neither team may change the kernel alone, so every change needs both teams and a joint release. That works only while the kernel stays small and rarely changes.
En esencia — Share one small, stable piece of model between two contexts as common code, and require both teams to approve every change to it.
Cuándo recurrir a él
Recurre a él cuando
- Two contexts need the same small, stable type, like Money or an ID
- The two teams already plan together and review each other's code
- Two copies would have to match exactly, like rounding rules
Reconsidéralo cuando
- The shared part changes often, so every release needs a meeting
- One team would own it in practice: that is customer–supplier
- The teams cannot plan together, for example in different companies
Conceptos clave
- The kernel
- A small package of code, schema or both that two contexts import. It holds values and identities, like Money or CustomerId, not business rules.
- Joint ownership
- Both teams own the kernel equally, and any change needs approval from both. If one team can change it alone, that team now leads and the other must follow, which nobody agreed to.
- Joint releases
- Both contexts compile against the kernel, so a change ships in both at once. Each change costs a conversation and a test run on both sides, which is why the kernel must stay small.
Reglas de diseño
- Make joint approval a rule the tools enforce
- Require approval from both teams on every kernel change. A CODEOWNERS line that lists two teams accepts either one, so add a CI check that needs an approval from each team.
- Share values, not business rules
- Share arithmetic, identifiers and value types. Keep policies, like discount or tax rules, in each context. Every rule in the kernel is one that neither team can change alone.
- Shrink the kernel when it changes often
- If the kernel changes every sprint, the coordination costs too much. Copy the parts one side needs, or give ownership to one team and treat it as customer–supplier.
Errores habituales
- The kernel grows into a “common” library with half the domain in it
- One team changes it alone, and the other finds out from a broken build
- Business rules move in, and both contexts lose their freedom
- Two contexts writing to one database table and calling it a kernel