Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 103

    Backend Debugging Tasks

    Debugging is the highest-leverage backend skill.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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 @Component retains responses.

    Step-by-step explanation

    1. Reproduce locally with the same headers/payload as the failing request.
    2. Capture trace id; open span waterfall; note the slowest child.
    3. Enable SQL logging or Hibernate statistics for the suspect path.
    4. Fix, write a regression test, and document a mini-postmortem.

    Informative example

    N+1 hunt — enable stats, then fix.

    java
    # temporary diagnosis
    spring.jpa.properties.hibernate.generate_statistics: true
    logging.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);
    }
    @Test
    void 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.

    3 questions
    1BeginnerQuestionWhat is your first move on a production 500?+

    Answer

    Grab trace/correlation id and the exact stack, reproduce with the same inputs, then bisect — do not restart blindly.
    2IntermediateQuestionHow do you confirm an N+1?+

    Answer

    Hibernate statistics or a StatementInspector showing query count growing with collection size.
    3BeginnerQuestionWhy write a regression test after a debug fix?+

    Answer

    So the same failure cannot return unnoticed; it documents the root cause for the team.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.