Circular Dependency Detection and Resolution in Production Services
Hidden dependency loops fail during recovery, so catch them with static rules and runtime tracing.
Sponsored by Graphide

Finding and fixing circular dependencies in production services takes more than finding loops in source code. The dangerous cycles often hide in the links across service boundaries, in configuration, and in recovery paths. Static enforcement must be combined with runtime visibility, then architectural boundaries used to prevent the same coupling from returning.
Why circular dependencies in production services are a visibility problem, not a complexity problem
A service can work normally until a change exposes a dependency loop that has existed for months or years. Under low load, synchronous calls can complete before resources are exhausted, so successful requests provide little evidence that the service graph is safe. The system feels healthy because the triggering conditions are absent. That is the central visibility trap. Production traffic puts the system to work, but it may not reveal every link that recovery depends on.
Service A calls B, B calls C, and C calls A. Each connection can look reasonable within the service that introduces it, while the combined graph contains a risk that no individual team sees. When ownership follows service boundaries, the integration surface can have no explicit owner. Review then approves local decisions without examining the structure they create together.
Runtime conditions make that structure harder to see. A premium feature flag might send a request to a different endpoint, closing a loop that ordinary requests never traverse. The relevant dependency is conditional, rather than an import that a source-code tool can follow. A 2025 arXiv study of the PyPI ecosystem found more than a million circular dependency instances, with cycles spanning deeper dependency chains on average than non-circular packages. That finding establishes their prevalence in package graphs, while routing and configuration make dependencies in production services harder to see.
How Discord's March 2026 voice outage shows what a hidden cycle actually does under stress
Discord’s March 25, 2026 voice outage began with a configuration update that changed pod resources for the session management service. It was not a new integration or feature. Kubernetes terminated pods in one availability zone, where a safety check waited for in-progress events before handoff. The check ran longer than the termination grace period, so the pods were destroyed ungracefully. The resulting loss of 17% of active sessions globally cascaded into widespread voice call failures.
The hidden coupling became decisive during recovery. Voice syncers announce their health to etcd on a 60-second TTL, but jammed supervisors prevented the heartbeats from getting through. When the time on those announcements ran out, instances dropped out of the consistent hash ring. Routing membership therefore depended on a health mechanism that could not progress under the failure conditions. Available redundancy could not function as independent capacity.
Restarting instances did not break that relationship. Instance 2-8, for example, briefly recovered, re-announced itself in etcd, and began receiving routed traffic. Pent-up traffic then overwhelmed it again. Meanwhile, the sole healthy instance could handle only a small fraction of global syncer traffic.
The safeguards were designed for independent failures. The cycle tied health announcements, routing, and recovery to components already under stress, so failures could no longer stay independent. That coupling had survived testing, code review, deployment gates, and more than a year of production traffic before the configuration change and load exposed it. GitHub encountered a related recovery problem when deployment and remediation tooling depended on infrastructure it was supposed to repair. After its outage, Discord broke the dependency loop, improved isolation between core voice components, strengthened validation, and enhanced observability to detect hidden coupling before it escalated.
Why static analysis and architecture diagrams miss the cycles that matter most
Import-graph tools can find source-level cycles, but they cannot show the full service graph that feature flags, configuration, and request routing create. A path that closes only for selected requests may have no equivalent cycle in the import graph. This is a structural gap.
Architecture diagrams have a different limitation. They represent intended relationships at a point in time, rather than continuously recording the calls a running system makes. Code review is closer to the implementation, but a reviewer examining Service A’s change may not see how B and C are connected elsewhere. As teams scale, that missing context becomes an ownership problem as well as a tooling problem.
Tools such as madge, dpdm, and ESLint plugins can expose cycles yet leave engineers with a wall of file paths rather than an explanation of the architectural pattern behind them. SentinelQuantumVanguardAiPro PR #242, merged September 5, 2026, dealt with initialization-order hazards that can arise even when every imported file and symbol exists.
Detection methods that can actually catch cycles before they reach production
The first layer makes source-level relationships testable in CI. ArchUnit for Java and dependency-cruiser for JavaScript and TypeScript let teams express forbidden dependencies as executable rules. A review convention that package A may not import package B becomes a gate. The rommapp project initially proposed dpdm, then adopted ESLint’s import-x/no-cycle rule before merging. Integrating detection into validation engineers already watch makes dependency violations part of the normal development workflow.
Useful diagnostics also require a stable graph. PR #242 built a deterministic graph of production JavaScript imports and re-exports, leaving out test/spec files and generated/dependency directories. It canonicalized and deduplicated cycles so repeated findings produced stable diagnostics, then placed the check after module interface validation. This catches a distinct failure class. Circular-dependency reports should likewise remain separate from other boundary violations so cycles do not disappear into generic build noise.
OpenTelemetry propagates a shared trace ID and per-hop span IDs through downstream service calls, allowing trace analysis to reveal when a request path returns to an earlier service. That shows feature-flag-gated paths, dynamic routing, and configuration-driven wiring when requests use them in staging or canary environments. Static analysis cannot supply that evidence. Tracing complements the import graph by showing how the deployed components interact.
Kernel-level and service-mesh instrumentation extend coverage beneath application code. eBPF-based tooling can detect circular calls without changes to the services themselves, and GitHub used eBPF controls to prevent deployment tooling from depending on services that might be degraded. Service meshes such as Istio can flag unusual call patterns, which tracing can help interpret. No single layer covers every dependency surface. Static enforcement and runtime tracing together cover source relationships and exercised service paths, while infrastructure instrumentation exposes coupling outside the application’s own view.
Why AI coding agents make the cycle problem structurally worse
The way teams worked at Discord poses a more urgent risk when agents speed up changes within each service. Agents can scaffold infrastructure, select frameworks, and wire integrations without visibility into the complete dependency graph. Those are architectural decisions even when they arrive as implementation work. Without enforcement, an agent can optimize a service while adding risk to the integration surface around it.
Localized refactoring reinforces that mismatch. Variable renaming and type updates operate within the context available to the agent, while resolving cross-service coupling requires a view of relationships beyond the immediate change. Agents can also add unrequested features or introduce and remove dependencies without warning. A feature request can consequently become a broader refactoring task. The graph may then change in more places than the reviewer expected to check.
Multi-file edits increase that review burden. Engineers must check linked changes across a repository instead of a single diff, and they may still never see the full runtime graph during review. Local optimization can proceed without anyone owning the accumulated dependencies, reproducing the conditions in which hidden coupling survives. The detection layers described above therefore need to operate as routine constraints on every change.
Enforcing dependency constraints in CI so architectural rules become executable, not aspirational
ARCHITECTURE.md and architecture decision records describe where boundaries should be, but those words alone cannot stop a merge that breaks the rules. The practical response is to encode those boundaries in CI. This practice, called harness engineering, treats constraint enforcement as a discipline in its own right. The harness goes beyond asking an agent to write good code by testing whether the code it writes follows the structural rules. The same gate applies to human-authored changes.
Dependency layering gives those rules a concrete form. Where layers are ordered, each layer may import only from layers to its left. A packages/contracts layer cannot import an app, and an app cannot reach into another app’s internals. Structural tests enforce those restrictions on every change.
The implementation depends on the language ecosystem. ArchUnit expresses allowed and forbidden dependency relationships as JUnit tests, while dependency-cruiser uses rule files to constrain JavaScript and TypeScript import paths. For Go, depguard in golangci-lint enforces package import rules through a linter gate. These tools constrain code-level dependencies. OpenTelemetry trace analysis complements them across languages by surfacing unexpected call paths in staging or canary environments.
Enforcement also needs to explain what failed. A circular dependency should be reported distinctly from an ordinary boundary violation because breaking a loop and correcting a forbidden import may require different architectural work. Stable, targeted diagnostics make the rejected relationship clear. However, a passing static gate is not evidence that configuration cannot create a runtime cycle. CI establishes the permitted source graph, while runtime validation tests the interactions that graph alone cannot describe.
Architectural patterns that eliminate the conditions for cycles rather than just detecting them
Shared types and interface extraction provide a direct way to remove dependencies introduced only to access a common contract. If apps need the same types, those definitions can sit in packages/contracts rather than inside an app that another app must import. Both apps then depend on the shared layer. The direction stays one-way because the contracts package cannot import either app. Enforcing that restriction in CI prevents the shared layer from becoming another route back into application code.
Dependency inversion addresses the related case in which a caller depends on a concrete implementation. When A calls B and B calls A, moving the relevant interface into a contracts layer can break the direct source-level loop. Both sides depend on the abstraction instead. The boundary must remain enforceable, or implementation imports can recreate the relationship that extraction removed. This changes the code’s dependency structure, but runtime tracing is still needed to check whether service calls form a loop.
Event-driven decoupling addresses synchronous coupling between logically independent services. Order Service can publish OrderCreated, Inventory Service can subscribe and publish InventoryReserved, and Pricing Service can subscribe to that event. The services use a shared bus in place of direct synchronous calls to each other. That removes the direct call loop illustrated by the example, rather than making one participant wait for another to call back. The tradeoff is eventual consistency and added operational complexity, so this pattern has a specific role in addressing synchronous coupling.
These patterns need a maintained record of the relationships they permit. Under agent-speed development, the dependency graph must be versioned with the code, visible in real time, and enforceable in CI. Otherwise, later changes can undo a sound design without anyone seeing what has changed. Treating the graph as a living artifact makes hidden coupling inspectable before recovery.


