Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system...
Transcript of Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system...
![Page 1: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/1.jpg)
Distributed Systemswith ZeroMQ and gevent
Jeff Lindsay@progrium
![Page 2: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/2.jpg)
Why distributed systems?
Harness more CPUs and resources
Run faster in parallel
Tolerance of individual failures
Better separation of concerns
![Page 3: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/3.jpg)
Most web apps evolve into distributed systems
![Page 4: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/4.jpg)
![Page 5: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/5.jpg)
![Page 6: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/6.jpg)
![Page 7: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/7.jpg)
![Page 8: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/8.jpg)
![Page 9: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/9.jpg)
![Page 10: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/10.jpg)
OpenStack
![Page 11: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/11.jpg)
Web
API
Client
Amazon AWS
TwiML
Provider
Provider
Provider
![Page 12: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/12.jpg)
ZeroMQ + geventTwo powerful and misunderstood tools
![Page 13: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/13.jpg)
ConcurrencyHeart of Distributed Systems
![Page 14: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/14.jpg)
Distributed computing is just another flavor of
local concurrency
![Page 15: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/15.jpg)
Multithreading
Distributed system
Shared Memory
Thread ThreadThread
Shared Database
App AppApp
![Page 16: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/16.jpg)
Concurrency models
Execution modelDefines the “computational unit”
Communication modelMeans of sharing and coordination
![Page 17: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/17.jpg)
Concurrency models
Traditional multithreadingOS threadsShared memory, locks, etc
Async or Evented I/OI/O loop + callback chainsShared memory, futures
Actor modelShared nothing “processes”Built-in messaging
![Page 18: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/18.jpg)
Examples
ErlangActor model
ScalaActor model
GoChannels, Goroutines
Everything else (Ruby, Python, PHP, Perl, C/C++, Java)
ThreadingEvented
![Page 19: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/19.jpg)
Normally, the networking of distributed systems is tacked on to the
local concurrency model.
Erlang is special.
MQ, RPC, REST, ...
![Page 20: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/20.jpg)
Why not always use Erlang?
![Page 21: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/21.jpg)
Why not always use Erlang?
Half reasonsWeird/ugly languageLimited library ecosystemVM requires operational expertiseFunctional programming isn’t mainstream
![Page 22: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/22.jpg)
Why not always use Erlang?
Half reasonsWeird/ugly languageLimited library ecosystemVM requires operational expertiseFunctional programming isn’t mainstream
Biggest reasonIt’s not always the right tool for the job
![Page 23: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/23.jpg)
Web
API
Client
Amazon AWS
TwiML
Provider
Provider
Provider
![Page 24: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/24.jpg)
Service Oriented Architecture
Multiple languagesHeterogeneous cluster
![Page 25: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/25.jpg)
RPC
![Page 26: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/26.jpg)
Client / server
RPC
![Page 27: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/27.jpg)
Client / serverMapping to functions
RPC
![Page 28: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/28.jpg)
Client / serverMapping to functionsMessage serialization
RPC
![Page 29: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/29.jpg)
Client / serverMapping to functionsMessage serialization
RPC
Poor abstraction of what you really want
![Page 30: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/30.jpg)
What you want are tools to help you get distributed actor model concurrency like Erlang ... without Erlang.
Even better if they're decoupled and optional.
![Page 31: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/31.jpg)
Rarely will you build an application as part of a distributed system that does
not also need local concurrency.
![Page 32: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/32.jpg)
Communication model
How do we unify communications in local concurrency and distributed systems across languages?
![Page 33: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/33.jpg)
Execution model
How do we get Erlang-style local concurrency without interfering with the language's idiomatic paradigm?
![Page 34: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/34.jpg)
ZeroMQCommunication model
![Page 35: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/35.jpg)
Misconceptions
![Page 36: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/36.jpg)
Misconceptions
It’s just another MQ, right?
![Page 37: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/37.jpg)
Misconceptions
It’s just another MQ, right? Not really.
![Page 38: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/38.jpg)
Misconceptions
It’s just another MQ, right? Not really.
![Page 39: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/39.jpg)
Misconceptions
It’s just another MQ, right? Not really.
Oh, it’s just sockets, right?
![Page 40: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/40.jpg)
Misconceptions
It’s just another MQ, right? Not really.
Oh, it’s just sockets, right? Not really.
![Page 41: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/41.jpg)
Misconceptions
It’s just another MQ, right? Not really.
Oh, it’s just sockets, right? Not really.
![Page 42: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/42.jpg)
Misconceptions
It’s just another MQ, right? Not really.
Oh, it’s just sockets, right? Not really.
Wait, isn’t messaging a solved problem?
![Page 43: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/43.jpg)
Misconceptions
It’s just another MQ, right? Not really.
Oh, it’s just sockets, right? Not really.
Wait, isn’t messaging a solved problem?*sigh* ... maybe.
![Page 44: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/44.jpg)
Regular Sockets
![Page 45: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/45.jpg)
Regular Sockets
Point to point
![Page 46: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/46.jpg)
Regular Sockets
Point to pointStream of bytes
![Page 47: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/47.jpg)
Regular Sockets
Point to pointStream of bytesBuffering
![Page 48: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/48.jpg)
Regular Sockets
Point to pointStream of bytesBufferingStandard API
![Page 49: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/49.jpg)
Regular Sockets
Point to pointStream of bytesBufferingStandard APITCP/IP or UDP, IPC
![Page 50: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/50.jpg)
Messaging
![Page 51: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/51.jpg)
MessagingMessages are atomic
![Page 52: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/52.jpg)
MessagingMessages are atomic
![Page 53: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/53.jpg)
MessagingMessages are atomic
![Page 54: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/54.jpg)
MessagingMessages are atomic
![Page 55: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/55.jpg)
MessagingMessages are atomic Messages can be routed
![Page 56: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/56.jpg)
MessagingMessages are atomic Messages can be routed
![Page 57: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/57.jpg)
MessagingMessages are atomic Messages can be routed
![Page 58: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/58.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 59: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/59.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 60: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/60.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 61: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/61.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 62: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/62.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 63: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/63.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around
![Page 64: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/64.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around Messages are delivered
![Page 65: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/65.jpg)
MessagingMessages are atomic Messages can be routed
Messages may sit around Messages are delivered
![Page 66: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/66.jpg)
Rise of the Big MQ
![Page 67: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/67.jpg)
AppApp
AppApp
ReliableMessage Broker
PersistentQueues
![Page 68: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/68.jpg)
AppApp
AppApp
![Page 69: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/69.jpg)
AMQP
Producer Consumer
MQ
![Page 70: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/70.jpg)
AMQP
Producer Consumer
MQ
Exchange Queue
BindingX
![Page 71: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/71.jpg)
AMQP
Producer Consumer
MQ
Exchange Queue
X
![Page 72: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/72.jpg)
AMQP Recipes
![Page 73: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/73.jpg)
AMQP RecipesWork queuesDistributing tasks among workers
![Page 74: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/74.jpg)
AMQP RecipesWork queuesDistributing tasks among workers
Publish/SubscribeSending to many consumers at once
X
![Page 75: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/75.jpg)
AMQP RecipesWork queuesDistributing tasks among workers
Publish/SubscribeSending to many consumers at once
X
RoutingReceiving messages selectively
X
foo
bar
baz
![Page 76: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/76.jpg)
AMQP RecipesWork queuesDistributing tasks among workers
Publish/SubscribeSending to many consumers at once
X
RPCRemote procedure call implementation
RoutingReceiving messages selectively
X
foo
bar
baz
![Page 77: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/77.jpg)
Drawbacks of Big MQ
Lots of complexity
Queues are heavyweight
HA is a challenge
Poor primitives
![Page 78: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/78.jpg)
Enter ZeroMQ“Float like a butterfly, sting like a bee”
![Page 79: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/79.jpg)
Echo in Python
Server Client
import zmqcontext = zmq.Context()socket = context.socket(zmq.REP)socket.bind("tcp://127.0.0.1:5000")
while True: msg = socket.recv() print "Received", msg socket.send(msg)
123456789
import zmqcontext = zmq.Context()socket = context.socket(zmq.REQ)socket.connect("tcp://127.0.0.1:5000")
for i in range(10): msg = "msg %s" % i socket.send(msg) print "Sending", msg reply = socket.recv()
123456789
10
![Page 80: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/80.jpg)
Echo in Ruby
Server Client
require "zmq"context = ZMQ::Context.new(1)socket = context.socket(ZMQ::REP)socket.bind("tcp://127.0.0.1:5000")
loop do msg = socket.recv puts "Received #{msg}" socket.send(msg)end
123456789
10
require "zmq"context = ZMQ::Context.new(1)socket = context.socket(ZMQ::REQ)socket.connect("tcp://127.0.0.1:5000")
(0...10).each do |i| msg = "msg #{i}" socket.send(msg) puts "Sending #{msg}" reply = socket.recvend
123456789
1011
![Page 81: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/81.jpg)
Echo in PHP
Server Client
<?php$context = new ZMQContext();$socket = $context->getSocket(ZMQ::SOCKET_REP);$socket->bind("tcp://127.0.0.1:5000");
while (true) { $msg = $socket->recv(); echo "Received {$msg}"; $socket->send($msg);}?>
123456789
1011
<?php$context = new ZMQContext();$socket = $context->getSocket(ZMQ::SOCKET_REQ);$socket->connect("tcp://127.0.0.1:5000");
foreach (range(0, 9) as $i) { $msg = "msg {$i}"; $socket->send($msg); echo "Sending {$msg}"; $reply = $socket->recv();}?>
123456789
101112
![Page 82: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/82.jpg)
Bindings
ActionScript, Ada, Bash, Basic, C, Chicken Scheme, Common Lisp, C#, C++, D, Erlang,
F#, Go, Guile, Haskell, Haxe, Java, JavaScript, Lua, Node.js, Objective-C, Objective Caml, ooc, Perl, PHP, Python, Racket, REBOL,
Red, Ruby, Smalltalk
![Page 83: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/83.jpg)
Plumbing
![Page 84: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/84.jpg)
Plumbing
![Page 85: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/85.jpg)
Plumbing
![Page 86: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/86.jpg)
Plumbing
![Page 87: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/87.jpg)
Plumbing
![Page 88: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/88.jpg)
Plumbing
inprocipctcpmulticast
![Page 89: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/89.jpg)
Plumbing
inprocipctcpmulticast
socket.bind("tcp://localhost:5560")socket.bind("ipc:///tmp/this-socket")socket.connect("tcp://10.0.0.100:9000")socket.connect("ipc:///tmp/another-socket")socket.connect("inproc://another-socket")
![Page 90: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/90.jpg)
Plumbing
inprocipctcpmulticast
socket.bind("tcp://localhost:5560")socket.bind("ipc:///tmp/this-socket")socket.connect("tcp://10.0.0.100:9000")socket.connect("ipc:///tmp/another-socket")socket.connect("inproc://another-socket")
![Page 91: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/91.jpg)
Plumbing
inprocipctcpmulticast
socket.bind("tcp://localhost:5560")socket.bind("ipc:///tmp/this-socket")socket.connect("tcp://10.0.0.100:9000")socket.connect("ipc:///tmp/another-socket")socket.connect("inproc://another-socket")
![Page 92: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/92.jpg)
Message Patterns
![Page 93: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/93.jpg)
Message PatternsRequest-Reply
REQ REP
![Page 94: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/94.jpg)
Message PatternsRequest-Reply
REQ REP
REP
REP
![Page 95: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/95.jpg)
Message PatternsRequest-Reply
REQ REP
REP
REP
![Page 96: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/96.jpg)
Message PatternsRequest-Reply
REQ REP
REP
REP
![Page 97: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/97.jpg)
Message PatternsRequest-Reply
REQ REP
REP
REP
![Page 98: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/98.jpg)
Message PatternsRequest-Reply Publish-Subscribe
REQ REP
REP
REP
PUB
SUB
SUB
SUB
![Page 99: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/99.jpg)
Message PatternsRequest-Reply Publish-Subscribe
Push-Pull (Pipelining)
REQ REP
REP
REP
PUB
SUB
SUB
SUB
PUSH
PULL
PULL
PULL
![Page 100: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/100.jpg)
Message PatternsRequest-Reply Publish-Subscribe
Push-Pull (Pipelining)
REQ REP
REP
REP
PUB
SUB
SUB
SUB
PUSH
PULL
PULL
PULL
![Page 101: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/101.jpg)
Message PatternsRequest-Reply Publish-Subscribe
Push-Pull (Pipelining)
REQ REP
REP
REP
PUB
SUB
SUB
SUB
PUSH
PULL
PULL
PULL
![Page 102: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/102.jpg)
Message PatternsRequest-Reply Publish-Subscribe
Push-Pull (Pipelining)
REQ REP
REP
REP
PUB
SUB
SUB
SUB
PUSH
PULL
PULL
PULL
![Page 103: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/103.jpg)
Message PatternsRequest-Reply Publish-Subscribe
Push-Pull (Pipelining) Pair
REQ REP
REP
REP
PUB
SUB
SUB
SUB
PUSH
PULL
PULL
PULL
PAIR PAIR
![Page 104: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/104.jpg)
Devices
Queue Forwarder Streamer
Design architectures around devices.
![Page 105: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/105.jpg)
Devices
Queue Forwarder Streamer
Design architectures around devices.
REQ REP
![Page 106: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/106.jpg)
Devices
Queue Forwarder Streamer
Design architectures around devices.
PUB SUB
![Page 107: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/107.jpg)
Devices
Queue Forwarder Streamer
Design architectures around devices.
PUSH PULL
![Page 108: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/108.jpg)
Performance
![Page 109: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/109.jpg)
Performance
Orders of magnitude faster than most MQs
![Page 110: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/110.jpg)
Performance
Orders of magnitude faster than most MQsHigher throughput than raw sockets
![Page 111: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/111.jpg)
Performance
Orders of magnitude faster than most MQsHigher throughput than raw sockets
Intelligent message batching
![Page 112: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/112.jpg)
Performance
Orders of magnitude faster than most MQsHigher throughput than raw sockets
Intelligent message batchingEdge case optimizations
![Page 113: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/113.jpg)
Concurrency?"Come for the messaging, stay for the easy concurrency"
![Page 114: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/114.jpg)
Hintjens’ Law of Concurrency
e = mc2
E is effort, the pain that it takesM is mass, the size of the codeC is conflict, when C threads collide
![Page 115: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/115.jpg)
Hintjens’ Law of Concurrency
![Page 116: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/116.jpg)
Hintjens’ Law of Concurrency
![Page 117: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/117.jpg)
Hintjens’ Law of Concurrency
e=mc2, for c=1ZeroMQ:
![Page 118: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/118.jpg)
ZeroMQ
EasyCheapFastExpressive
Messaging toolkit for concurrency and distributed systems.
... familiar socket API
... lightweight queues in a library
... higher throughput than raw TCP
... maps to your architecture
![Page 119: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/119.jpg)
geventExecution model
![Page 120: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/120.jpg)
Threading vs EventedEvented seems to be preferred for scalable I/O applications
![Page 121: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/121.jpg)
Evented Stack
Non-blocking Code
Flow Control
I/O Abstraction
Reactor
Event Poller
I/OLoop
![Page 122: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/122.jpg)
def lookup(country, search_term): main_d = defer.Deferred()
def first_step(): query = "http://www.google.%s/search?q=%s" % (country,search_term) d = getPage(query) d.addCallback(second_step, country) d.addErrback(failure, country)
def second_step(content, country): m = re.search('<div id="?res.*?href="(?P<url>http://[^"]+)"', content, re.DOTALL) if not m: main_d.callback(None) return url = m.group('url') d = getPage(url) d.addCallback(third_step, country, url) d.addErrback(failure, country)
def third_step(content, country, url): m = re.search("<title>(.*?)</title>", content) if m: title = m.group(1) main_d.callback(dict(url = url, title = title)) else: main_d.callback(dict(url=url, title="{not-specified}"))
def failure(e, country): print ".%s FAILED: %s" % (country, str(e)) main_d.callback(None)
first_step() return main_d
123456789
10111213141516171819202122232425262728293031323334
![Page 123: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/123.jpg)
gevent
Reactor / Event Poller
Greenlets Monkey patching
“Regular” Python
![Page 124: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/124.jpg)
Green threads“Threads” implemented in user space (VM, library)
![Page 125: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/125.jpg)
Monkey patchingsocket, ssl, threading, time
![Page 126: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/126.jpg)
Twisted
![Page 127: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/127.jpg)
Twisted~400 modules
![Page 128: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/128.jpg)
gevent25 modules
![Page 132: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/132.jpg)
Building a Networking App#===# 1. Basic gevent TCP server
from gevent.server import StreamServer
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)tcp_server.serve_forever()
123456789
10111213
![Page 133: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/133.jpg)
#===# 2. Basic gevent TCP server and WSGI server
from gevent.pywsgi import WSGIServerfrom gevent.server import StreamServer
def handle_http(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)tcp_server.start()
http_server = WSGIServer(('127.0.0.1', 8080), handle_http)http_server.serve_forever()
123456789
10111213141516171819202122
![Page 134: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/134.jpg)
from gevent.pywsgi import WSGIServerfrom gevent.server import StreamServerfrom gevent.socket import create_connection
def handle_http(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
def client_connect(address): sockfile = create_connection(address).makefile() while True: line = sockfile.readline() # returns None on EOF if line is not None: print "<<<", line, else: break
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)tcp_server.start()
gevent.spawn(client_connect, ('127.0.0.1', 1234))
http_server = WSGIServer(('127.0.0.1', 8080), handle_http)http_server.serve_forever()
123456789
10111213141516171819202122232425262728293031
![Page 135: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/135.jpg)
from gevent.pywsgi import WSGIServerfrom gevent.server import StreamServerfrom gevent.socket import create_connection
def handle_http(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
def client_connect(address): sockfile = create_connection(address).makefile() while True: line = sockfile.readline() # returns None on EOF if line is not None: print "<<<", line, else: break
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)http_server = WSGIServer(('127.0.0.1', 8080), handle_http)greenlets = [ gevent.spawn(tcp_server.serve_forever), gevent.spawn(http_server.serve_forever), gevent.spawn(client_connect, ('127.0.0.1', 1234)),]gevent.joinall(greenlets)
123456789
1011121314151617181920212223242526272829303132
![Page 136: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/136.jpg)
ZeroMQ in gevent?
![Page 137: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/137.jpg)
![Page 138: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/138.jpg)
from gevent import spawnfrom gevent_zeromq import zmq
context = zmq.Context()
def serve(): socket = context.socket(zmq.REP) socket.bind("tcp://localhost:5559") while True: message = socket.recv() print "Received request: ", message socket.send("World")
server = spawn(serve)
def client(): socket = context.socket(zmq.REQ) socket.connect("tcp://localhost:5559") for request in range(10): socket.send("Hello") message = socket.recv() print "Received reply ", request, "[", message, "]"
spawn(client).join()
123456789
101112131415161718192021222324
![Page 139: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/139.jpg)
Actor model?Easy to implement, in whole or in part,
optionally with ZeroMQ
![Page 140: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/140.jpg)
![Page 141: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/141.jpg)
What is gevent missing?
![Page 142: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/142.jpg)
What is gevent missing?
Documentation
![Page 143: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/143.jpg)
What is gevent missing?
Documentation
Application framework
![Page 144: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/144.jpg)
gserviceApplication framework for gevent
![Page 145: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/145.jpg)
from gevent.pywsgi import WSGIServerfrom gevent.server import StreamServerfrom gevent.socket import create_connection
def handle_http(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
def client_connect(address): sockfile = create_connection(address).makefile() while True: line = sockfile.readline() # returns None on EOF if line is not None: print "<<<", line, else: break
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)http_server = WSGIServer(('127.0.0.1', 8080), handle_http)greenlets = [ gevent.spawn(tcp_server.serve_forever), gevent.spawn(http_server.serve_forever), gevent.spawn(client_connect, ('127.0.0.1', 1234)),]gevent.joinall(greenlets)
123456789
1011121314151617181920212223242526272829303132
![Page 146: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/146.jpg)
from gevent.pywsgi import WSGIServerfrom gevent.server import StreamServerfrom gevent.socket import create_connection
from gservice.core import Service
def handle_http(env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
def handle_tcp(socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
def client_connect(address): sockfile = create_connection(address).makefile() while True: line = sockfile.readline() # returns None on EOF if line is not None: print "<<<", line, else: break
app = Service()app.add_service(StreamServer(('127.0.0.1', 1234), handle_tcp))app.add_service(WSGIServer(('127.0.0.1', 8080), handle_http))app.add_service(TcpClient(('127.0.0.1', 1234), client_connect))app.serve_forever()
123456789
10111213141516171819202122232425262728293031
![Page 147: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/147.jpg)
from gservice.core import Servicefrom gservice.config import Setting
class MyApplication(Service): http_port = Setting('http_port') tcp_port = Setting('tcp_port') connect_address = Setting('connect_address') def __init__(self): self.add_service(WSGIServer(('127.0.0.1', self.http_port), self.handle_http)) self.add_service(StreamServer(('127.0.0.1', self.tcp_port), self.handle_tcp)) self.add_service(TcpClient(self.connect_address, self.client_connect)) def client_connect(self, address): sockfile = create_connection(address).makefile() while True: line = sockfile.readline() # returns None on EOF if line is not None: print "<<<", line, else: break def handle_tcp(self, socket, address): print 'new tcp connection!' while True: socket.send('hello\n') gevent.sleep(1)
def handle_http(self, env, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) print 'new http request!' return ["hello world"]
123456789
1011121314151617181920212223242526272829303132
![Page 148: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/148.jpg)
# example.conf.py
pidfile = 'example.pid'logfile = 'example.log'http_port = 8080tcp_port = 1234connect_address = ('127.0.0.1', 1234)
def service(): from example import MyApplication return MyApplication()
123456789
1011
# Run in the foregroundgservice -C example.conf.py
# Start service as daemongservice -C example.conf.py start
# Control servicegservice -C example.conf.py restartgservice -C example.conf.py reloadgservice -C example.conf.py stop
# Run with overriding configurationgservice -C example.conf.py -X 'http_port = 7070'
![Page 149: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/149.jpg)
Generalizinggevent proves a model that can be implemented in almost
any language that can implement an evented stack
![Page 150: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/150.jpg)
gevent
EasySmallFastCompatible
Futuristic evented platform for network applications.
... just normal Python
... only 25 modules
... top performing server
... works with most libraries
![Page 151: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/151.jpg)
RaidenLightning fast, scalable messaging
https://github.com/progrium/raiden
![Page 152: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/152.jpg)
Concurrency models
Traditional multithreading
Async or Evented I/O
Actor model
![Page 153: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/153.jpg)
Conclusion
Two very simple, but very powerful tools for distributed / concurrent systems
![Page 154: Distributed Systems - GitHub · Defines the “computational unit ... part of a distributed system that does not also need local concurrency. ... Messaging toolkit for concurrency](https://reader034.fdocuments.in/reader034/viewer/2022042223/5ec97e0fb216631abf278632/html5/thumbnails/154.jpg)
Thanks@progrium