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

Rails Underground 2009 RabbitMQ

Rails Underground 2009 RabbitMQ

hungryblank

April 24, 2012
Tweet

More Decks by hungryblank

Other Decks in Programming

Transcript

  1. “Divide and conquer riding rabbits and trading gems” - a

    tale about distributed programming - Paolo Negri @hungryblank • http://www.slideshare.net/hungryblank • http://github.com/hungryblank/rabbit_starter
  2. The problem Given a dictionary of 1.000.000 search phrases, compare

    for each one the first page of results on google.com and bing.com
  3. Don’t do it. It’s a violation of terms of service!

    (Sorry, I needed an example that required little or no explanation)
  4. Great! but... • Fetching, parsing and analyzing 2.000.000 pages of

    results will take a long time • we want to collect the statistics in a limited time and this sequential approach is not quick enough
  5. Distributed Computing Is a method of solving computational problem by

    dividing the problem into many tasks run simultaneously on many hardware or software systems (Wikipedia)
  6. Map Reduce "Map" step the master node takes the input,

    chops it up into smaller sub-problems, and distributes those to worker nodes. (Wikipedia)
  7. What about queuing? • the master node takes the input,

    chops it up into smaller sub-problems, and publishes them in a queue • workers independently consume the content of the queue
  8. Here comes • RabbitMQ is an implementation of AMQP, the

    emerging standard for high performance enterprise messaging • It’s opensource • Written in Erlang
  9. 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
  10. Erlang - is coming back • Projects • CouchDB -

    RESTful document storage • WebMachine - REST toolkit • Nitrogen - web framework • Mochiweb - web framework • Ruby shops using it • 37 Signal - Campfire • Heroku
  11. Install it •sudo apt-get install rabbitmq •sudo gem install tmm1-amqp

    Note: rabbitMQ must be v1.6.0 and amqp gem v 0.6.4 to follow the code in the slides
  12. Get for free • Decoupling master/worker • Workers take care

    of feeding themselves • Flexible number of workers
  13. RabbitMQ • Multiple exchanges (and multiple types of exchange) •

    Multiple queues • Queues are connected by bindings to exchanges • Exchanges route messages to queues
  14. RabbitMQ • Exchanges and queues have names • BUT direct

    exchanges created implicitly are not public and don’t have name • Queues and messages are resident in RAM and can be persisted on disk (at a performance price)
  15. What and where RabbitMQ (Erlang) TCP/IP Master (ruby) Worker (ruby)

    Worker (ruby) Queue Exchange Worker (ruby)
  16. Problem #1 If a worker has a problem we might

    lose one or more messages http://www.flickr.com/photos/danzen/2288625136/
  17. Acknoledgement • messages - 1 or more depending by prefetch

    settings - are passed to the client • the client processes and acknowledges the messages one by one • if the client connection closes => unacknowledged messages go back in the queue
  18. EventMachine • Non blocking IO and lightweight concurrency • eliminate

    the complexities of high- performance threaded network programming Is an implementation of Reactor Pattern
  19. without EM code network operation use network operation result Free

    code network operation use network operation result Callback Free Free Time with EM Ruby process Ruby process
  20. EventMachine amqp gem is built on top of EventMachine =>

    you’re in a context where you can leverage concurrent programming
  21. EM - Deferrables “The Deferrable pattern allows you to specify

    any number of Ruby code blocks that will be executed at some future time when the status of the Deferrable object changes “
  22. Problem #3 How many of them? what are they doing?

    http://www.flickr.com/photos/philocrites/341850461/
  23. Rabbits on github • danielsdeleo/moqueue Tests/stubbing • Check out ezmobius/nanite

    “self assembling fabric of ruby daemons” • Keep an eye on tonyg/rabbithub implementation of pubsubhubbub (PubSub over REST) • auser/alice web app to monitor RabbitMQ