Microservice boundaries and cost
Microservice boundaries and cost
Microservices are independently deployable services organized around business capabilities. Splitting a process creates network, ownership and operational boundaries; it is not just moving classes into different folders.
Start with a modular monolith unless independently deploying or scaling a capability solves a demonstrated problem. A bounded context describes the meaning of its model; it does not prescribe one process per entity. For a fictional inventory and orders system, inventory owns stock reservation and exposes a narrow contract, while orders owns order status. Neither should update the other's tables. Synchronous calls are appropriate when an immediate result is required; asynchronous messages decouple time but add queue age, duplicate delivery, schema evolution and eventual consistency. A gateway can route requests and apply shared edge policies, but domain authorization remains in the service. Independent releases require backward-compatible contracts, telemetry, runbooks, identity, network budgets and on-call ownership. Load balancing distributes calls but cannot fix a broken contract or guarantee shared state consistency. Record the decision and review trigger in an ADR. The following numbers are fictional estimates and examples are local simulations, not network or capacity benchmarks.
Baseline: .NET 10 / C# 14. All C# results below are reasoned, not executed. Offline models and the separate Aspire lab are explicitly distinguished.
Compare scaling scenarios
Assume the combined process needs 3 units per replica, while orders needs 2 and inventory needs 1. Inventory alone requires four replicas. Ignore all shared overhead initially.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;int monolith = 4 * 3;int separate = 1 * 2 + 4 * 1;int estimatedOverhead = 3;Console.WriteLine($"combined={monolith}; split={separate + estimatedOverhead}");Console.WriteLine($"difference={monolith - separate - estimatedOverhead}");Expected result (not executed):
combined=12; split=9difference=3This is a cost-unit model, not cloud pricing. Raising split overhead to 6 removes the modeled advantage; actual capacity and failure tests are required.
Keep service ownership explicit
Orders receives a reservation result through a contract; it does not touch inventory fields.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;var inventory = new Inventory(3);var orders = new Orders(inventory);Console.WriteLine(orders.Place(2));Console.WriteLine(orders.Place(2));Console.WriteLine(inventory.Available);interface IInventory { bool Reserve(int quantity); }sealed class Inventory(int initial) : IInventory { public int Available { get; private set; } = initial; public bool Reserve(int quantity) { if (quantity <= 0 || quantity > Available) return false; Available -= quantity; return true; }}sealed class Orders(IInventory inventory) { public string Place(int quantity) => inventory.Reserve(quantity) ? "accepted" : "rejected";}Expected result (not executed):
acceptedrejected1A synchronous interface in one process is not a network client. It demonstrates ownership and an explicit result; a distributed implementation needs timeout, replay and transaction design.
Read compatible versions of a response
An older consumer reads the required fields and ignores an additive optional property.
Use the archived Fixture.csproj with an approved .NET 10 SDK. No third-party packages are required. Inspect the project, then build and run with the approved SDK.
using System;using System.Text.Json;var old = """{"id":"A","reserved":true}""";var next = """{"id":"A","reserved":true,"version":2}""";foreach (var payload in new[] { old, next }) { using var doc = JsonDocument.Parse(payload); var root = doc.RootElement; Console.WriteLine($"{root.GetProperty("id").GetString()}: {root.GetProperty("reserved").GetBoolean()}");}Expected result (not executed):
A: TrueA: TrueAdditive fields are compatible with this consumer only. A strict schema or changed meaning may reject them. Never remove or repurpose required fields without a migration plan.
Apply the concept
Write an ADR for inventory scaling. Compare the combined and separated versions when split overhead rises to 7 units; propose one measured review trigger and one contract test.
Use the manual practice and knowledge check. Do not deploy or publish these fixtures.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Microservice boundaries and cost — knowledge check
Apply the lesson concepts to the following scenarios.
What justifies a new microservice boundary?
Who should change inventory stock?
What does a local interface test not prove?
When is adding a response field compatible?
What does a gateway not replace?