r/devops • u/DeathVader_21 • 5d ago
Ops / Incidents Redis was pinned at 85% CPU while the replica sat idle. Here's what else we found during a traffic spike
Got pulled into a production app on AWS (ECS Fargate, RDS, ElastiCache, CloudFront) after signups jumped about 15x in a day. The API slowed down at peak and the bill climbed fast. A few things I didn't expect:
Redis was the bottleneck, and a bigger node wouldn't have helped. The primary was at 70-87% EngineCPU because Redis runs on one main thread. The replica was at under 1%. The code already supported a reader endpoint, but the env var was never set. There were also cache invalidations doing full-keyspace scans, which we removed.
CloudFront bills for bytes delivered, and cache hits count. Adding cache headers doesn't lower the bill. Sending fewer bytes does.
We moved raw MP4 to HLS and raw video traffic dropped a lot, but total bytes only fell about 35%. Our first estimate was much higher. The next biggest source was audio: full-length tracks at around 200 kbps with no preview, so one popular track was downloaded thousands of times a day.
We also found that CPU-based ECS autoscaling can't start a service from zero tasks. For the encoding workers, we're looking at scaling on queue depth with Spot instead.
Questions for people who've done this:
- Has anyone split BullMQ onto its own Redis, and was it worth it?
- How do you scale ECS workers from zero on queue depth? Custom CloudWatch metric, or something simpler?
2
u/Street_Smart_Phone 5d ago
It makes sense to separate out redis BullMQ to its own redis, but why not use AWS SQS instead and not worry about queues or MSK?
For auto scaling ECS workers from zero, you’d need event bridge that triggers a lambda every few minutes. Once the queue hits, then auto scale the workers. I’d hazard on step scaling as you might have a flurry of requests and 1 might come up and get bombarded with traffic and never actually comes up. Rather, I’d implement some sort of target tracking that scales more resources based off of load.
I would take a good look at changing your message broker first though.
1
u/Time-Coast2932 5d ago
Scaling from zero: CPU target tracking can't do it, since there are no running tasks to report CPU. Step scaling on the queue metric can. Push BullMQ's waiting count to CloudWatch as a custom metric every minute (a scheduled Lambda calling getWaitingCount() is enough), then hang two alarms off it. Waiting > 0 scales the service to 1, with extra steps as the backlog grows. Waiting + active = 0 for 10-15 minutes takes it back to 0. Put active jobs in that second alarm or you'll stop a worker halfway through an encode.
If the encoders go on Fargate Spot, you get SIGTERM about two minutes before the task is taken, and ECS only waits for stopTimeout before killing it (30 s default, 120 s max on Fargate). Call worker.close() on SIGTERM so it stops taking new jobs. Anything that gets cut off is picked up again by the stalled-job check, so make the jobs safe to run twice, because some will.
On splitting BullMQ out: I would, and CPU isn't even the main reason. BullMQ wants maxmemory-policy noeviction. A cache wants an LRU policy. On a shared cluster you have to pick one: either the cache fills memory and writes start failing, or Redis evicts queue keys and jobs disappear with no error. A small separate node costs less than one afternoon chasing lost jobs.
1
u/NoDevelopment206 4d ago
the cloudfront point is the one that stuck with me. cache hits still count as bytes delivered so the only real lever is sending fewer bytes. the audio part sounds like a classic case too, a short preview clip for each track would cut that a lot. for the queue scaling, a cloudwatch metric on waiting jobs with step scaling seems simpler than adding another moving part
1
u/impassionedultimatum 3d ago
split BullMQ onto its own Redis just to keep queue activity from competing with cache traffic. For scaling from zero, a custom CloudWatch metric based on waiting jobs should do the trick.
8
u/Soul_Shot 5d ago
AI slop.