Circular dependencies 00:00:00 Make a prediction You will trace an import cycle and break it by moving an operation back to the owner that should make the decision. Order service calls billing to charge. Billing calls order service to mark the order paid. If billing instead returns the result, which import can disappear? Billing no longer needs to know the order service. The order service consumes the result and owns its own state transition. Redraw both source dependencies after the change. 00:00:29 The keys of Sam and Asha Sam keeps the key to Asha's shed, and Asha keeps the key to Sam's shed. If both keys are stored inside the locked sheds, neither can begin. Move the shared access point outside the cycle. Depending on each other's startup or internal decisions can create a similar loop. Moving the decision to one owner or extracting a neutral contract can remove a real arrow. A dependency is an arrow: A needs B to compile, to be created or to do its work. A circular dependency is a path of arrows that comes back to where it started: A to B to A, or A to B to C to A. Start-up. If A must exist before B and B before A, neither can be first. You get a container error, a half loaded module, a null field or an endless recursion. 00:01:24 Break a billing loop The order service imports billing and billing imports the order service just to mark an order paid. Neither is easy to construct or test alone. Let the order service call a payment port and consume the returned result. Billing only knows the charge request and result. The order service itself transitions the order after success. Draw the imports again: the implementation depends on the port and no import leads back into the order service. Have an unpaid order request a charge. The payment component returns declined, and the order stays unpaid. If payment calls back into an order operation that requests payment again, the trace loops. A result value lets the original owner finish one transition. 00:02:12 When one module is enough Two classes constantly update the same invariant and only ever operate together. Hiding a cycle behind an event bus adds timing problems. If there is a single coherent responsibility, merge the boundary and keep internal collaboration private. Imagine two components that always change, deploy and transact together. Replacing a direct cycle with two asynchronous callbacks adds timing choices without buying independence. Put the shared rule under one owner until there is a real boundary worth crossing. Publishing events in both directions can preserve the same behavioural cycle even after import statements disappear. Trace how many times one change bounces back. If two classes protect a single inseparable invariant, merging the boundary may be clearer than hiding the dependence behind a message bus. Spring Boot. Since 2.6 circular references between beans are prohibited by default. The release notes advise updating the application to break the cycle, with a property as a temporary way back. Spring Framework. The reference explains that constructor injection can create an unresolvable ring, detected at run time as BeanCurrentlyInCreationException, and that setter injection can configure it but is not recommended. 00:03:37 Try an interview follow-up Two teams own services that call each other synchronously. Comments? It is a call cycle across processes: a slow A fills B's threads, which are waiting for A, and deployments need an order that does not exist. I would ask what data each needs, move ownership or copy it by events, and allow synchronous calls in one direction only. The order needs a promotion to compute a discount. The promotion calls back into the order total, which calls the promotion again. Write down the inputs the rule actually needs, such as subtotal and customer tier. Pass a snapshot of those inputs and trace the calculation to a result without re-entry. 00:04:24 What to take with you Name the kind of cycle: module, construction or call. Interfaces cure the first, extraction cures all three, events turn a direct call into a notice. Usually a cycle hides a third thing that both sides need, or a method that lives in the wrong class. Move it, and one arrow disappears or turns around.