Scaling Backend Systems
Scaling is not 'more pods' — it is removing the current bottleneck.
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
- Baseline p99 and DB wait under load; identify the top constraint.
- Fix SQL/indexes or raise vertical capacity if that is cheaper.
- Externalise session/state; add Redis for hot reads; deploy N replicas.
- Only then evaluate read replicas, queues, or partitioning by tenant.
Informative example
Stateless Orders ready for horizontal scale (Boot 3).
# application.yml — no sticky session requirementspring:data:redis:host: ${REDIS_HOST}port: 6379session:store-type: redisthreads:virtual:enabled: true# Kubernetes sketch: scale replicas, shared Redis/PostgresapiVersion: apps/v1kind: Deploymentmetadata:name: ordersspec:replicas: 3template:spec:containers:- name: ordersimage: techlearningpro/orders:3.2env:- name: SPRING_DATA_REDIS_HOSTvalue: redis- name: SPRING_DATASOURCE_URLvalue: ${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.
1BeginnerQuestionWhat should you scale first in a typical Spring Boot API?+
Answer
2IntermediateQuestionWhy are sticky sessions a tax on scaling?+
Answer
3AdvancedQuestionWhen is sharding justified?+
Answer
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.