KEMBAR78
Queue Everything and Please Everyone | PDF
Queue Everything
       &
Please Everyone
       ***

   Vaidik Kapoor
Who am I?
➔   Undergraduate student.
➔   Involved with:
    ✔   Mozilla as a Contributor and Rep
    ✔   Drupal as a Contributor
➔   Twitter: @vaidikkapoor
➔   Github: vaidikkp
➔   Web: vaidikkapoor.info
Web Applications
Responsiveness
Request                      Response
                      Client




    Task 1   Task 2   Task 3   Task N




Why do everything at once?
Break the Request-Response Cycle
       Request                   Response
                      Client




           Task 1              Task N




                    Task 2




                    Task 3
Meet Queues
What can Queues help with?
➔   Background Processing
    ✔   Data Processing
    ✔   Media Processing
    ✔   Updating Caches
➔   Anything that you want to offload off your
    server.
➔   Improve the overall User Experience
Redis
       +
HotQueue / PyRes
Redis + HotQueue
PRODUCER:

from hotqueue import HotQueue
queue = HotQueue("myqueue", host="localhost", port=6379, db=0)

queue.put(message)




CONSUMER / WORKER:

from hotqueue import HotQueue
queue = HotQueue("myqueue", host="localhost", port=6379, db=0)

while True:
     message = queue.get()
     # do something awesome
Redis + PyRes
➔   Python clone of Github's Resque
➔   Offers a lot more features than HotQueue
    ✔   Job failure handling
    ✔   Queue status information
➔   Comes with Monitoring System written in Ruby
Redis + PyRes
PRODUCER:

from pyres import ResQ
r = Resq()

class WebPage:
     queue = “screenies”

    @staticmethod
    def perform(urll):
         # save the screenshot

r.enqueue(WebPage, 'http://python.org')


CONSUMER:

./pyres_worker screenies
Message Queue
A system for enabling asynchronous processing
               of discrete tasks.
What can MQs help with?
➔   Everything that generic Queues can help with.
➔   Increase Reliability
➔   Cron Jobs (Celery)
Available MQ Solutions
➔   RabbitMQ
➔   Amazon Simple Queue
➔   Apache MQ
➔   Gearman
➔   Starling
➔   OpenAMQ
➔   Sun Java Message Queue System
Message Queue Protocols
➔   AMQP: Advanced Message Queue Protocol
➔   JMS: Java Messaging Service
➔   STOMP: Streaming Text Oriented Messaging
    Protocol
Criteria for Broker Selection
➔   Difficulty in Recovery
➔   Relatively low level of required maintenance
➔   Ease of Deployment
➔   Durability
➔   Persistence
➔   Community Support
➔   Cluster Support
➔   What language is it written in?
RabbitMQ
➔   A Message Broker
➔   Implements AMQP
Pika & Other AMQP Libraries
Celery
➔   An amazing Task Manager
➔   Batteries Included
➔   Uses RabbitMQ as broker (or almost anything)
➔   Libraries for most of the common web frameworks
    like Django, Flask, Pyramid
➔   Supports multiprocessing.
➔   Distribute tasks easily.
➔   Makes life easy
Celery Example
from celery.task import task

@task():
def take_screenshot(url):
  # magical code to take screenshot comes here
How to put it all together?
The General Setup

                   Task
Web App                        Broker
                  Manager




                  Worker
Database         Server (s)
Then what?
Notify Your Users
Django's Messaging System
Flask's Flashes
Poll using AJAX
WebSockets
Email
Things to Remember
Isolate
Isolate code, make reusable components.
Recycle
Remove Request Dependency
Unit Testing?
Review
➔   Choose a system according to needs
➔   Build a robust system
➔   Integrate seamlessly with your UI
➔   Isolate & Recycle: make it a habit
➔   Improve the overall UX!
➔   Please everyone, even yourself!
References
➔   PyRes - http://pyres.readthedocs.org/
➔   RabbitMQ - http://www.rabbitmq.com/
➔   Celery - http://celeryproject.org/
➔   Amazing Article -
    http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes
