Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
https://github.com/soggie/naws
Leveraging ZeroMQ for 
Node.js 
Another tool in your toolbox
About Me 
• Ruben Tan 
• @roguejs (twitter) 
• Works in GrabTaxi (Malaysia) 
github.com/soggie/naws
ZeroMQ 
• “zero” as in none, nada, nil, 無 
• MQ as in Message Queue
Why call ZeroMQ as 
so when it is not a 
message queue?
I know. Wrong meme. Shhh.
• Originally conceived as a stock trading 
messaging solution 
• Moved on to generalizing its purpose 
• Adopted BSD Socke...
• ZeroMQ is a networking library with 
message queue semantics 
• Used primarily for connectivity between 
services 
• Can...
Crash Course! 
• Socket basics 
• Buffering (high watermark) 
• Load balancing (outgoing/incoming strategy) 
• PUSH socket...
0MQ Sockets 
• Supports: 
• inproc, IPC, TCP, pgm, epgm 
• Maintains it’s own internal queue 
• High watermark controls bu...
MUTE 
• Connection states: 
• connect() ==> READY 
• bind() ==> MUTE 
• accept() ==> READY 
• 2 kinds of MUTE state: block...
BLOCK
DROP
Exhibit A - REQ-REP 
https://github.com/soggie/naws/tree/master/test/talk/exhibit-a.js
// Bind the REP socket 
reply.bind('inproc://exhibit-a'); 
// Make the REQ socket connect to the REP socket 
request.conne...
PUSH 
• INCOMING: 
none 
• OUTGOING: 
round-robin 
• MUTE: 
blocks 
PPUUSSHH 
1 2 3 
4 5 6 
AA BB CC
Exhibit B 
https://github.com/soggie/naws/tree/master/test/talk/exhibit-b.js
PULL 
AA BB CC 
1 2 3 
• INCOMING 
fair-queue 
• OUTGOING 
none 
• MUTE 
block (??) PPUULLLL 
4 5 6
Exhibit C 
https://github.com/soggie/naws/tree/master/test/talk/exhibit-c.js
PUB 
• INCOMING 
none 
• OUTGOING 
fan-out 
• MUTE 
drop 
PPUUBB 
1 1 1 
2 2 2 
AA BB CC
Exhibit D 
https://github.com/soggie/naws/tree/master/test/talk/exhibit-d.js
SUB 
PP PP PP 
1 2 3 
• INCOMING 
fair-queue 
• OUTGOING 
none 
• MUTE 
none SSUUBB 
4 5 6
Exhibit E 
https://github.com/soggie/naws/tree/master/test/talk/exhibit-e.js
Let’s Build Something
• A simple HTTP web server on top of 
express 
• Uses worker processes to handle requests 
• Can add or kill workers any t...
Architecture 
SSUUBB PPUUBB 
RR EExxpprreessss 
PPUUSSHH 
5 
HHaannddlleerr 
PPUULLLL 
1 
2 
3 
4 
6 
8 7 
Server Process ...
Conclusion 
• zeromq is a lightweight networking library 
• build messaging topologies you want 
• nice async features ena...
Similar & References 
• Nanomsg (http://nanomsg.org) 
- Martin Sustrik, fork/rewrite of 0mq 
• Axon (https://github.com/vi...
THE END 
Questions? 
Questions?
Leveraging zeromq for node.js
Upcoming SlideShare
Loading in …5
×

Leveraging zeromq for node.js

2,259 views

Published on

ZeroMQ is a good tool to know, because it provides a huge variety of socket and messaging patterns that you can leverage in your infrastructure to solve specific problems without using heavyweight message queues to do the same job. This talk illustrates 4 basic sockets provided by zeromq and the many ways you can use them.

Published in: Technology
  • Be the first to comment

Leveraging zeromq for node.js

  1. 1. https://github.com/soggie/naws
  2. 2. Leveraging ZeroMQ for Node.js Another tool in your toolbox
  3. 3. About Me • Ruben Tan • @roguejs (twitter) • Works in GrabTaxi (Malaysia) github.com/soggie/naws
  4. 4. ZeroMQ • “zero” as in none, nada, nil, 無 • MQ as in Message Queue
  5. 5. Why call ZeroMQ as so when it is not a message queue?
  6. 6. I know. Wrong meme. Shhh.
  7. 7. • Originally conceived as a stock trading messaging solution • Moved on to generalizing its purpose • Adopted BSD Socket API and semantics • Smart end-point, dump network
  8. 8. • ZeroMQ is a networking library with message queue semantics • Used primarily for connectivity between services • Can be used to facilitate brokerless message passing topologies
  9. 9. Crash Course! • Socket basics • Buffering (high watermark) • Load balancing (outgoing/incoming strategy) • PUSH socket • PULL socket • PUB socket • SUB socket • We won’t cover: REQ-REP, XPUB-XSUB, ROUTER-DEALER, STREAM, PAIR, as well as DEVICES
  10. 10. 0MQ Sockets • Supports: • inproc, IPC, TCP, pgm, epgm • Maintains it’s own internal queue • High watermark controls buffer size • Has a MUTE strategy • Has different INCOMING and OUTGOING strategies
  11. 11. MUTE • Connection states: • connect() ==> READY • bind() ==> MUTE • accept() ==> READY • 2 kinds of MUTE state: block or drop
  12. 12. BLOCK
  13. 13. DROP
  14. 14. Exhibit A - REQ-REP https://github.com/soggie/naws/tree/master/test/talk/exhibit-a.js
  15. 15. // Bind the REP socket reply.bind('inproc://exhibit-a'); // Make the REQ socket connect to the REP socket request.connect('inproc://exhibit-a'); request.on('message', function (msg) { console.log('---- recv: ' + msg.toString()); }); reply.on('message', function (msg) { console.log('-- recv: ' + msg.toString()); work(); // Some heavy duty work reply.send(msg); console.log('--- send: ' + msg.toString()); }); setInterval(function () { var msg = color(ran(1000, 1000) + ''); console.log('- send: ' + msg); request.send(msg); }, 1000);
  16. 16. PUSH • INCOMING: none • OUTGOING: round-robin • MUTE: blocks PPUUSSHH 1 2 3 4 5 6 AA BB CC
  17. 17. Exhibit B https://github.com/soggie/naws/tree/master/test/talk/exhibit-b.js
  18. 18. PULL AA BB CC 1 2 3 • INCOMING fair-queue • OUTGOING none • MUTE block (??) PPUULLLL 4 5 6
  19. 19. Exhibit C https://github.com/soggie/naws/tree/master/test/talk/exhibit-c.js
  20. 20. PUB • INCOMING none • OUTGOING fan-out • MUTE drop PPUUBB 1 1 1 2 2 2 AA BB CC
  21. 21. Exhibit D https://github.com/soggie/naws/tree/master/test/talk/exhibit-d.js
  22. 22. SUB PP PP PP 1 2 3 • INCOMING fair-queue • OUTGOING none • MUTE none SSUUBB 4 5 6
  23. 23. Exhibit E https://github.com/soggie/naws/tree/master/test/talk/exhibit-e.js
  24. 24. Let’s Build Something
  25. 25. • A simple HTTP web server on top of express • Uses worker processes to handle requests • Can add or kill workers any time • Rudimentary monitoring and reliability
  26. 26. Architecture SSUUBB PPUUBB RR EExxpprreessss PPUUSSHH 5 HHaannddlleerr PPUULLLL 1 2 3 4 6 8 7 Server Process Worker Process
  27. 27. Conclusion • zeromq is a lightweight networking library • build messaging topologies you want • nice async features enabling you to do fun stuff (add/kill workers) • use to connect apps written in different languages/tech stacks • a very useful tool to have and understand
  28. 28. Similar & References • Nanomsg (http://nanomsg.org) - Martin Sustrik, fork/rewrite of 0mq • Axon (https://github.com/visionmedia/axon) - Pure node.js 0mq sockets • Theory of ZeroMQ http://250bpm.com/concepts • ZeroMQ Internal Architecture http://www.aosabook.org/en/zeromq.html • Dissecting Message Queues http://www.bravenewgeek.com/dissecting-message-queues/
  29. 29. THE END Questions? Questions?

×