Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A team introduces an in-process command bus: controllers send command objects, and the bus finds a handler by type. After a year, engineers say they cannot follow the code. Evaluate the mediator approach and suggest improvements.

An in-process command bus is a mediator: it decouples controllers from handlers and gives one place for cross-cutting behaviour such as logging and validation, but it hides the call graph, so IDE navigation from a controller to the code that runs stops working and failures surface far from their cause. For a single-process application with one handler per command, direct calls to application services are usually clearer. If the team keeps the bus, it should enforce naming that links each command to its handler, test that every command has exactly one handler, and move cross-cutting concerns into documented decorators or aspects.

Background to this question

Command buses became popular through CQRS articles and frameworks from other ecosystems, and they look elegant in a design review: controllers send a command object, a bus looks up the handler by the command's type, and nothing depends on anything concrete. A year later, the same team often reports that nobody can follow the code. This question tests whether we can evaluate a pattern by its effect on the people reading the code, not by its elegance. In this answer, we will recap what the bus does, see what it gives and what it takes away, decide when direct calls are better, and then look at the improvements that make a bus bearable if the team keeps it.

1A Quick Recap of the Design

The design looks like this. A command is a small object that describes an intention, such as approve this invoice. A handler is a class that performs it. The bus is a mediator that keeps a map from command class to handler and dispatches each command it receives.

Java
public record ApproveInvoice(long invoiceId, String approver) implements Command {}
@Component
class InvoiceApprover implements CommandHandler<ApproveInvoice> { // the handler
public Class<ApproveInvoice> commandType() { return ApproveInvoice.class; }
public void handle(ApproveInvoice c) { /* load, check rules, save */ }
}
@RestController
class InvoiceController {
private final CommandBus bus;
@PostMapping("/invoices/{id}/approve")
void approve(@PathVariable long id, Principal p) {
bus.send(new ApproveInvoice(id, p.getName())); // where does this go?
}
}

The controller depends only on CommandBus and the command record. That is the decoupling the team wanted. The bus can also wrap every dispatch with logging, validation, metrics and a transaction, so these concerns are written once instead of in every service method.

2Why Engineers Cannot Follow the Code

Now look at what an engineer does when debugging the approve endpoint. In a direct design, they would click on a method call in the IDE and land in the code that runs. Here, the call is bus.send, and clicking it lands in the generic bus. To find the handler, they must search for classes that implement CommandHandler<ApproveInvoice>, and that search only works if they know the convention. The call graph, the map of which code calls which, has been moved from the source code into a runtime lookup table.

Failures move too. If the handler throws, the stack trace runs through the bus's dispatch loop and three wrappers before reaching the controller. If no handler is registered, because someone renamed a command or forgot @Component, the failure appears only at runtime on the first request. And because the bus applies behaviour implicitly, such as starting a transaction or swallowing validation errors, engineers find effects in the logs that no code they read seems to explain. A year of features multiplies these costs by every command.

3When Direct Calls Are Clearer

The value of a bus comes from flexibility we may not use. It pays off when commands are sent to more than one place, cross process boundaries, are queued or retried, or when handlers are chosen dynamically. In a single-process application where each command has exactly one handler, none of that applies. A plain application service with one method per use case gives the same separation of controller and business logic, keeps IDE navigation and compile-time checking, and lets a missing implementation fail at compile time instead of at runtime.

Java
@Service
class InvoiceService {
@Transactional
public void approve(long invoiceId, String approver) { /* load, check rules, save */ }
}
@PostMapping("/invoices/{id}/approve")
void approve(@PathVariable long id, Principal p) {
invoices.approve(id, p.getName()); // ctrl-click goes straight to the code that runs
}
Command bus versus direct service calls
ConcernIn-process command busDirect application service
NavigationSearch by conventionIDE jumps to the method
Missing handlerFails at runtimeFails at compile time
Cross-cutting behaviourOne place in the bus, implicitAnnotations on each method, visible
Multiple handlers or asyncNatural fitNeeds events or a queue
Stack tracesThrough bus and wrappersController to service

Cross-cutting concerns still have a home: Spring's @Transactional, method validation and Micrometer's @Observed annotation are applied through proxies around the service, and they are visible on the method itself. So the honest evaluation is that, for this application, the bus adds indirection without adding capability.

4If We Keep the Bus: Make It Discoverable

