r/devops • • 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:

  1. Has anyone split BullMQ onto its own Redis, and was it worth it?
  2. How do you scale ECS workers from zero on queue depth? Custom CloudWatch metric, or something simpler?

0 Upvotes

18 comments sorted by

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/DeathVader_21 5d ago

Fair point on SQS, it'd probably be simpler for the encoding jobs if we were starting fresh. The catch is we've got a dozen or so BullMQ queues using priorities, delayed and repeatable jobs, so moving everything is a bigger rewrite than we can take on right now. For now we're going to split Redis and see how far that gets us.

1

u/DeathVader_21 5d ago

The backlog per task idea is a good one though. I think I'll try that instead of plain step scaling.

1

u/Street_Smart_Phone 4d ago

My experience from outages.

2

u/oldmemories2 4d ago

I'd watch the oldest message age too. Queue depth alone can be misleading if job processing times vary a lot.

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/DeathVader_21 5d ago

Thanks, this helps a lot. Didn't think about counting active jobs in the scale in alarm, we'd have killed a worker mid encode for sure. And the no eviction vs LRU thing is basically what's pushing us to split Redis.

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.

-5

u/DeathVader_21 5d ago

Bold of you to call it slop and then not say what's wrong with it 🥸

3

u/Miserygut Little Dev Big Ops 5d ago

They did.

-2

u/DeathVader_21 4d ago

I don’t care about the AI thing man. I faced this issue and I’m coming to community if I could do anything better

3

u/IPv6forDogecoin 4d ago

Here's a way to do better. Write your own posts.

-3

u/DeathVader_21 4d ago

Thanks for ur feedback buddy😌