Background jobs are essential for executing tasks that are too time-consuming to run in the main execution thread without affecting the user experience.
These tasks might include data processing, sending batch emails, performing scheduled maintenance, or any other operations that are not immediately required to respond to user requests.
Utilizing background jobs allows your application to remain responsive and scalable, handling more requests simultaneously by offloading heavy lifting to background processes.
In Serverless frameworks, your hosting provider will likely have a limit for how long each task can last. Try searching for the maximum execution time for your hosting provider to find out more.
QStash provides a simple and efficient way to run background jobs, you can understand it as a 2 step process:
QStash requires a public endpoint to trigger background jobs, which means it cannot directly access localhost APIs. To get around this, you have two options:
Here’s what this looks like in a simple Next.js application:
To better understand the application, let’s break it down:
/api/start-email-job
, is invoked by the client to start the background job./api/send-email
endpoint, which contains the logic for the background job.Here is a visual representation of the process:
To view a more detailed Next.js quick start guide for setting up QStash, refer to the quick start guide.
It’s also possible to schedule a background job to run at a later time using schedules.
If you’d like to invoke another endpoint when the background job is complete, you can use callbacks.
Background jobs are essential for executing tasks that are too time-consuming to run in the main execution thread without affecting the user experience.
These tasks might include data processing, sending batch emails, performing scheduled maintenance, or any other operations that are not immediately required to respond to user requests.
Utilizing background jobs allows your application to remain responsive and scalable, handling more requests simultaneously by offloading heavy lifting to background processes.
In Serverless frameworks, your hosting provider will likely have a limit for how long each task can last. Try searching for the maximum execution time for your hosting provider to find out more.
QStash provides a simple and efficient way to run background jobs, you can understand it as a 2 step process:
QStash requires a public endpoint to trigger background jobs, which means it cannot directly access localhost APIs. To get around this, you have two options:
Here’s what this looks like in a simple Next.js application:
To better understand the application, let’s break it down:
/api/start-email-job
, is invoked by the client to start the background job./api/send-email
endpoint, which contains the logic for the background job.Here is a visual representation of the process:
To view a more detailed Next.js quick start guide for setting up QStash, refer to the quick start guide.
It’s also possible to schedule a background job to run at a later time using schedules.
If you’d like to invoke another endpoint when the background job is complete, you can use callbacks.