Massaging the Pony:Message Queues and YouShawn RiderPBS EducationDjangoCon 2010http://www.flickr.com/photos/funtik/1175522045
What is a Message Queue?A system for enabling asynchronous processing of discrete tasks.Super awesome.http://www.flickr.com/photos/gadl/89650415
What are they good for?Alleviate server issues by offloading processinghttp://www.flickr.com/photos/islandgyrl/3197469932
What are they good for?Make better user experienceshttp://www.flickr.com/photos/bbaltimore/6779682
What are they good for?Increase the reliability of third party service integrations.
What are they good for?Background media processinghttp://www.flickr.com/photos/factoryjoe/2882992091
What are they good for? Repeating Tasks can replace Cron jobs  Keep all your code in your project
Available MQ SolutionsThere are many solutions out there. To name a few of them:RabbitMQAmazon Simple Queue SystemApache MQGearmanOpenAMQPeafowlQ4MStarlingSun Java System Message Queue
Message Queue ProtocolsAMQP: Advanced Message Queuing ProtocolJMS: Java Messaging ServiceSTOMP: Streaming Text Oriented Messaging Protocol
Criteria for Broker SelectionThe following criteria were important to us in selecting a solution. You may have some different needs.Handling exceptions and recoveryLow level of required maintenanceEase of deploymentPersistent queuingCommunity supportWhat language is it written in? How compatible is that with our current systems?Cluster support
Failsafe MQ BrokersMust be able to survive a server failure.Upon restarting the MQ Broker, processing should resume from the last un-processed messages in queues
Queue DurabilityThe Messages Queue Broker must be able to reconstruct the queues when it is restarted.http://www.flickr.com/photos/raul/846318014
Message PersistenceThe Messages (tasks to be completed) must persist between shutdowns and restarts of MQ Server
Our ChoiceCelery (a Django task management app)Carrot (Django middleware)PyAMQP (Python module)RabbitMQ (MQ Broker software)
The Production SystemWebappRabbitMQCeleryWorkerServer(s)Database
MQ in Development EnvironsIt is important to be able to develop Message Queue tasks in Django without being concerned about running the broker process, queue setup, persistence etc.
MQ in Development EnvironsCARROT_BACKEND = ‘qhettoq.taproot.Database’Carrot can be set up to use different queuing backend for development purposesGhettoQ provides database backend for carrot where a database serves as the queue storageThe broker is a simple Django application that monitors the queue in DBInefficient, therefore only suitable for development environments
Ease a future MQ implementationIsolate functionality into reusable components.
Recycle Existing Code
Django, HTTP Requests & MQMost functionalities provided throughDjangoapplications are tightly coupled with HTTP requests e.g. form data processingIn order to asynchronously execute a task, Celery (any task management platform) must serialize & store parametersHTTP requests are usually large and not easily serializable. e.g. WSGI requests
ThePickleableHTTPRequestObject
UsingPickleableHttpRequestif request.GET.get(‘download’):try:        result = EnrollmentCSVExportTask.delay(PickleableHttpRequest(                request,                 attributes=\                [‘user’, ‘admin_current_organization’]            )        )
Making Message Queue TasksIt is fast and easy to add a decorator to specific functions to create Message Queue tasks.from celery.decoratorsimport task@taskdef add(x, y):return x + y
Explicitly Inherit from Celery’s Task Object
Then What?Some Message Queue tasks can complete with no notification required.For other tasks:Notify by emailUse an AJAX listener to notify usersSome other messaging
In ReviewChoose your MQ solution wiselySet up a robust systemLeverage your existing codeReplace trouble spots as they come upProfit!
Message Queues Make Life Better
References and LinksSecond Life’s Survey of Message Queues:http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_NotesAsk SolemHoel’sGithub Repos:  http://github.com/askCelery Project: http://celeryproject.org/PickleableHttpRequest by NowellStrite (@nowells)http://gist.github.com/571027Special thanks to TarequeHossain (http://codexn.com)

