Rewrite your workers in Rust (probably best option) or Go (very good option). Or at least try one that’s been creating issues, and just do one of them as a POC. I think you’ll find the issues go away nearly entirely.
What was the decision behind hosting it on a single ec2 ?
The memory spikes could be a number of application level issues, do they happen at a set timeframe or are they random?
Are your api calls asynchronous ? Do you have timeouts on them? All these things can cause a constraint on cpu resource.
I would look at moving your service to ECS Fargate if it’s handling tasks that are critical, a single ec2 isn’t going to give you the resilience you need.
For a starter I would recommend separating your API from your workers by placing them on separate instances. And for more resilience dockerize both and use an AWS managed service like ECS or Elastic Beanstalk (simple setup). That way a single api/worker failure won’t tear down your entire app.
And for the workers, I would (like you said) look into SQS or Bullmq to queue your worker jobs. That enables you to better manage your jobs and see what causes your memory spikes.
Try to figure out the memory spikes / crashing first before doing anything arch / design related. There could be a problem there and then you are just shifting your problem around.
You underestimated the necessary resources required to run your apps. Not uncommon really, but your reaction is. If an app is crashing 7 times a day, you can either fix the code, provision a larger node, or build better infra to handle the capacity problem. Throwing more cloudy solutions to fix a problem of your own making is what AWS is betting on.
So generally it seems like you’ve got a lot right! Separate API and workers is going to make scaling this an awful lot easier!
First of consider moving over to Fargate. If have the API auto scale based on CPU/memory to make sure it stays responsive. I would go with the SQS option for queueing jobs, because you can auto scale your workers based on queue length, and that way you keep background job processing responsive.
It’s not clear exactly how the background jobs work, i.e. is it always one type of job, or are there multiple different jobs? Basically I would go with one queue per job type, and have dedicated workers assigned to their own queue, but if that creates extra work, a single queue/worker combo will do fine
Longer term, depending on how the whole system works I might be tempted to convert the API to API gateway. You can use it to validate requests without code and place items direct into SQS. Other endpoints can use lambda functions. For the reports, save them S3 and then in the API return a redirect to a pre-signed S3 url so user can download easily without exposing the bucket itself. These are future suggestions though, for now just moving away from EC2 and into Fargate with SQS buffering should take you an awfully long way
OP is unclear, but it doesn’t sound like there’s a memory leak, there’s just either “bad” programming (holding entire documents in memory perhaps too long) or just lack of scaling in the design.
OP could spend a lot of time rewriting to Rust but it wouldn’t solve the underlying mathematical problem that requests/sec * avg request time * avg document size is close to the memory limit.
I do think rewriting to Rust could be helpful though, but it’s further down the list of priorities. To me, #1 is stopping the bleeding, give everything more RAM to stop dropping requests. #2 is to switch to passing documents as s3 URIs and then migrate the background workers to Lambda.
i don’t think using another cloud service is going to fix your underlying issue of a poorly designed application. if you dm me we can talk about the shift aspect of lift and shift but i’d need to see the details
Fargate is going to be more expensive and you still have to configure it to scale. While it removes the overhead of managing EC2, it does not remove the issue for OP.
node.js is a poor choice for time-critical heavy data processing
You can try to patch over that by throwing more hardware at it to buy time but if the SaaS keeps growing you’ll hit a wall eventually forcing a rewrite of at least the core logic in something more suitable
Use Lambda. You already know Node.js and can keep your web server since it probably won’t need scaling for a bit if you put the workers elsewhere.
Ideally, have the web sever put messages for workers in a queue (or multiple queues if you have different priority levels) and use Lambda to work it/them. This isn’t an idea setup, but it’s an easy step from where you are and allows workers to scale from zero to whatever you need when spikes happen.
As a software engineer I’d rather spin up a second instance in minutes to hours than a week or weeks doing a risky language rewrite of my core application.
Yeah, don’t migrate everything — just the worker code that runs in the background. The Lambda can connect to the same VPC that contains the DB, so it doesn’t need to be “open” in that sense.
As a temporary measure, I’d be right there with you. But, that’s temporary. And if they were really risk adverse, it wouldn’t be a single instance architecture based on Node.js. Obviously their tolerance for risk is already quite high.