BBackend CourseLeroy Labs
0/0
Search the course

Graceful shutdown & the limits of one instance

Graceful shutdown, statelessness, and every piece of in-memory state in your projects that breaks when you run two instances (schedulers, rate limits, WebSocket brokers, SSE, caches), with the fix for each.

● advancedIR3D (Kontentt)Hostel-Web-AppReport-Manager-2SperoTek (in dev)

Graceful shutdown

server.shutdown: graceful
spring.lifecycle.timeout-per-shutdown-phase: 30s

On SIGTERM (a deploy or scale-in), Spring:

  1. Stops accepting new requests.
  2. Lets in-flight requests finish, up to the timeout.
  3. Drains executors, if configured.
  4. Closes DB pools.

Without it, a deploy cuts off requests halfway through, and payments end up in limbo.

SperoTek (in dev)application.yaml server.shutdown: graceful

Graceful shutdown is enabled. IR3D’s application-prod.yaml does the same.

Horizontal scaling needs stateless apps

Running two or more instances behind a load balancer only works if any instance can serve any request. Here’s every piece of per-JVM state found in your projects:

In-memory thing Where Breaks how with 2+ instances Fix
@Scheduled jobs All projects (IR3D has 19) Run on every instance, e.g. double revenue sweeps ShedLock (Postgres/Redis), Quartz cluster, or a dedicated worker
Rate-limit buckets Hostel ConcurrentHashMap, SperoTek Bucket4j, RM, IR3D Effective limit ×N, reset on deploy Bucket4j + Redis/JDBC, or edge limits (Cloudflare/WAF)
STOMP enableSimpleBroker Hostel, IR3D chat Users on instance A miss messages from B STOMP broker relay to RabbitMQ, or Redis pub/sub
SSE emitter map RM SseEmitterService Same as above Redis pub/sub or a broker fan-out to all nodes
@Cacheable default (ConcurrentMap) RM reportLines cache Stale data on other nodes after an update Redis cache, or a short TTL plus events to evict
Hand-rolled caches IR3D PublicStatsService, presence maps Inconsistent views Redis
@Async queues All Lost on restart (not about scaling, but related) Broker or outbox
Local file storage n/a (you use S3/Cloudinary/DB ✅) Files only on one node Object storage
HTTP sessions n/a (all stateless JWT ✅) Session only on one node Stateless tokens or Spring Session + Redis
IR3D (Kontentt)deploy/docker-compose.prod.yml comments

The team already wrote this down: add ShedLock and a STOMP relay when scaling. That’s good engineering: knowing the limits of the current design and recording the path forward.

  • Vertical vs horizontal scaling. A bigger box vs more boxes.
  • Load balancer health checks remove dead instances (readiness).
  • Sticky sessions. A crutch for stateful apps; avoid them if you can.
  • Database as the bottleneck. More app instances means more connections (pool lesson), so add read replicas, caching and PgBouncer.
  • Redis solves most of the table above at once (bonus module).

Check yourself

1. Simplest fix for duplicate @Scheduled runs across instances?
2. Which of your designs already scales horizontally without changes?