All articles

Software Engineering · Amazon Web Services

A cost efficient serverless solution to offload heavy background task processing

I was facing a problem of celery using too much memory on my VPS which currently has 4GiB of memory. For every docker compose service I launched, celery used roughly 300MiB of memory.

PC

Patrice Chaula

· 2 min read

A cost efficient serverless solution to offload heavy background task processing

I was using celery on multiple instances of the student management system to send SMS notifications in the background using SMS Portal. I was facing a problem of celery using too much memory on my VPS which currently has 4GiB of memory. For every docker compose service I ran celery took roughly 300MB of memory. I noticed when I ran 4 or more services at the same time the server became unresponsive.


I had to find a simple and cost effective solution that requires less maintainance. The first thing that came to my mind was AWS Lambda, SQS and EventBridge. I had to design the solution first before implementation. I knew that if I ran into implementation first I was going to end up with a messy solution that wasn't going to help me.


I listed my requirements in a document in points form. After listing all my requirements I started designing the architecture using draw.io. Below is the diagram of the architecture of the solution.

  1. Event Bridge: I added an event bridge rule (schedule) to run 3 (Lambda function to retrieve student records) every three days
  2. Admin API: It stores application data.
  3. Lambda function to retrieve student records: The function retrieves application data which includes API keys for every application from the Admin API. This so it can retrieve records of students with outstanding balances from each application.
  4. Application 2 API
  5. Application 1 API
  6. Amazon SQS: After 3 (Lambda function to retrieve student records) gets information from each application, it publishes all the records into SQS. Messages are put into message groups. The group's name is the application name. The is so each message group is processed in parallel. If we have 4 applications, that means 4 instances of the lambda function will run concurrently to process records for each application.
  7. Send SMS Lambda function: Whenever a new message is published into SQS, the function will be invoked. It makes a request to SMS portal API to send a message to a student. All the information required is stored in the SQS message record. If there are failures, It will retry 3 times before sending the message to the dead letter queue.


AWS CDK usage

I used AWS CDK toolkit to deploy this as Infrastructure as Code. AWS CDK has different libraries to provision your infrastructure using Python, Typescript, Javascript, Java, C# and Go. AWS CDK integrates with AWS CloudFormation to deploy and provision your infrastructure on AWS.


Was this solution worth it?

I didn't upgrade my virtual private server and I'm using AWS free tier services for the solution. This solution helped me cut my costs.

Less admin. More learning.

Enrolments, lessons, invoices and progress in one place. Set up your institution today.

Get startedMore articles