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

    Scaling Backend Systems

    Scaling is not 'more pods' — it is removing the current bottleneck.

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

    Introduction

    Scaling is not 'more pods' — it is removing the current bottleneck. For TechLearningPRO Orders on Boot 3 / Java 21, the usual order is: fix the database and queries, add caching, then horizontally scale stateless replicas. Vertical scale (bigger box) is often the cheapest first win.

    Stateless Orders pods behind a load balancer scale cleanly when sessions, uploads, and locks live in Redis/Postgres/S3 — not in the JVM heap. Sticky sessions and local caches that must be coherent across replicas fight horizontal scale.

    Read replicas, sharding, async queues, and CDNs appear later in the playbook. Sharding is last resort: operationally expensive and hard to reverse.

    This lesson gives Orders a disciplined scaling sequence before environment and security hardening.

    Purpose of this lesson

    Apply a constraint-first scaling playbook to Orders: vertical, cache, horizontal, then advanced data patterns.

    Understanding the topic

    Find the constraint; fix it; remeasure — do not shotgun.

    • Vertical first: larger instance / faster Postgres when CPU or single-node I/O is the limit.
    • Horizontal: N stateless Boot replicas behind a load balancer.
    • Cache: Redis between API and DB for hot catalog/reads.
    • Read replicas: offload reporting and heavy reads.
    • Async + CDN: queues for slow work; edge cache for cacheable GETs.
    • Sharding: last resort when one primary cannot keep up.

    Step-by-step explanation

    1. Baseline p99 and DB wait under load; identify the top constraint.
    2. Fix SQL/indexes or raise vertical capacity if that is cheaper.
    3. Externalise session/state; add Redis for hot reads; deploy N replicas.
    4. Only then evaluate read replicas, queues, or partitioning by tenant.

    Informative example

    Stateless Orders ready for horizontal scale (Boot 3).

    yaml
    # application.yml — no sticky session requirement
    spring:
    data:
    redis:
    host: ${REDIS_HOST}
    port: 6379
    session:
    store-type: redis
    threads:
    virtual:
    enabled: true
    # Kubernetes sketch: scale replicas, shared Redis/Postgres
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: orders
    spec:
    replicas: 3
    template:
    spec:
    containers:
    - name: orders
    image: techlearningpro/orders:3.2
    env:
    - name: SPRING_DATA_REDIS_HOST
    value: redis
    - name: SPRING_DATASOURCE_URL
    value: ${SPRING_DATASOURCE_URL}

    Real-world use

    TechLearningPRO doubled replica count during a sale and saw almost no gain — Postgres CPU was saturated on unindexed customer_id lookups. One index plus Redis for SKU reads cut p99 in half; only then did adding replicas help absorb traffic.

    Security implications

    Scale multiplies attack surface and credential blast radius.

    • Same least-privilege DB role on every replica — no shared superuser.
    • Do not embed connection passwords in images; inject via env/secret refs.
    • Rate limits and auth must be shared-state aware under horizontal scale.

    Best practices

    • Keep Orders stateless; externalise state early.
    • Measure after every change; stop when SLOs are met.
    • Prefer async for email, webhooks, and heavy reports.

    Common mistakes

    • Scaling pods while the DB is the bottleneck.
    • In-memory rate limits or caches that disagree across replicas.
    • Sharding early and living with cross-shard joins forever.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhat should you scale first in a typical Spring Boot API?+

    Answer

    Usually the database and queries; then cache; then horizontal replicas of a stateless app.
    2IntermediateQuestionWhy are sticky sessions a tax on scaling?+

    Answer

    They couple clients to pods, complicate failover, and prevent true load spreading.
    3AdvancedQuestionWhen is sharding justified?+

    Answer

    When a single primary cannot meet write/storage needs after vertical scale, caching, and partitioning alternatives — and the team can own the ops cost.

    Hands-on exercise

    Lab. Load-test POST /api/orders with one replica, then three, with and without a Redis SKU cache. Record which change moved p99 and which only moved cost.

    Summary

    Scaling Orders is a playbook, not a panic button. Next: keep config and secrets out of the image with environment variables.

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