Thank You!

Queue Everything and Please Everyone

  • 1.
    Queue Everything & Please Everyone *** Vaidik Kapoor
  • 2.
    Who am I? ➔ Undergraduate student. ➔ Involved with: ✔ Mozilla as a Contributor and Rep ✔ Drupal as a Contributor ➔ Twitter: @vaidikkapoor ➔ Github: vaidikkp ➔ Web: vaidikkapoor.info
  • 3.
  • 4.
  • 5.
    Request Response Client Task 1 Task 2 Task 3 Task N Why do everything at once?
  • 6.
    Break the Request-ResponseCycle Request Response Client Task 1 Task N Task 2 Task 3
  • 7.
  • 8.
    What can Queueshelp with? ➔ Background Processing ✔ Data Processing ✔ Media Processing ✔ Updating Caches ➔ Anything that you want to offload off your server. ➔ Improve the overall User Experience
  • 9.
    Redis + HotQueue / PyRes
  • 10.
    Redis + HotQueue PRODUCER: fromhotqueue import HotQueue queue = HotQueue("myqueue", host="localhost", port=6379, db=0) queue.put(message) CONSUMER / WORKER: from hotqueue import HotQueue queue = HotQueue("myqueue", host="localhost", port=6379, db=0) while True: message = queue.get() # do something awesome
  • 11.
    Redis + PyRes ➔ Python clone of Github's Resque ➔ Offers a lot more features than HotQueue ✔ Job failure handling ✔ Queue status information ➔ Comes with Monitoring System written in Ruby
  • 12.
    Redis + PyRes PRODUCER: frompyres import ResQ r = Resq() class WebPage: queue = “screenies” @staticmethod def perform(urll): # save the screenshot r.enqueue(WebPage, 'http://python.org') CONSUMER: ./pyres_worker screenies
  • 13.
    Message Queue A systemfor enabling asynchronous processing of discrete tasks.
  • 14.
    What can MQshelp with? ➔ Everything that generic Queues can help with. ➔ Increase Reliability ➔ Cron Jobs (Celery)
  • 15.
    Available MQ Solutions ➔ RabbitMQ ➔ Amazon Simple Queue ➔ Apache MQ ➔ Gearman ➔ Starling ➔ OpenAMQ ➔ Sun Java Message Queue System
  • 16.
    Message Queue Protocols ➔ AMQP: Advanced Message Queue Protocol ➔ JMS: Java Messaging Service ➔ STOMP: Streaming Text Oriented Messaging Protocol
  • 17.
    Criteria for BrokerSelection ➔ Difficulty in Recovery ➔ Relatively low level of required maintenance ➔ Ease of Deployment ➔ Durability ➔ Persistence ➔ Community Support ➔ Cluster Support ➔ What language is it written in?
  • 18.
    RabbitMQ ➔ A Message Broker ➔ Implements AMQP
  • 19.
    Pika & OtherAMQP Libraries
  • 20.
    Celery ➔ An amazing Task Manager ➔ Batteries Included ➔ Uses RabbitMQ as broker (or almost anything) ➔ Libraries for most of the common web frameworks like Django, Flask, Pyramid ➔ Supports multiprocessing. ➔ Distribute tasks easily. ➔ Makes life easy
  • 21.
    Celery Example from celery.taskimport task @task(): def take_screenshot(url): # magical code to take screenshot comes here
  • 22.
    How to putit all together?
  • 23.
    The General Setup Task Web App Broker Manager Worker Database Server (s)
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
    Isolate Isolate code, makereusable components.
  • 33.
  • 34.
  • 35.
    Review ➔ Choose a system according to needs ➔ Build a robust system ➔ Integrate seamlessly with your UI ➔ Isolate & Recycle: make it a habit ➔ Improve the overall UX! ➔ Please everyone, even yourself!
  • 36.
    References ➔ PyRes - http://pyres.readthedocs.org/ ➔ RabbitMQ - http://www.rabbitmq.com/ ➔ Celery - http://celeryproject.org/ ➔ Amazing Article - http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes
  • 37.