Pub/Sub across threads, can the library handle it? #401
-
When the server is running with a single instance, everything appears fine. If we spin up multiple threads using workers, pub/sub fails... presumably because each thread is isolated and cannot see messages in other threads. With that in mind, what's the most appropriate way to handle this, in terms of scaling? Even if we scale horizontally with a single instance on each machine, neither machine can communicate without some method of passing data between. I'm hoping this can be achieved without a stateful DB or similar. Any advice would be much appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
You can send messages between workers https://nodejs.org/api/worker_threads.html#worker_threads_class_messagechannel You could use websockets to send between machines. Use a websocket client in each server to connect to the other uWebSocket servers https://www.npmjs.com/package/ws This would be good reason to add websocket client to uWebSockets but that is not included yet so you have to use different client library https://github.com/uNetworking/uWebSockets/issues/791 redis pub/sub as mentioned below is probably better option to connect servers, this requires a central server to broadcast messages |
Beta Was this translation helpful? Give feedback.
-
I'm doing exactly this (workers communication) to publish the messages on all workers. It's working well with a lag below 1ms between workers |
Beta Was this translation helpful? Give feedback.
-
Another option, Redis his pub/sub |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply. Do you have an example of this please? Would be much appreciated. |
Beta Was this translation helpful? Give feedback.
-
Sure, here it is: first of all I override the publish function of the app to change the behavior, instead of directly publishing, I publish + postMessage to the parent process controlling all workers
Then each worker will receive this message and process it like this:
|
Beta Was this translation helpful? Give feedback.
-
@jbenguira That's a huge help, thank you. |
Beta Was this translation helpful? Give feedback.
-
postMessage is good, but as soon as you have more than one server, you will need Redis or its equivalent, and if you are already sending to Redis you will receive messages in all subscribed threads. |
Beta Was this translation helpful? Give feedback.
-
Best approach is to do both, postMessage for inter worker communication and websockets for inter server communication |
Beta Was this translation helpful? Give feedback.
You can send messages between workers https://nodejs.org/api/worker_threads.html#worker_threads_class_messagechannel
You could use websockets to send between machines. Use a websocket client in each server to connect to the other uWebSocket servers https://www.npmjs.com/package/ws This would be good reason to add websocket client to uWebSockets but that is not included yet so you have to use different client library https://github.com/uNetworking/uWebSockets/issues/791
redis pub/sub as mentioned below is probably better option to connect servers, this requires a central server to broadcast messages