Task Queue Systems: A LeetCode Example

Jaspreet SinghJaspreet Singh
2 min read

A task queue system is used when we want to run some time-consuming task asynchronously outside of the request-response cycle of the application.

Let's take an example of how a solution is executed on Leetcode and the result is shown:-

When we run our solution on Leetcode then Leetcode makes a POST request to interpret_solution/ endpoint which in response returns an interpret_id. This interpret_id is later used to get the status of this job.

Leetcode makes a GET request to get the status of the job using interpret_id. The server returns the status something like this.

Leetcode uses polling to keep fetching the status until the state is successful of failed.

Here is a high-level design of how the task queue system is implemented and what components are involved in this process.

Client: On submitting the solution the client makes a post request to the backend to start the process and in response, it gets the job ID. It keeps making the get request using the job ID until the status is successful or failed.

Backend: On receiving the request backend creates a job payload. It also generates a job id and attaches it to the job payload. It first saves the initial job status in the data store and then pushes the job to a queue.

Queue: In our case Rabbitmq. It contains the jobs and forwards them to the respective workers.

Worker: Workers receive the jobs from the queue and perform the task. In this case, it will receive our code to evaluate and then update the result in the datastore using the job ID.

Redis/DB: This is the datastore that saves the job details about its status and result. So, the backend can use it to get the job status.

Conclusion:-

This was a high-level overview of how a task queue system works. I hope the examples above have helped in understanding the flow.
I'd love to hear your thoughts! Have you implemented a task queue system before? Share your experiences in the comments below!
If you have any questions about task queues or related concepts, feel free to ask in the comments. I'm here to help!

Till then happy coding and have a great time ahead ๐Ÿ˜‡๏ธ.

1
Subscribe to my newsletter

Read articles from Jaspreet Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jaspreet Singh
Jaspreet Singh

Software developer who loves solving problems and building things and has an interest in contributing to open-source.