zeromq in php
DESCRIPTION
TRANSCRIPT
Socket programming brought to your web app
Ømq and CakePHP
What is Ømq?
Intelligent socket library for messaging
Incredibly flexible, you can build your own patterns
Fast as hell
Fault tolerant
Language agnostic
What is not Ømq?
A message queue, it literally has zero message queues
A server you can connect to
Persistent
Out of the box solution (sorry to let you down)
Why use Ømq instead of X?It’s a paradigm change, you can hardly compare it to “other solutions”
When your system require more advanced network interactions (or patterns)
You need cheap and easy parallel processing
You don’t want a single point of failure
You need exceptional speed
Transports
Supported transports
INPROC (in-process communication)
IPC (inter-process communication)
Multicast (using a pgm multicast)
TCP
Let’s talk about patterns
Patterns is all what Ømq is about
You can choose to use any of the patterns or combine them all in a single application
Combining patterns help you build more advanced distributed systems
Request - Reply
Client ServerICANHAZ?
Request - Replyclient.php
Request - Replyserver.php
Wait a minute...
This looks way too slow, a single php process won’t cope the load of multiple clients.
We need a solution
The solution
Start more servers on different ports
Bind your clients to multiple servers using the same socket
Ømq will automatically load balance them for you
Drawback: You need to hardcode server addresses on each client
Load Balanced Requests
More on balancing later...
Publisher - Subscriber
ServerClient
Client
Client
Client
Publisherpublisher.php
Subscriberclient.php
Pub - Sub
Like a radio broadcast, if you tune in late you’ll miss part of the show
If client crashes the it will also lose messages
It is possible to attach multiple filters to a connection
You can listen on multiple addresses at once
Pub -Sub
Ømq will buffer the messages in the subscriber if it is a “slow listener”
It will drop messages if buffer gets too big
That is extremely cool
Push - Pull (Pipeline)
Client Worker Worker Worker
Fan
Worker
Worker
Worker
Sink
more steps
Push - Pullventilator.php
Push - Pulltask.php
Push - Pullsink.php
Push - Pull
One way only
PUSH is used for “fanning out” tasks
PULL is used for fan-in or sink processes
Workers can hop-in at any time
Only one worker gets the task at a time and they are load balanced
Push - Pull
Ømq will buffer the messages if there are no peers to get them
It will block at some point if buffering too much
That is also extremely cool
Devices
Devices or Brokers
Devices are programs that will route messages between two nodes
Devices are usually small but they can also be trusted with larger tasks, such as persisting messages
Used to solve hard problems but also introduce single points of failure
We needed a solution
Devices as balancersClient Client Client
Dealer
Server Server Server
A simple brokerbroker.php
Devices
Clients will connect to localhost:5559
Servers will connect to localhost:5560
Load balancing is done by Ømq
If dealer fails everything breaks :(
You can install failover dealers! :)
Devices
Allow you to create more complex topologies
Help you route requests to anonymous workers
Good place for handling/persisting/retrying messages extra logic
Can be written in any language supported by Ømq!
Let’s see some examples