Scalable Backend Systems
Scalable systems rest on a few invariants: stateless services, externalised state, async boundaries, caching, backpressure, idempotency, and deliberate partitioning.
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
- Remove in-memory session or job state from Orders pods.
- Put hot reads behind Redis with TTL; keep writes authoritative in Postgres.
- Move email/webhooks to a queue consumer with idempotent handlers.
- Set timeouts and Resilience4j limits on every outbound client; budget latency per hop.
Informative example
Scalable Orders wiring sketch.
# application.ymlspring:data:redis:host: ${REDIS_HOST}kafka:bootstrap-servers: ${KAFKA_BOOTSTRAP}threads:virtual:enabled: true# Outbound Inventory client — timeouts + idempotent key headerspring:cloud:openfeign:client:config:inventory:connectTimeout: 200readTimeout: 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.
1BeginnerQuestionWhy must services be stateless to scale horizontally?+
Answer
2IntermediateQuestionWhat is backpressure?+
Answer
3IntermediateQuestionWhy is idempotency mandatory at scale?+
Answer
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.