Backend Debugging Tasks
Debugging is the highest-leverage backend skill.
Introduction
Debugging is the highest-leverage backend skill. These TechLearningPRO tasks give you concrete failure scenarios against an Orders-like Boot 3 service: stack traces, slow traces, N+1, phantom rows, and heap growth.
Your method is always the same: reproduce → isolate → fix → add a regression test. Guessing from vibes is not a strategy.
Use Micrometer Tracing, Hibernate statistics, and heap dumps — tools you will need on-call.
Purpose of this lesson
Practice five production-style debugging drills on Orders with traces, SQL stats, and heap analysis.
Understanding the topic
Five mysteries — each has a measurable root cause.
- Mystery 500 — logs + stack trace → null deref in mapper after empty Optional.
- Slow endpoint — p95 doubled; trace shows Postgres wait + missing index.
- N+1 in production — Hibernate stats show 1+N selects on order lines.
- Phantom data — row appears via double outbox publish / missing idempotency.
- Memory leak — unbounded cache map in a
@Componentretains responses.
Step-by-step explanation
- Reproduce locally with the same headers/payload as the failing request.
- Capture trace id; open span waterfall; note the slowest child.
- Enable SQL logging or Hibernate statistics for the suspect path.
- Fix, write a regression test, and document a mini-postmortem.
Informative example
N+1 hunt — enable stats, then fix.
# temporary diagnosisspring.jpa.properties.hibernate.generate_statistics: truelogging.level.org.hibernate.stat: DEBUG// Before: findById then lines.forEach → N selects// After:public interface OrderRepository extends JpaRepository<OrderEntity, Long> {@EntityGraph(attributePaths = "lines")@Query("select o from OrderEntity o where o.id = :id")Optional<OrderEntity> findDetailById(@Param("id") Long id);}@Testvoid orderDetail_runsConstantQueries() {// seed 20 lines; assert query count ≤ 2 via StatementInspector}
Real-world use
TechLearningPRO on-call once chased 'random 500s' for hours until they correlated a single trace id: a mapper called optional.get() after a soft-deleted order. The fix was one line; the lasting value was the regression test and the runbook step 'always grab trace id first'.
Security implications
Debug artefacts can leak data.
- Redact tokens and PII from shared stack traces.
- Heap dumps stay in locked storage — never email them.
- Do not disable auth in prod 'to reproduce faster'.
Best practices
- Keep a debug checklist in the team wiki.
- Prefer continuous profiling in staging.
- Share postmortems without blame.
Common mistakes
- Restarting pods before capturing heap/trace evidence.
- Fixing symptoms (timeouts ↑) without finding the root query.
- Shipping a fix without a test that fails on the old code.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat is your first move on a production 500?+
Answer
2IntermediateQuestionHow do you confirm an N+1?+
Answer
3BeginnerQuestionWhy write a regression test after a debug fix?+
Answer
Hands-on exercise
Debug drills (timebox 25 min each).
1) Mystery 500: given a stack trace ending in OrderMapper.from, find the null and patch + test.
2) Slow GET: EXPLAIN a list-by-customer query; add the index; show before/after ms.
3) N+1: turn on stats, fix with @EntityGraph, assert query budget.
4) Phantom order: simulate double webhook; enforce idempotency unique key.
5) Leak: find the static/unbounded map; replace with Caffeine/Redis TTL; confirm heap stabilises.
Submit: notes with commands used + PR link.
Summary
Debugging drills prepare you for on-call. Next: database challenges for SQL seniority.