The Zeromq Guide - for Python Developers
Total Page:16
File Type:pdf, Size:1020Kb
The ZeroMQ Guide - for Python Developers Pieter Hintjens The ZeroMQ Guide - for Python Developers by Pieter Hintjens Dedication This book is dedicated to the ØMQ community. Table of Contents Preface.......................................................................................................................................................ix 1. ØMQ in a Hundred Words............................................................................................................ix 2. How It Began................................................................................................................................ix 3. The Zen of Zero............................................................................................................................ix 4. How This Book Came To Be.........................................................................................................x 5. Audience.......................................................................................................................................xi I. Learning ØMQ.................................................................................................................................... xii 1. Basics.............................................................................................................................................1 1.1. Fixing the World................................................................................................................1 1.2. Starting Assumptions.........................................................................................................2 1.3. Getting the Examples........................................................................................................2 1.4. Ask and Ye Shall Receive..................................................................................................2 1.5. A Minor Note on Strings...................................................................................................7 1.6. Version Reporting..............................................................................................................9 1.7. Getting the Message Out...................................................................................................9 1.8. Divide and Conquer.........................................................................................................13 1.9. Programming with ØMQ.................................................................................................18 1.10. Why We Needed ØMQ..................................................................................................20 1.11. Socket Scalability..........................................................................................................24 1.12. Upgrading from ØMQ v2.2 to ØMQ v3.2....................................................................25 1.13. Warning: Unstable Paradigms!......................................................................................26 2. Sockets and Patterns.....................................................................................................................28 2.1. The Socket API................................................................................................................28 2.2. Messaging Patterns..........................................................................................................33 2.3. Handling Errors and ETERM..........................................................................................52 2.4. Handling Interrupt Signals..............................................................................................57 2.5. Detecting Memory Leaks................................................................................................58 2.6. Multithreading with ØMQ...............................................................................................59 2.7. Signaling Between Threads (PAIR Sockets)...................................................................64 2.8. Node Coordination..........................................................................................................67 2.9. Zero-Copy........................................................................................................................70 2.10. Pub-Sub Message Envelopes.........................................................................................71 2.11. High-Water Marks.........................................................................................................73 2.12. Missing Message Problem Solver.................................................................................74 3. Advanced Request-Reply Patterns...............................................................................................78 3.1. The Request-Reply Mechanisms.....................................................................................78 3.2. Request-Reply Combinations..........................................................................................83 3.3. Exploring ROUTER Sockets...........................................................................................86 3.4. The Load Balancing Pattern............................................................................................88 3.5. A High-Level API for ØMQ.........................................................................................101 3.6. The Asynchronous Client/Server Pattern......................................................................108 3.7. Worked Example: Inter-Broker Routing.......................................................................114 4. Reliable Request-Reply Patterns................................................................................................140 4.1. What is "Reliability"?....................................................................................................140 4.2. Designing Reliability.....................................................................................................141 iv 4.3. Client-Side Reliability (Lazy Pirate Pattern).................................................................142 4.4. Basic Reliable Queuing (Simple Pirate Pattern)............................................................146 4.5. Robust Reliable Queuing (Paranoid Pirate Pattern)......................................................150 4.6. Heartbeating..................................................................................................................158 4.7. Contracts and Protocols.................................................................................................161 4.8. Service-Oriented Reliable Queuing (Majordomo Pattern)............................................162 4.9. Asynchronous Majordomo Pattern................................................................................178 4.10. Service Discovery........................................................................................................186 4.11. Idempotent Services....................................................................................................187 4.12. Disconnected Reliability (Titanic Pattern)..................................................................188 4.13. High-Availability Pair (Binary Star Pattern)...............................................................193 4.14. Brokerless Reliability (Freelance Pattern)...................................................................200 4.15. Conclusion...................................................................................................................208 5. Advanced Pub-Sub Patterns.......................................................................................................209 5.1. Pros and Cons of Pub-Sub.............................................................................................209 5.2. Pub-Sub Tracing (Espresso Pattern)..............................................................................211 5.3. Last Value Caching........................................................................................................211 5.4. Slow Subscriber Detection (Suicidal Snail Pattern)......................................................213 5.5. High-Speed Subscribers (Black Box Pattern)...............................................................216 5.6. Reliable Pub-Sub (Clone Pattern).................................................................................219 II. Advanced ØMQ................................................................................................................................239 6. The ØMQ Community...............................................................................................................240 6.1. Architecture of the ØMQ Community..........................................................................241 6.2. How to Make Really Large Architectures.....................................................................242 6.3. The ØMQ Process: C4...................................................................................................251 6.4. A Real-Life Example.....................................................................................................263 6.5. Git Branches Considered Harmful................................................................................266 6.6. Designing for Innovation...............................................................................................270 6.7. Burnout..........................................................................................................................277