Removing a bus that a whole codebase uses is itself a large migration, and sometimes the team has a good reason to keep it, for example because some commands are already sent asynchronously. Then the goal is to give back what the bus took away. The first step is a strict naming convention: command ApproveInvoice is handled by ApproveInvoiceHandler, in the same package. Engineers can then jump by name, and a reviewer can see at a glance whether a handler exists.

The second step is a test that turns the runtime failure into a build failure. If commands implement a sealed Command interface, Java lists every command type for us through getPermittedSubclasses, and the test can check that each one has exactly one handler bean.

Java
@SpringBootTest
class CommandHandlerCoverageTest {
@Autowired List<CommandHandler<?>> handlers;
@Test
void everyCommandHasExactlyOneHandler() {
Map<Class<?>, Long> counts = handlers.stream()
.collect(Collectors.groupingBy(h -> (Class<?>) h.commandType(), Collectors.counting()));
for (Class<?> command : Command.class.getPermittedSubclasses()) {
assertEquals(1L, counts.getOrDefault(command, 0L),
command.getSimpleName() + " needs exactly one handler");
}
}
}
// Fails with: "RejectInvoice needs exactly one handler ==> expected: <1> but was: <0>"

The bus itself should also fail fast at startup if two handlers claim the same command, instead of picking one silently.

5Make Cross-Cutting Behaviour Explicit

The last improvement is to stop hiding behaviour inside the bus loop. Each concern becomes a named decorator, a wrapper that implements the same interface and adds one behaviour, and the bus is assembled from them in one configuration class that documents the order: validation, then logging, then the transaction, then the handler. Engineers reading the configuration see exactly what runs around every command, and each decorator can be tested alone. The same effect can be had with Spring aspects, as long as they are listed and documented in one place rather than discovered by surprise. With discoverable handlers, a coverage test and explicit decorators, the bus keeps its benefits and stops being a mystery.

6Follow-up questions

Is this the same as CQRS?

No. CQRS, command query responsibility segregation, means using different models for writes and reads. A command bus is one way to dispatch writes, but CQRS works perfectly well with plain service methods, and a bus does not make a system CQRS. Teams sometimes adopt the bus believing it is required, which is one reason it appears in applications that gain little from it.

How would you migrate away from the bus without a big bang?

Introduce application services next to the handlers, starting with the commands that change most often. Each handler can first delegate to the new service method, then controllers switch to calling the service directly, and finally the handler is deleted. The coverage test keeps the remaining commands honest during the transition.

When is a bus clearly the right tool?

When commands genuinely travel: they are persisted and retried, sent to another process through a message broker, scheduled for later, or handled by plugins chosen at runtime. Then the indirection is the point, and investing in tooling, tracing and naming to keep it understandable is worth it.

7Conclusion

In this answer, we evaluated an in-process command bus as a mediator. It decouples controllers from handlers and centralises logging and validation, but it hides the call graph, breaks IDE navigation, moves missing-handler failures to runtime and makes stack traces long. For a single-process application with one handler per command, direct calls to application services are clearer. If the bus stays, a naming convention, a test that every command has exactly one handler, and documented decorators or aspects for cross-cutting behaviour give back most of what it took away.

What to say in the interviewAbout 90 seconds aloud

Open with

I would first acknowledge what the bus gives: controllers do not depend on handlers, and there is one place to apply logging, validation and transactions to every command.

Then, if they want more

But it is a mediator that moves the call graph from the code into a runtime lookup. When engineers click on bus.send they land in the bus, not in the code that runs, failures show up deep in the dispatch loop, and a missing handler is only found at runtime. That is exactly the complaint after a year.

For a single-process application with one handler per command, I would argue direct calls to application services are clearer: one method per use case, IDE navigation works, a missing implementation fails at compile time, and cross-cutting concerns are still applied through @Transactional, validation and observation annotations that are visible on the method.

A bus pays off when commands are queued, retried or sent across processes. If the team keeps it, I would make it discoverable: a naming rule so ApproveInvoice is handled by ApproveInvoiceHandler, a test that every command, for example every permitted subclass of a sealed Command interface, has exactly one handler, and startup failure for duplicates.

And I would move the cross-cutting behaviour into named decorators assembled in one documented configuration, so nobody is surprised by what runs around a command.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "A team introduces an in-process command bus: controllers send command objects, and the...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/6b15214fdc/