someone
someone

Reputation: 101

Matching users flow Nodejs

So i'm trying to create a system which users can match each other by specific information, the flow i have in mind is as follows:

  1. user 1 fills the information and clicks "find"
  2. at the same time user 2 does the same as user 1
  3. the client sends a request to the server in route /X so the server can push the client to a (threadsafe)queue
  4. a worker thread pulls out from the queue each time and do the matching
  5. meanwhile the user polls route /Y in the server to get his match
  6. the worker thread finds 2 users match and pushes it to some (threadsafe)data structure
  7. next time the user polls the server(in /Y), the user gets the match and is redirected to the conversation

so first of all is this a good approach? and also is using a worker thread and threadsafe datastructure logical in javascript?(specifically Nodejs and express) is there an alternative or a better way to do this kind of stuff?

thanks.

Upvotes: 2

Views: 256

Answers (1)

Codebling
Codebling

Reputation: 11397

This is a bad approach.

You do not need (and should not use) worker threads for your use case.

On Worker Threads

Worker Threads are isolated instances of Javascript which run as a separate thread. They are intended strictly for performing CPU-intensive work.

vs vanilla Node

But you don't need them, because Node libraries are asynchronous, which means that unless your code really is CPU-intensive, you won't see any benefit from using Worker Threads (in fact there is overhead to using them, so if they aren't needed, your code will run slower).

From the docs: "Workers (threads) are useful for performing CPU-intensive JavaScript operations. They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be."

More on Threadedness

Javascript is single-threaded, and works very well that way. There is no concept of "threadsafe" in Javascript, because it isn't needed; all code is threadsafe.

If you do have CPU-intensive code

If you're doing expensive regex matching, then you are right to want to run this code in parallel. Worker Threads might not be the best way to do this, though.

Splitting CPU-intensive code into separate programs is often the most flexible solution. It gives you several options:

  1. spawn a new instance of Node run your CPU-intensive code (on the same server)
  2. run your CPU-intensive code in the cloud on "serverless" services, such AWS Lambda
  3. turn your CPU-intensive code into a "microservice", essentially a tiny webserver which does any specialized processing and returns the result

Further Reading

How Node's asynchronicity works (big picture) https://blog.insiderattack.net/event-loop-and-the-big-picture-nodejs-event-loop-part-1-1cb67a182810

What kinds of operations block the event loop and how to avoid it https://nodejs.org/uk/docs/guides/dont-block-the-event-loop/

Upvotes: 1

Related Questions