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

    Scalable Backend Systems

    Scalable systems rest on a few invariants: stateless services, externalised state, async boundaries, caching, backpressure, idempotency, and deliberate partitioning.

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

    Introduction

    Scalable systems rest on a few invariants: stateless services, externalised state, async boundaries, caching, backpressure, idempotency, and deliberate partitioning. TechLearningPRO Orders scales when any replica can serve any request and Postgres/Redis/Kafka/S3 hold the durable state.

    Latency budgets per hop must sum under the SLO. Retries without idempotency and unbounded queues turn incidents into stampedes. Boot 3 / Java 21 helps with virtual threads and Micrometer — they do not replace these invariants.

    Design for failure: timeouts, circuit breakers, and bounded queues protect Inventory and Payments from Orders' own retries.

    This lesson ties scaling tactics into a system-level checklist before high availability.

    Purpose of this lesson

    Build Orders on scalable invariants: statelessness, external state, async, cache, backpressure, and idempotency.

    Understanding the topic

    Invariants that make horizontal scale possible.

    • Stateless services: any replica serves any request.
    • Externalise state: DB, Redis, object storage, Kafka.
    • Async boundaries: queues for slow or unreliable work.
    • Caching layers: CDN → Redis → DB.
    • Backpressure: bounded queues, timeouts, circuit breakers.
    • Idempotency: end to end — retries are inevitable.
    • Partitioning: by tenant, region, or hash key when needed.

    Step-by-step explanation

    1. Remove in-memory session or job state from Orders pods.
    2. Put hot reads behind Redis with TTL; keep writes authoritative in Postgres.
    3. Move email/webhooks to a queue consumer with idempotent handlers.
    4. Set timeouts and Resilience4j limits on every outbound client; budget latency per hop.

    Informative example

    Scalable Orders wiring sketch.

    yaml
    # application.yml
    spring:
    data:
    redis:
    host: ${REDIS_HOST}
    kafka:
    bootstrap-servers: ${KAFKA_BOOTSTRAP}
    threads:
    virtual:
    enabled: true
    # Outbound Inventory client — timeouts + idempotent key header
    spring:
    cloud:
    openfeign:
    client:
    config:
    inventory:
    connectTimeout: 200
    readTimeout: 500
    // Consumer must be idempotent
    @KafkaListener(topics = "orders.placed")
    public void onPlaced(OrderPlacedEvent event) {
    fulfillment.handleOnce(event.eventId(), event);
    }

    Real-world use

    During a flash sale, Orders retries overwhelmed Inventory because timeouts were five seconds and queues were unbounded. TechLearningPRO cut client timeouts, added a bulkhead, and made reservation handlers idempotent — throughput recovered without adding database shards.

    Security implications

    Scale amplifies abuse and credential reuse.

    • Shared rate limits and auth across replicas.
    • Tenant partitioning must not leak cross-tenant cache keys.
    • Queue payloads stay free of raw secrets — reference ids only.

    Best practices

    • Stateless + externalised state as the default architecture.
    • Backpressure and idempotency before more replicas.
    • Partition only with a clear key and operational plan.

    Common mistakes

    • Sticky sessions and local-only caches pretending to be a cluster.
    • Unlimited retries without jitter or idempotency keys.
    • Ignoring hop latency budgets until the SLO is already breached.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhy must services be stateless to scale horizontally?+

    Answer

    So any replica can handle any request and load balancers can shed failed pods without sticky affinity.
    2IntermediateQuestionWhat is backpressure?+

    Answer

    Limiting accepted work (bounded queues, timeouts, load shedding) so overload does not cascade.
    3IntermediateQuestionWhy is idempotency mandatory at scale?+

    Answer

    Retries, at-least-once delivery, and duplicate clients are normal; without it you double-charge or double-fulfill.

    Hands-on exercise

    Lab. Make Orders checkout idempotent under retry, move confirmation email to a Kafka/consumer path, and verify two pods with shared Redis behave identically for a hot SKU read.

    Summary

    Scalable invariants keep Orders healthy under growth. Next: high availability concepts so failures stay invisible to learners.

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