Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Node.js need a job queue?

Tags:

node.js

Say I have a express service which sends email:

app.post('/send', function(req, res) {
  sendEmailAsync(req.body).catch(console.error)
  res.send('ok')
})

this works.

I'd like to know what's the advantage of introducing a job queue here? like Kue.

like image 937
wong2 Avatar asked Jun 20 '17 02:06

wong2


People also ask

Can I get job with only NodeJS?

Yes! If you find a company with the right tech stack. Look for companies who use Node. js, which is essentially server-side JavaScript.

Is NodeJS good for Career?

js development is a promising career opportunity for developers looking to start their career in web development and experienced developers looking to fast-forward their growth. At edWisor, you can learn Node. js development, starting from JavaScript. You can also learn other frameworks including Express.

Is NodeJS worth learning 2022?

Node. js development has become very popular over the last four years and continues to stand the competition in 2022 making startups worldwide choose it over other available options.

Does NodeJS have a future in 2022?

MEAN and MERN Stacks. js, React. js, Node. js) will keep their leading positions in app development in 2022. They are robust solutions for building dynamic and single-page web applications.


2 Answers

Does Node.js need a job queue?

Not generically.

A job queue is to solve a specific problem, usually with more to do than a single node.js process can handle at once so you "queue" up things to do and may even dole them out to other processes to handle.

You may even have priorities for different types of jobs or want to control the rate at which jobs are executed (suppose you have a rate limit cap you have to remain below on some external server or just don't want to overwhelm some other server). One can also use nodejs clustering to increase the amount of tasks that your node server can handle. So, a queue is about controlling the execution of some CPU or resource intensive task when you have more of it to do than your server can easily execute at once. A queue gives you control over the flow of execution.

I don't see any reason for the code you show to use a job queue unless you were doing a lot of these all at once.

The specific https://github.com/OptimalBits/bull library or Kue library you mention lists these features on its NPM page:

  • Delayed jobs
  • Distribution of parallel work load
  • Job event and progress pubsub
  • Job TTL
  • Optional retries with backoff
  • Graceful workers shutdown
  • Full-text search capabilities
  • RESTful JSON API
  • Rich integrated UI
  • Infinite scrolling
  • UI progress indication
  • Job specific logging

So, I think it goes without saying that you'd add a queue if you needed some specific queuing features and you'd use the Kue library if it had the best set of features for your particular problem.


In case it matters, your code is sending res.send("ok") before it finishes with the async tasks and before you know if it succeeded or not. Sometimes there are reasons for doing that, but sometimes you want to communicate back whether the operation was successful or not (which you are not doing).

like image 137
jfriend00 Avatar answered Oct 09 '22 15:10

jfriend00


Basically, the point of a queue would simply be to give you more control over their execution.

This could be for things like throttling how many you send, giving priority to other actions first, evening out the flow (i.e., if 10000 get sent at the same time, you don't try to send all 10000 at the same time and kill your server).

What exactly you use your queue for, and whether it would be of any benefit, depends on your actual situation and use cases. At the end of the day, it's just about controlling the flow.

like image 39
samanime Avatar answered Oct 09 '22 14:10

samanime