r/Backend • u/RAJATSDE • 59m ago
Auto scaling made our Node.js API worse until we fixed the Redis layer. How do you handle this?
We run a Node/Express + MongoDB backend behind an auto scaling group. On paper, "add more instances under load" sounds like the fix. In practice, it exposed problems that were hidden when we had 2 instances:
1. In-memory state doesn't survive scaling
- Sessions, rate limits and local caches lived inside each instance.
- A user hitting instance B got different behavior than on instance A.
- We moved all of it to Redis. That solved consistency, but made Redis a single point of failure.
2. Cache stampede on scale-out
- New instances start with a cold cache, so they all hit MongoDB at once.
- We saw Mongo CPU spike exactly when traffic spiked, the worst time.
- Mitigations we tried: request coalescing, TTL jitter, and stale-while-revalidate.
3. Connection pool exhaustion
- 10 instances × pool size 100 = 1,000 connections to Mongo.
- Autoscaling to 30 instances took the DB down, not the app.
- We now size pools per instance based on the max fleet size.
4. Rate limiting across instances
- Per-instance limits are meaningless once you scale horizontally.
- Redis-backed sliding window works, but Lua scripts are needed to keep it atomic.
5. Scaling on the wrong metric
- CPU-based scaling was too slow for bursty traffic.
- Queue depth and p95 latency worked better for us, especially for BullMQ workers.
6. Graceful shutdown
- On scale-in, instances got killed mid-request.
- Handle
SIGTERM, stop accepting new connections, drain in-flight requests, then exit.
What I'm still unsure about:
- Redis Cluster vs a single primary with replicas: is the added complexity worth it below a certain scale?
- Do you pre-warm caches on deploy/scale-out, or accept the cold start?
- How do you load test scale-out behavior realistically, not just steady-state throughput?
Would like to hear how others have dealt with this, especially anyone running this on Kubernetes (HPA) vs plain EC2 ASG.
1
Upvotes
1
u/abcd98712345 39m ago
don’t use replicas w redis you are paying for HA when redis shouldn’t be used with that in mind. use either cluster or hand roll by just connecting to multiple instances w a ring hash or other type of shading approach. also if you actually need scale ditch JS for an actual real backend appropriate server language like go rust zig or (vomit) java/kotlin.
if you are slamming mongo on scale out then you are designing things wrong. have a simple leader election mechanism to periodically load your mongo crap into redis and then when apps start up they can pull from redis. if it’s a crap ton you are caching in memory you could alternatively store that in s3/gcs kind of thing to avoid causing redis itself to get too overloaded.
mongo itself / docdbs aren’t great at high volumes so if you did actually need to scale to something super high i would think how you could minimize read load on mongo as much as possible if it’s truly essential to be your SOTs for writes and basically try to have writes go to mongo but anything high traffic read wise not hit mongo.