Messaging Patterns with ZeroMQ
           In Python
    “It’s time to think distributedly”

              By Victor Lin
The Age of Distribution
Different messaging methods
           Node                               Node
          Process                            Process
                           TCP/IP
             UDP




                                                         IPC
Network
             UDP




                                             Process
 Node      Node     Node
                                    Thread             Thread

                                               ITC
Background
•   It’s all about messaging
•   Too many messaging transports
•   Common patterns cannot be reused
•   Auto reconnect is needed
Superhero
ZeroMQ features
•   Lightweight overhead
•   Auto reconnect
•   One-to-many connection
•   Support different transports
•   Support different patterns
•   Support different languages
Patterns
•   REQ/REP
•   SUB/PUB
•   PUSH/PULL
•   PAIR
REQ/REP
• Round-robin
• One-to-many
• One request, one response
SUB/PUB
• One-to-many
• All subscribers receive messages from
  publisher
PUSH/PULL
• Round-robin
• One-to-many
• Many-to-one
PAIR
• Just like BSD socket
• Bidirectional communication
Different transports
•   TCP/IP – tcp://
•   Unix domain socket (IPC) – ipc://
•   Inter-thread Communication – inproc://
•   Multicast – pgm://
Same program, different transport
           Node                               Node
          Process                            Process
                           tcp://



                                                        ipc://
           pgm://
Network
                                             Process
 Node      Node     Node
                                    Thread             Thread

                                         inproc://
TCP/IP


                                      Worker
                             tcp://   Node



Manager                               Worker
 Node     tcp://   REQ/REP   tcp://   Node



                                      Worker
                             tcp://   Node
Unix domain socket
                      Node


                                      Worker
                             ipc://   Process



Manager                               Worker
Process   ipc://   REQ/REP   ipc://   Process



                                      Worker
                             ipc://   Process
Inter-thread Communication
                         Node

                        Process
                                              Worker
                                  inproc://   Thread



Manager                                       Worker
Thread    inproc://   REQ/REP     inproc://   Thread



                                              Worker
                                  inproc://   Thread
Different languages
•   Python
•   C/C++
•   Java
•   .Net
•   Ruby
•   Go
•   PHP
•   Perl
•   …
Example: authentication service
• Updating authentication module requires
  restarting server
• Authentication module could be the
  bottleneck

                               Broadcast

       Mr.DJ     TCP/IP      Authentication
                                module
Example: authentication service
• Authentication service can be restarted
  anytime
• Working load can be distributed

                                       Authentication
           Broadcast   REQ/REP (IPC)      service




            Mr.DJ
Example: authentication service

Broadcast                     Authentication
                                 service

             Authentication
Broadcast       broker        Authentication
                                 service



Broadcast                     Authentication
                                 service
Broker in ZMQ
Example: recording service
• Record audio when broadcasting
• Upload file chunks to Amazon S3
• Must be reliable
Amazon S3

  Broadcast



PUB/SUB (IPC)
                         Uploading     Uploading      Uploading
                          worker        worker         worker

Writing service
                                                   REQ/REP (IPC)

                  File                 Uploading
                                        service

         Local
          DB
Server level plugins
• Attach whatever you want

                                             Record service
                                    ipc://



     Broadcast   ipc://                           Audio
                          PUB/SUB   ipc://     identifying
                                                 service



                                    ipc://
                                                   …
Conclusions
•   Flexible
•   High performance
•   Easy to use
•   Multi-language supported
•   Focus on messaging and topology
Q&A
• Any question?

Pyhug zmq