When would you extract the Timetabling module of a university's ASP.NET Core modular monolith into its own service, and what changes in its registration, data and cross-module calls?
answer
- a reason beyond tidiness
- AddX and MapX move hosts
- schema becomes a database
- same contract, typed HttpClient behind it
- in-process events need a broker
basics
~20 sExtract only for a concrete reason: scaling, release cadence, failure isolation or ownership. Then its AddX and MapX calls move into a new host, its schema becomes its own database, and callers keep its contract interface, now backed by a typed HttpClient.
solid answer
~50 sI would extract Timetabling when it has a reason the monolith cannot meet: timetable generation needs very different scaling at the start of term, it must release independently, or its failures keep taking admissions down. Not because microservices look tidier. If the module was built well, the move is mechanical. A new ASP.NET Core project's `Program.cs` calls `AddTimetablingModule` and `MapTimetablingEndpoints`, and the monolith deletes those two lines. `TimetablingDbContext` already has its own schema and history table, so its connection string points at a new database once the data is copied. Other modules depended on `ITimetablingModule` from Timetabling's Contracts project; the monolith re-registers that interface with `AddHttpClient<ITimetablingModule, TimetablingHttpClient>()`, so callers do not change. In-process events become broker messages, ideally through an outbox. What makes it hard is whatever bypassed those seams: shared tables, chatty calls inside one request, a cross-context transaction.
go deeper
Know that a well-built module can be moved into its own service because its registration, data and contracts are already separate.
Explain what moves: the AddX and MapX calls into a new Program.cs, the DbContext to a new database, and the contract behind a typed HttpClient.
Describe the order of work and what breaks: shared tables, chatty contracts, cross-module transactions and events that need an outbox and a broker.
Make the call: name the business reason, the evidence you would gather, the cost of two deployables, and when you would decide not to extract.
## Decide whether to extract at all This is a judgement call. Extraction swaps in-process calls for network calls, one deployment for two, and one database transaction for eventual consistency. It should buy something specific. Reasons that justify it for Timetabling: - **Scaling profile**: timetable generation is CPU-heavy for a few weeks each term, while the rest of the system is steady. - **Release cadence**: the timetabling team ships solver changes several times a day and does not want to redeploy fees and admissions with them. - **Failure isolation**: a runaway solver should not starve admissions requests of threads and memory. - **Ownership**: a separate team now owns timetabling end to end. Reasons that do not: "microservices are the target architecture", or a module that is merely messy. Extraction does not clean a module; it makes its mess more expensive. ## What the earlier module design buys you If the module followed the per-module conventions, most of the work is relocation: | Seam inside the monolith | After extraction | |---|---| | `AddTimetablingModule` and `MapTimetablingEndpoints` called by the monolith's `Program.cs` | the same two calls in the new service's `Program.cs`; deleted from the monolith | | `TimetablingDbContext` with `HasDefaultSchema("timetabling")` and its own history table | same context, connection string pointing at a new database | | `ITimetablingModule` from the Contracts project, implemented in-process | same interface, implemented by a typed client registered with `AddHttpClient<ITimetablingModule, TimetablingHttpClient>()` | | in-process `IModuleEventBus` | a broker-backed implementation of the same interface, fed from an outbox | | internal types, no `InternalsVisibleTo` to other modules | nothing to untangle | The Contracts project becomes the client package for other modules. Because the monolith's callers inject `ITimetablingModule`, re-registering it with a typed `HttpClient` changes no calling code. The interface does need reviewing, though: a method that was cheap in-process, called in a loop per student, becomes a network call per student. ## Order of work 1. Remove anything that bypasses the seams: queries into timetabling tables from other modules, a shared cross-context transaction, internals access. 2. Make the contract coarse enough for a network: batch methods instead of per-item calls, explicit timeouts. 3. Introduce an outbox for events that must not be lost, while still in-process. 4. Stand up the new host with the module's `AddX` and `MapX`, its own configuration, authentication validating the same tokens, and health checks. 5. Copy the `timetabling` schema to the new database and point both the new service and any remaining jobs at it. 6. Switch the monolith's `ITimetablingModule` registration to the typed client and its event bus to the broker. 7. Delete the module from the monolith and remove its project reference. Steps 1 to 3 happen inside the monolith and are worth doing even if extraction is later cancelled. ## What makes it hard in practice - **Shared data**: another module mapped `timetabling` tables or joined them in a view. - **Chatty contracts**: per-item calls that were nanoseconds in-process and are now milliseconds each. - **Transactions across modules**: a cross-context transaction has no equivalent across two databases. - **Hidden runtime coupling**: shared in-memory caches or singletons both modules relied on. ## Keeping the option open while staying a monolith Most modules are never extracted, and that is fine. What matters is that the option stays cheap. Checks that keep it cheap, all enforceable inside the monolith: - No module grants `InternalsVisibleTo` to another module or to the host. - Each module's implementation project is referenced by the host only; other modules reference its Contracts project. - Each module's `DbContext` has its own schema and history table, and no context maps another module's tables. - Cross-module calls go through contract interfaces with coarse, batch-friendly methods. - Events cross modules through an event bus interface, not direct handler calls. An architecture test can assert most of these, so the readiness to extract is visible in CI rather than discovered during the extraction. ## How to answer Give the business reason first, then show that the ASP.NET Core seams (two registration calls, a context with its own schema, a contract interface, an event bus interface) make the move mechanical, and name what would make it expensive in this particular codebase.
- During the switch, how do you avoid a big-bang cutover of ITimetablingModule?Register the implementation behind a configuration flag: the in-process facade or the typed client. Run with the in-process one, deploy the new service, flip the flag in one environment, compare behaviour, then remove the in-process path once traffic has moved. The callers never change because they only see the interface.
- What should be measured before deciding to extract?The evidence behind the stated reason: resource use of timetabling work against the rest of the host, how often its releases are blocked by other modules or block them, incidents where its failures affected other modules, and how many cross-module calls per request touch it, which predicts network cost after the split.
- Every module of a modular monolith should eventually become a microservice.
- Extraction requires rewriting every caller of the module's contract.
- A module that shares tables with others can be extracted by copying its schema.
- In-process events keep working unchanged after the module moves to another process.