Volume 7 · Chapter 9 — Advanced .NET Engineering, Performance & Production Excellence
Production Troubleshooting
9.1 Introduction
Production troubleshooting is applied observability under pressure. The distinguishing mark of a senior engineer here is discipline: forming hypotheses from evidence rather than changing things at random, stabilizing before deeply diagnosing, and always finding the root cause so the incident does not recur. This chapter covers a systematic method and the specific techniques for the failure classes you will actually meet in .NET production systems.
9.2 The Incident Method: Stabilize, Then Diagnose
During a live incident the first priority is to stop the bleeding for customers, even with a temporary measure (roll back, scale out, fail over, disable a feature),thendiagnose the root cause with the pressure off. Reversing this order - deep-diving while customers suffer - is a common and costly mistake.
flowchart TB D["Detect (alert/report)"] --> A["Assess impact & severity"] A --> S["Stabilize: rollback / failover / scale / feature-flag off"] S --> I["Investigate with evidence (metrics -> traces -> logs -> dumps)"] I --> R["Root cause"] R --> F["Permanent fix + regression guard"] F --> P["Blameless postmortem"]
9.3 Evidence-Driven Investigation
Follow the observability funnel from cheap and broad to detailed and specific: metrics to localize the affected service and symptom, traces to find the slow or failing span, logs (by trace id) to explain it, and only then a dump or profile for runtime-level detail. This ordering avoids the trap of jumping straight to a debug build of one component.
9.4 Diagnosing Memory Problems
Rising memory and GC pressure present as slowdowns, then out-of-memory restarts. Confirm with counters (heap size not plateauing, Gen 2 climbing), then capture adotnet-gcdumpand follow retention paths, as in Chapter 2. Two dumps spaced apart, diffed, reveal exactly which object types are growing.
9.5 Diagnosing High CPU
Sustained high CPU means either legitimate load, an inefficient hot path, or a pathological loop. Capture a CPU trace on a hot instance and read the flame graph for the dominant stack.
// Collect a CPU trace from a live process, then open in speedscope/PerfView.
// dotnet-trace collect --process-id 1234 --profile cpu-sampling --duration 00:00:30
// The top stacks reveal whether it's JSON serialization, regex, a tight loop, etc.
9.6 Thread-Pool Starvation
As covered in Chapter 3, low CPU combined with growing request queues and stalling throughput is the signature of thread-pool starvation from sync-over-async. Confirm withdotnet-counters(thread pool queue length rising, thread count pinned) and a dump showing many threads blocked inMonitor.Waitor synchronous I/O; the fix is to make the offending path async end to end.
9.7 Database and Deadlock Issues
The database is the usual latency culprit. Investigate slow queries (execution plans, missing indexes), connection-pool exhaustion (too few connections, or connections leaked by undisposed contexts), and deadlocks (two transactions each holding a lock the other needs). EF Core query logging and the database’s own deadlock graph are the primary evidence.
9.8 A Latency Incident, End to End
sequenceDiagram autonumber participant On as On-call participant M as Metrics participant T as Traces participant L as Logs participant DB as DB Plans On->>M: SLO burn alert - p99 up 5x on /transfer M-->>On: errors flat, latency high -> not a crash, a slowdown On->>T: open slow /transfer trace T-->>On: 90% of time in Ledger DB span On->>L: logs for that trace id L-->>On: "query timeout after 30s" on statement view On->>DB: check execution plan DB-->>On: full scan - index dropped by a migration On->>On: stabilize (add index online) then postmortem
9.9 Safe Live Debugging
Diagnosing production must not harm customers. Prefer out-of-process, low-overhead tools (counters, sampled traces, dumps) over attaching a debugger, which can freeze the process. Capture artifacts from one instance (or a drained instance) rather than the whole fleet, and analyze offline.
How do you approach a production outage?
Stabilize first, diagnose second. My immediate goal is to reduce customer impact with the fastest safe lever - roll back the recent deploy, fail over, scale out, or disable the offending feature via a flag - while communicating status to stakeholders. Only once the bleeding is stopped do I investigate the root cause methodically, following evidence from metrics to traces to logs rather than guessing. I keep a timeline, avoid making multiple simultaneous changes that obscure cause and effect, and once resolved I run a blameless postmortem that produces concrete preventions. The instinct to fully understand before acting is the wrong order during an outage - customers come first, understanding follows.
A service is slow but CPU and memory look fine. Where do you look?
Healthy CPU and memory with poor latency points away from local compute and toward waiting - so I look at what the service is waiting on. Distributed traces usually show the time sitting in a downstream call or a database query. Common causes are slow or unindexed queries, connection-pool exhaustion, thread-pool starvation from sync-over-async (low CPU, growing request queue), lock contention, or a degraded dependency. I confirm withdotnet-countersfor thread-pool queue length and lock contention, and with traces for the slow span. The tell is that the bottleneck is a wait, not work, so I chase queues, locks, and downstream latency rather than optimizing CPU-bound code.
Which tools do you use to diagnose a live .NET process, and for what?
The .NET diagnostics suite, chosen by symptom.dotnet-countersfor a live, low-overhead view of GC, thread pool, exceptions, and lock contention to localize the problem.dotnet-tracefor a CPU or event timeline to find hot stacks and allocation sources.dotnet-gcdumpfor heap/retention analysis of memory growth.dotnet-dumpfor a full process dump to inspect threads and stacks (for example to confirm thread-pool starvation). All are out-of-process and work in containers via the diagnostic socket, so they do not require a debugger and are safe(r) on production. I generally go counters first to narrow, then a trace or dump to confirm.
How do you prevent the same production incident from recurring?
A fix that only addresses the immediate symptom guarantees a repeat, so I treat the root cause and the systemic gaps that let it reach production. The postmortem (blameless, focused on the system) identifies not just the technical cause but why it was not caught: missing test, missing alert, a risky manual step, an insufficient review. Each becomes a concrete action item with an owner - a regression test or CI guard so the exact failure cannot ship again, an alert so the next occurrence is detected earlier, automation to remove the manual error, or a design change to remove the failure mode entirely. I track those to completion, because postmortems whose actions are never done are theatre. Over time this turns each incident into a permanent improvement in the system’s resilience.
How do you lead a high-severity incident with multiple engineers involved?
Clear roles and communication prevent chaos. I establish an incident commander (often me) who coordinates rather than debugs, so the investigation has direction and no one duplicates work; a scribe keeps a timeline; and specific engineers own specific hypotheses. I keep a single source of truth (an incident channel) and give stakeholders regular, honest status updates so leadership is not interrupting responders. I enforce change discipline - one deliberate change at a time, with the expected effect stated first - so we can tell what actually helped. I bias to stabilizing customer impact quickly even with a temporary measure, and I protect the team from panic and blame. After resolution I drive the blameless postmortem. The commander’s job is to keep the response calm, coordinated, and evidence-driven, which matters more during a Sev-1 than any single person’s debugging skill.
9.10 Banking Enterprise Scenario
A bank’s transfer endpoint suddenly shows p99 latency five times normal, with error rates flat - a slowdown, not a crash. The on-call engineer follows the funnel: metrics localize it to transfers, a trace shows 90% of the time in the ledger’s database span, and logs for that trace id reveal query timeouts. The execution plan shows a full table scan because a migration accidentally dropped an index. The engineer stabilizes by rebuilding the index online, restoring latency, then the postmortem adds a migration review check and a query-duration alert so the failure is both prevented and detected earlier next time.
Chapter Revision Notes
- Stabilize customer impact first; diagnose root cause second.
- Investigate with evidence: metrics to traces to logs to dumps.
- Slow-but-healthy CPU/memory means waiting - chase queues, locks, and DB.
- Use out-of-process tools (counters, trace, gcdump, dump) safely in production.
- Every incident ends in a root cause and a regression guard, via a blameless postmortem.