Motivation
Serverless functions are great for many tasks with their dynamic scaling and flexible pricing models. But when you have a task which is composed of long running complex steps, it is not feasible to run it in a single serverless function. A simple solution is simply to offload complicated tasks from the serverless function. You can process those asynchronously in your preferred environment, this can be other serverless functions, serverless containers or traditional server based processes too. To offload your tasks, you need a reliable event queue. In this article we will use Upstash Redis for this purpose.Scenario
You are developing aNew Employee Registration
form for your company. Saving
employee records to the database is the easy part. Here possible things to do:
- Create accounts (email, slack etc).
- Send email to the employee.
- Send email to the hiring manager and others.
- Create a JIRA ticket for the IT department so they will set up the employee’s computer.
- You want the form to be responsive. You do want a new employee to wait for minutes after clicking submit.
- The above steps are subject to change. You do not want to update your code whenever a new procedure is added.
Project Setup
The project will consist of two modules:- Producer will be a serverless function which will receive input parameters required to register a new employee. It will also produce events for the task queue.
- Consumer will be a worker application which will continuously consume the task queue.
Tech Stack
- AWS Lambda for Serverless computing
- Upstash as Serverless Redis
- Bull as task queue implementation
- Serverless framework for project deployment
Upstash Database
You can create a free Redis database from Upstash. After creating a database, copy the endpoint, port and password as you will need in the next steps.Producer Code
Our producer will be the serverless function which will get the request parameters and produce the task for the queue. In the real world this code should do things like saving to the database but I will not implement this for the sake of simplicity. 1- Create a Serverless project byserverless
command.
npm install bull
3- Function code:
Consumer Code
We will write a basic Node application to consume the events. Create a new directory and runnpm init
and npm install bull
. Then create index.js as
below:
Test the Application
First run the consumer application withnode index
To test the producer code, run:

