87
“RabbitMQ python and ruby staring at the looking glass” - an asynchronous tale - Paolo Negri @hungryblank http://www.slideshare.net/hungryblank http://github.com/hungryblank/rabbitmq_rupy_2009

RabbitMQ with python and ruby RuPy 2009

Embed Size (px)

Citation preview

Page 1: RabbitMQ with python and ruby RuPy 2009

“RabbitMQpython and ruby staring

at the looking glass”- an asynchronous tale -

Paolo Negri @hungryblank

• http://www.slideshare.net/hungryblank

• http://github.com/hungryblank/rabbitmq_rupy_2009

Page 2: RabbitMQ with python and ruby RuPy 2009

Resources

• http://www.slideshare.net/hungryblank

• http://github.com/hungryblank/rabbitmq_rupy_2009

Page 3: RabbitMQ with python and ruby RuPy 2009

About meTime

GNU/Linux - Dbs“systems”

Perl

Python

Ruby

PHP

Page 4: RabbitMQ with python and ruby RuPy 2009

AMQP

• standard for high performance enterprise messaging

• totally open

• platform agnostic

• interoperable

Advanced Message Queuing Protocol

Page 5: RabbitMQ with python and ruby RuPy 2009

AMQP

“We use asynchronicity heavily in those cases where performance is an issue.

This is generally where we send content from one peer to another.  We send off methods as fast as possible without   waiting for confirmations”

AMQP Specs [0.8 June 2006]

Why is it interesting?

Page 6: RabbitMQ with python and ruby RuPy 2009

Summary

“AMQP as a case of asynchronous protocol implementation in python and ruby”

Page 7: RabbitMQ with python and ruby RuPy 2009

AMQP - Entities

ConsumerBroker

msgProducer

http://www.flickr.com/photos/17258892@N05/2588347668/

msg

Page 8: RabbitMQ with python and ruby RuPy 2009

AMQP - Entities

ConsumerBroker

msgProducer

http://www.flickr.com/photos/17258892@N05/2588347668/

msg

msgProducer

Consumer msg

msgProducer

Page 9: RabbitMQ with python and ruby RuPy 2009

AMQPImplementations

• Producer/Consumer: libs available for python and ruby

• Broker no python or ruby implementation

Page 10: RabbitMQ with python and ruby RuPy 2009

• RabbitMQ is an implementation of AMQP Broker

• It’s opensource

• Written in Erlang

Page 11: RabbitMQ with python and ruby RuPy 2009

Erlang?

• general-purpose concurrent programming language designed by Ericsson

• first version written by J. Armstrong in 1986

• distributed

• fault tolerant

• soft real time

• high availability

Page 12: RabbitMQ with python and ruby RuPy 2009

Erlang?Erlang's main strength is support for concurrency

“Process communication is done via a shared-nothing asynchronous message passing:

every process has a “mailbox”, a queue of messages sent by other processes, that are not yet consumed” (Wikipedia)

Page 13: RabbitMQ with python and ruby RuPy 2009

+ Erlang

It’s messages all the way down

Page 14: RabbitMQ with python and ruby RuPy 2009

Async Python libs

• “twisted AMQP”

• based on twisted python

• https://launchpad.net/txamqp

txAMQP

Page 15: RabbitMQ with python and ruby RuPy 2009

Async Python libs

• based on eventlet

• experimental, not ready for production

• http://bitbucket.org/edarc/amqpev

amqpev

Page 16: RabbitMQ with python and ruby RuPy 2009

Async Python libs

• based on asyncio

• young but mantained by member of RabbitMQ core team

• http://github.com/tonyg/pika

pika

Page 17: RabbitMQ with python and ruby RuPy 2009

Async Ruby libs

• amqp gem

• based on EventMachine

• mature and widely used

• http://github.com/tmm1/amqp/

amqp

Page 18: RabbitMQ with python and ruby RuPy 2009

Async?

• Twisted Python

• Asyncio (python stdlib)

• eventlet (python)

• EventMachine (ruby)

Page 19: RabbitMQ with python and ruby RuPy 2009

Reactor Pattern

• Non blocking IO and lightweight concurrency

• eliminate the complexities of high-performance threaded network programming

Page 20: RabbitMQ with python and ruby RuPy 2009

synchronous

code

networkoperation

use network operation

result

Free

code

networkoperation

use network operation

result

Callback

Free

Free

Timeasynchronous

process process

Page 21: RabbitMQ with python and ruby RuPy 2009

Not only reactor

Some of the libs provide more i.e. eventlet provides also coroutines

Page 22: RabbitMQ with python and ruby RuPy 2009

Python, pick one

• asyncio is part of python stdlib

• easy syntax

• developer member of RabbitMQ team

• fits on slides :-P

pika

Page 23: RabbitMQ with python and ruby RuPy 2009

Code! Producer (ruby)

Page 24: RabbitMQ with python and ruby RuPy 2009

Consumer (ruby)

Page 25: RabbitMQ with python and ruby RuPy 2009

Behind the scenes

msg Queue Consumer

Publisher

Exchange

Page 26: RabbitMQ with python and ruby RuPy 2009

What and where

RabbitMQ(Erlang)

TCP/IP

Producer(ruby/python)

Consumer(ruby/python)Queue Exchange

Page 27: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 28: RabbitMQ with python and ruby RuPy 2009

Producer (python)

TCP/IP connection

Page 29: RabbitMQ with python and ruby RuPy 2009

Producer (python)

TCP/IP connectionchannel 1

Page 30: RabbitMQ with python and ruby RuPy 2009

Producer (python)

TCP/IP connectionchannel 1

channel 2

Page 31: RabbitMQ with python and ruby RuPy 2009

Producer (python)

TCP/IP connectionchannel 1

channel 2

channel n

Page 32: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 33: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 34: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 35: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 36: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 37: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 38: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 39: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 40: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 41: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 42: RabbitMQ with python and ruby RuPy 2009

Producer (python)

Page 43: RabbitMQ with python and ruby RuPy 2009

Producer (python)

msg Queue Consumer

Publisher

Exchange

Page 44: RabbitMQ with python and ruby RuPy 2009

Producer (python)

msg Queue Consumer

Publisher

Exchange

key based routing

Page 45: RabbitMQ with python and ruby RuPy 2009

Producer (python)

msg Queue Consumer

Publisher

Exchange

binding by key

key based routing

Page 46: RabbitMQ with python and ruby RuPy 2009

Consumer (python)

Page 47: RabbitMQ with python and ruby RuPy 2009

Things go wrong

If a consumer has a problem we might lose one or more messages

http://www.flickr.com/photos/danzen/2288625136/

Page 48: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.rb

Page 49: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.rb

Page 50: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.rb

Page 51: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.py

Page 52: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.py

Page 53: RabbitMQ with python and ruby RuPy 2009

ACKing consumer.py

Page 54: RabbitMQ with python and ruby RuPy 2009

Achieved: decoupling

msg Queue Consumer

Publisher

Exchange

Page 55: RabbitMQ with python and ruby RuPy 2009

Achieved: easy work load distribution

msg A Queue

Consumer

Publisher

Exchange Consumer

Consumer

Page 56: RabbitMQ with python and ruby RuPy 2009

Achieved: easy work load distribution

Queue

Consumer

Publisher

Exchange Consumer

Consumer

msg A

Page 57: RabbitMQ with python and ruby RuPy 2009

Achieved: easy work load distribution

Queue

Consumer

Publisher

Exchange Consumer

Consumer

msg A

Page 58: RabbitMQ with python and ruby RuPy 2009

Achieved: easy work load distribution

Queue

Consumer

Publisher

Exchange Consumer

Consumer

msg A

Page 59: RabbitMQ with python and ruby RuPy 2009

prefetch?

http://www.flickr.com/photos/balakov/327431271

Page 60: RabbitMQ with python and ruby RuPy 2009

prefetch?

tcp ipbuffer

http://www.flickr.com/photos/balakov/327431271

Page 61: RabbitMQ with python and ruby RuPy 2009

prefetch - ruby

Page 62: RabbitMQ with python and ruby RuPy 2009

prefetch - ruby

Page 63: RabbitMQ with python and ruby RuPy 2009

prefetch - python

Page 64: RabbitMQ with python and ruby RuPy 2009

prefetch - python

Page 65: RabbitMQ with python and ruby RuPy 2009

More routing models

multicastingwith fanout exchange

Page 66: RabbitMQ with python and ruby RuPy 2009

Fanout producer.rb

Page 67: RabbitMQ with python and ruby RuPy 2009

Fanout producer.rb

Page 68: RabbitMQ with python and ruby RuPy 2009

Fanout consumer.rb

Page 69: RabbitMQ with python and ruby RuPy 2009

Fanout consumer.rb

Page 70: RabbitMQ with python and ruby RuPy 2009

Fanout consumer.rb

Page 71: RabbitMQ with python and ruby RuPy 2009

Fanout consumer.rb

Page 72: RabbitMQ with python and ruby RuPy 2009

Fanout

Queue1

Queue3

Queue2

Cons1

Cons2

Cons3

Producer

Exchangemsg A

Page 73: RabbitMQ with python and ruby RuPy 2009

Fanout

Queue1

Queue3

Queue2

Cons1

Cons2

Cons3

Producer

Exchangemsg A

Page 74: RabbitMQ with python and ruby RuPy 2009

Fanout

Queue1

Queue3

Queue2

Cons1

Cons2

Cons3

Producer

Exchange

msg A

msg A

msg A

Page 75: RabbitMQ with python and ruby RuPy 2009

Fanout producer.py

Page 76: RabbitMQ with python and ruby RuPy 2009

Fanout producer.py

Page 77: RabbitMQ with python and ruby RuPy 2009

Fanout producer.py

Page 78: RabbitMQ with python and ruby RuPy 2009

Fanout consumer.py

Page 79: RabbitMQ with python and ruby RuPy 2009

Exchanges

• Direct - based on binding routing key

• Fanout - always to all bound queues

• Topic - pattern matching on routing key

Page 80: RabbitMQ with python and ruby RuPy 2009

Clustering

RabbitMQ - node C

RabbitMQ - node A RabbitMQ - node B

TCP/IP

TCP/IPTCP/IP

Page 81: RabbitMQ with python and ruby RuPy 2009

Plugins

• introduced with v 1.7.0

• Add functionalities to the broker

• makes a interesting first erlang project

Page 82: RabbitMQ with python and ruby RuPy 2009

Ecosystem

• 65 RabbitMQ / AMQP projects on github

• + more on bitbucket, and others

Page 83: RabbitMQ with python and ruby RuPy 2009

Sync?

• easier to add to web apps

• suitable for non performance critical messaging

• useful to build cli tools

Page 84: RabbitMQ with python and ruby RuPy 2009

Sync?

• celldee / bunny - Ruby (github)

• famoseagle / carrot - Ruby (github)

• py-amqplib - Python (http://barryp.org/software/py-amqplib/)

• ...

Page 85: RabbitMQ with python and ruby RuPy 2009

Distributed systems

• ezmobious / nanite - Ruby (github)

• ask / carrot - Python (github)

• ask / celery - Python (github)

• auser / poolparty - Erlang (github)

Page 86: RabbitMQ with python and ruby RuPy 2009

Real time web

•RabbitHub PubSubHubBub

•dansimpson / amqp-js (github)

Page 87: RabbitMQ with python and ruby RuPy 2009

Thank you!

?