Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ZeroMQ: Sockets on steroids

ZeroMQ: Sockets on steroids

It's 2012, the world's supposedly coming to an end and communication's still more painful than it should be. ZeroMQ is a socket abstraction and concurrency framework ("scripting for sockets") that's changing the way we think and reason about distributed systems.

Lourens Naudé

June 05, 2012
Tweet

More Decks by Lourens Naudé

Other Decks in Programming

Transcript

  1. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ZeroMQ: Sockets on steroids Lourens Naudé RailsWayCon 2012 Berlin - http://railswaycon.com/2012 Tuesday, June 5, 12
  2. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Operations @ WildfireApp.com Bio Tuesday, June 5, 12
  3. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Agenda 35k feet view Messages and patterns Transports Devices Resiliency Rewby Tuesday, June 5, 12
  4. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 What’s wrong with socket I/O ? Communication Tuesday, June 5, 12
  5. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Skills Strong dev profile required Multiplexed I/O knowledge Understand protocols Buffer management Tuesday, June 5, 12
  6. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Not portable Standards compliance, meh Socket options Error codes Ruby abstracts most of these Tuesday, June 5, 12
  7. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Not flexible 2 patterns Bi-directional or multicast Lock-step connection Fixed network topology Tuesday, June 5, 12
  8. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Zero broker, admin, cost, waste and (almost) zero latency What is ZeroMQ ? Tuesday, June 5, 12
  9. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Super (scriptable) sockets Magical sockets Tuesday, June 5, 12
  10. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Socket types PUB SUB REQ REP PUSH PULL Tuesday, June 5, 12
  11. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Example PUSH PULL PUSH PULL PULL PUSH PULL PUSH VENTILATOR SINK Tuesday, June 5, 12
  12. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 IM for apps Communications / concurrency framework Async by design ( Actor Pattern ) Pipes as buffers Ex AMQP protocol Aka Crossroads I/O (fork) Tuesday, June 5, 12
  13. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Cheap and efficient BSD sockets API Bindings for 30+ languages Lockless and FAST Automatic re-connection Multiplexed I/O ftw! Tuesday, June 5, 12
  14. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ENOTAMQ Tuesday, June 5, 12
  15. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Rich bindings ecosystem Ruby Erlang ( Sergey Aleynikov ) C++ PHP Python etc. Tuesday, June 5, 12
  16. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 In the wild Financial trading / brokers AT&T Twitter Peter Jackson - Lord of the Rings Github etc. Tuesday, June 5, 12
  17. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 http://zguide.zeromq.org Best OSS docs. Ever. Tuesday, June 5, 12
  18. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Opaque binary data (blobs) Messages Tuesday, June 5, 12
  19. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Message format Atomic delivery Framed wire protocol No contracts Multipart messages Tuesday, June 5, 12
  20. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 bind() or connect() to multiple endpoints, simultaneously Multiple endpoints Tuesday, June 5, 12
  21. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 bind() or connect() in any order Async connect Tuesday, June 5, 12
  22. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Request / Reply Supported patterns Tuesday, June 5, 12
  23. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Request / reply REQ REP REQ REP REP REP REQ REQ Tuesday, June 5, 12
  24. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Request / Reply usage REQ socket load balances REP socket multiplexes Lock step Bi-directional Custom routing Downstream topology flow control Tuesday, June 5, 12
  25. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Publish / subscribe Supported patterns Tuesday, June 5, 12
  26. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Publish / subscribe PUB SUB SUB SUB Tuesday, June 5, 12
  27. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Logging SUB PUB PUB PUB Tuesday, June 5, 12
  28. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Publish / subscribe usage Market data, game state etc. PUB socket distributes SUB socket filters Subscription forwarding (3.x) Tuesday, June 5, 12
  29. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Pipeline Supported patterns Tuesday, June 5, 12
  30. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Pipeline PUSH PULL PULL PULL Tuesday, June 5, 12
  31. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Pipeline usage Like REQ/REP, downstream reply Extendable - UNIX pipes Round-robin distribution Tasks with variable processing Tuesday, June 5, 12
  32. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Parallel pipeline PUSH PULL PUSH PULL PULL PUSH PULL PUSH VENTILATOR SINK Tuesday, June 5, 12
  33. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Transport agnostic messaging Transports Tuesday, June 5, 12
  34. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Transports Threads in a process - inproc:// Processes on a box - ipc:// Nodes in a network - tcp:// Uniform API - it just works Tuesday, June 5, 12
  35. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Lightweight switches Devices Tuesday, June 5, 12
  36. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Pub/sub persistence SUB PUB PUB SUB SUB Tuesday, June 5, 12
  37. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Proxy f = ctx.connect(:SUB, "tcp:// 0.0.0.0:5110") b = ctx.bind(:PUB, “tcp:// 0.0.0.0:5111”) loop do m = f.recv process(m) b.send(m) end Tuesday, June 5, 12
  38. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Uses Proxy Routing (LRU) Filtering / de-dup Translation Persistence Tuesday, June 5, 12
  39. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Interjection Principle Scalable topologies Tuesday, June 5, 12
  40. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Saving bandwidth SUB PUB PUB SUB SUB SUB SUB Tuesday, June 5, 12
  41. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Asynchronous external dependencies. Resiliency Tuesday, June 5, 12
  42. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ZeroMQ resiliency Atomic message delivery Delayed retry on failure Multi bind() and connect() Unordered bind() or connect() SOA - easy upgrades Tuesday, June 5, 12
  43. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Water marks Throttling / flow control High Water Mark (HWM) Socket option DROPS - REP, PUB, SUB BLOCKS - REQ, PUSH Tuesday, June 5, 12
  44. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 99% of deployments don’t need enterprise reliability Of reliability Tuesday, June 5, 12
  45. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Then Costly transactions Small topology Large ops team Little churn Tuesday, June 5, 12
  46. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Now Negligible transaction cost Large topology Small ops team Rapid churn rate Tuesday, June 5, 12
  47. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Reliability as patterns Lazy Pirate (REQ / REP) Majordomo (queue) Titanic (persistence) Binary Star (High Availability) See http://zguide.zeromq.org Tuesday, June 5, 12
  48. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Low latency, high throughput. Performance Tuesday, June 5, 12
  49. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Lab: 6 000 000 msgs/s Ruby: 200 000 msgs/s (16 byte) Numbers Tuesday, June 5, 12
  50. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Measured per message. Latency Tuesday, June 5, 12
  51. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Measured at specific points Throughput Tuesday, June 5, 12
  52. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Optimizations Dedicated I/O thread I/O model suits scripting languages Syscalls and allocations VSM (very small messages) Tuesday, June 5, 12
  53. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Opportunistic batching Latency > TCP Throughput > TCP Avoid the network stack Scales to any pipe size Tuesday, June 5, 12
  54. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 How to use it. Rewby. Tuesday, June 5, 12
  55. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Available gems ffi-rzmq - FFI, most popular rzmq - MRI only, not maintained rbczmq - high level C API ZeroMQ 2.x specific Tuesday, June 5, 12
  56. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ctx = ZMQ::Context.new(1) rep = ctx.bind(:REP, “inproc://p” req = ctx.connect(:REQ, “inproc://p” req.send(“message”) rep.receive(“message”) ctx.destroy Context Tuesday, June 5, 12
  57. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ctx = ZMQ::Context.new(1) sub = ctx.socket(:SUB) sub.subscribe(“APPLE”) sub.connect(“tcp://0.0.0.0:5000”) sub.connect(“ipc://other.pub”) loop do process(sub.recv) end Socket init Tuesday, June 5, 12
  58. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 ctx = ZMQ::Context.new s = ctx.bind(:PUSH, "inproc://work") 10.times{ ws << spawn_worker(ctx) } loop do msg = work.shift break if msg == “exit” s.send(msg) end Master Tuesday, June 5, 12
  59. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Thread.new do s = ctx.connect(:PULL, “inproc:// work”) loop do msg = s.recv break if msg == "quit" process(msg) end end Workers Tuesday, June 5, 12
  60. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Mongrel2 Case study Tuesday, June 5, 12
  61. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 How it works Multi-protocol ZeroMQ for backends Async backend requests Language agnostic (bindings) Tuesday, June 5, 12
  62. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Mongrel2 topology PULL PUB PULL PUB PUSH SUB BACKEND A BACKEND B MONGREL2 CLIENT Tuesday, June 5, 12
  63. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Best practices Patterns - learn them Stable bind() Unstable connect() Interjection principle Set water marks Read http://zguide.zeromq.org Tuesday, June 5, 12
  64. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 The Future UDP, DNS transports etc. Scalability Protocol (Linux Kernel) Mobile / embedded Monitoring support (coming soon) Tuesday, June 5, 12
  65. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 Questions ? O fim Tuesday, June 5, 12
  66. Wildfire Interactive, Inc. | 1600 Seaport Boulevard, Suite 500, Redwood

    City, CA 94063 | (888) 274-0929 @methodmissing github.com/methodmissing Thanks! Tuesday, June 5, 12