Massaging the Pony: Message Queues and You

  • 1.
    Massaging the Pony:MessageQueues and YouShawn RiderPBS EducationDjangoCon 2010http://www.flickr.com/photos/funtik/1175522045
  • 2.
    What is aMessage Queue?A system for enabling asynchronous processing of discrete tasks.Super awesome.http://www.flickr.com/photos/gadl/89650415
  • 3.
    What are theygood for?Alleviate server issues by offloading processinghttp://www.flickr.com/photos/islandgyrl/3197469932
  • 4.
    What are theygood for?Make better user experienceshttp://www.flickr.com/photos/bbaltimore/6779682
  • 5.
    What are theygood for?Increase the reliability of third party service integrations.
  • 6.
    What are theygood for?Background media processinghttp://www.flickr.com/photos/factoryjoe/2882992091
  • 7.
    What are theygood for? Repeating Tasks can replace Cron jobs Keep all your code in your project
  • 8.
    Available MQ SolutionsThereare many solutions out there. To name a few of them:RabbitMQAmazon Simple Queue SystemApache MQGearmanOpenAMQPeafowlQ4MStarlingSun Java System Message Queue
  • 9.
    Message Queue ProtocolsAMQP:Advanced Message Queuing ProtocolJMS: Java Messaging ServiceSTOMP: Streaming Text Oriented Messaging Protocol
  • 10.
    Criteria for BrokerSelectionThe following criteria were important to us in selecting a solution. You may have some different needs.Handling exceptions and recoveryLow level of required maintenanceEase of deploymentPersistent queuingCommunity supportWhat language is it written in? How compatible is that with our current systems?Cluster support
  • 11.
    Failsafe MQ BrokersMustbe able to survive a server failure.Upon restarting the MQ Broker, processing should resume from the last un-processed messages in queues
  • 12.
    Queue DurabilityThe MessagesQueue Broker must be able to reconstruct the queues when it is restarted.http://www.flickr.com/photos/raul/846318014
  • 13.
    Message PersistenceThe Messages(tasks to be completed) must persist between shutdowns and restarts of MQ Server
  • 14.
    Our ChoiceCelery (aDjango task management app)Carrot (Django middleware)PyAMQP (Python module)RabbitMQ (MQ Broker software)
  • 15.
  • 16.
    MQ in DevelopmentEnvironsIt is important to be able to develop Message Queue tasks in Django without being concerned about running the broker process, queue setup, persistence etc.
  • 17.
    MQ in DevelopmentEnvironsCARROT_BACKEND = ‘qhettoq.taproot.Database’Carrot can be set up to use different queuing backend for development purposesGhettoQ provides database backend for carrot where a database serves as the queue storageThe broker is a simple Django application that monitors the queue in DBInefficient, therefore only suitable for development environments
  • 18.
    Ease a futureMQ implementationIsolate functionality into reusable components.
  • 19.
  • 20.
    Django, HTTP Requests& MQMost functionalities provided throughDjangoapplications are tightly coupled with HTTP requests e.g. form data processingIn order to asynchronously execute a task, Celery (any task management platform) must serialize & store parametersHTTP requests are usually large and not easily serializable. e.g. WSGI requests
  • 21.
  • 22.
    UsingPickleableHttpRequestif request.GET.get(‘download’):try: result = EnrollmentCSVExportTask.delay(PickleableHttpRequest( request, attributes=\ [‘user’, ‘admin_current_organization’] ) )
  • 23.
    Making Message QueueTasksIt is fast and easy to add a decorator to specific functions to create Message Queue tasks.from celery.decoratorsimport task@taskdef add(x, y):return x + y
  • 24.
    Explicitly Inherit fromCelery’s Task Object
  • 25.
    Then What?Some MessageQueue tasks can complete with no notification required.For other tasks:Notify by emailUse an AJAX listener to notify usersSome other messaging
  • 26.
    In ReviewChoose yourMQ solution wiselySet up a robust systemLeverage your existing codeReplace trouble spots as they come upProfit!
  • 27.
  • 28.
    References and LinksSecondLife’s Survey of Message Queues:http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_NotesAsk SolemHoel’sGithub Repos: http://github.com/askCelery Project: http://celeryproject.org/PickleableHttpRequest by NowellStrite (@nowells)http://gist.github.com/571027Special thanks to TarequeHossain (http://codexn.com)

Editor's Notes

  • #6 Example from TL about bulk refunds and paypal.
  • #8 Cron jobs essentially pass control of your code to the operating system. Although the Cron job is a valuable tool, it’s not the only option.
  • #9 Because a Message Queue Broker is a separate system that your project interfaces with, you could feasibly use many different apps created in many different languages.
  • #11 Cluster support will be a priority for a lot of people, but it was not a priority for us. (Cluster support is the ability to have a cluster of MQ Brokers.)
  • #12 To achieve this, we consider two concepts (on next slides).
  • #16 This is what that looks like in production. Many of these components exist on dedicated servers and/or server clusters.
  • #18 Some people who are more familiar with Carrot and Celery might wonder why not use the “always eager” setting, which processes MQ tasks in real time. However, in our experience that setting does not provide the effect we are after, and it is frustrating to get timeouts on long-running processes in development.
  • #20 You want to replace trouble spots in your project as they come up. No need to rewrite your entire project right away.
  • #22 The MockHTTP object always serializes GET and POST data. It then saves any additional HTTPRequest attributes specified when it is called.