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

    Performance Engineering

    Performance engineering is a loop: define SLOs, measure under realistic load, fix the hottest path, remeasure.

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

    Introduction

    Performance engineering is a loop: define SLOs, measure under realistic load, fix the hottest path, remeasure. For TechLearningPRO Orders on Boot 3 / Java 21, tools include k6/Gatling for load, JFR / async-profiler for production-shaped profiles, and JMH only for true micro-benchmarks.

    Without SLOs (for example p95 under 200ms and error rate under 0.1%), teams over-optimise vanity paths. Laptop datasets lie — stage with production-like cardinality and payload sizes.

    Virtual threads are a step-change for I/O-bound Boot web apps on Java 21, but they do not fix N+1 SQL or an undersized Hikari pool. Measure first.

    This lesson installs the discipline that turns monitoring data into durable latency wins.

    Purpose of this lesson

    Run an SLO-driven performance loop on Orders with load tests, profiling, and Java 21-aware fixes.

    Understanding the topic

    SLOs make performance measurable; tools answer where time goes.

    • SLOs: latency and error budgets before tuning.
    • Load: k6/Gatling against prod-shaped staging.
    • Profile: JFR/async-profiler under real traffic patterns.
    • Micro: JMH only for isolated hot methods — not whole HTTP stacks.
    • Java 21: virtual threads for blocking I/O concurrency when the stack is ready.

    Step-by-step explanation

    1. Write SLOs for checkout (p95/p99 and error rate).
    2. Load-test staging with realistic SKUs, users, and payload sizes.
    3. Profile the slow window; fix top SQL, pools, or I/O — not random JVM flags.
    4. Enable virtual threads if I/O-bound and remeasure against the same SLO.

    Informative example

    SLO knobs + virtual threads for Orders (Boot 3 / Java 21).

    javascript
    # application.yml
    spring:
    threads:
    virtual:
    enabled: true
    datasource:
    hikari:
    maximum-pool-size: 20
    connection-timeout: 3000
    jpa:
    open-in-view: false
    # k6 sketch — checkout SLO probe
    import http from 'k6/http';
    import { check } from 'k6';
    export const options = {
    thresholds: {
    http_req_duration: ['p(95)<200'],
    http_req_failed: ['rate<0.001'],
    },
    };
    export default function () {
    const res = http.post('https://orders.example/api/orders', payload, params);
    check(res, { 'status 201': (r) => r.status === 201 });
    }

    Real-world use

    TechLearningPRO spent a week tuning GC flags while checkout p99 was dominated by missing indexes. After an async-profiler session under k6 load, one composite index and disabling open-in-view met the SLO — GC flags never shipped.

    Security implications

    Perf work must not weaken auth or leak data via caches.

    • Do not disable security filters for speed.
    • Cache keys for private data must include tenant/principal.
    • Keep profiler outputs free of secret material when sharing dumps.

    Best practices

    • Define SLOs before changing code.
    • Load-test in a prod-shaped staging environment.
    • Prefer fixing SQL and pools over micro-benchmark theatre.

    Common mistakes

    • Optimising without an SLO or baseline.
    • Profiling on empty local databases.
    • Adopting virtual threads while blocking the carrier with synchronized pin-heavy libraries without measuring.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhy define SLOs before optimising?+

    Answer

    They tell you when you are done and prevent endless micro-optimisation of non-bottlenecks.
    2IntermediateQuestionWhen should you use JMH vs k6?+

    Answer

    JMH for isolated method micro-benchmarks; k6/Gatling for end-to-end HTTP load and SLO validation.
    3IntermediateQuestionHow do virtual threads help Spring Boot web apps?+

    Answer

    They make blocking I/O concurrency cheaper on Java 21, improving throughput for call-heavy services when libraries are virtual-thread friendly.

    Hands-on exercise

    Lab. Set a p95 SLO for POST /api/orders. Run a short k6 test, capture a JFR or async-profiler snapshot under load, fix the top frame or SQL, and show the delta against the same threshold.

    Summary

    Performance engineering keeps Orders honest against SLOs. Next: structure the codebase with Clean Architecture so optimisations stay testable.

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