SlideShare a Scribd company logo
Celery for internal API in
  SOA infrastructure
        Roman Imankulov
     PyCon Russia, Feb 2013
1. Internal API. What's the deal
SOA service architecture example
1. Internal API. What's the deal

● DCOM: too Microsoftish
● CORBA: too Enterprisy
● SOAP: XML inside
● RESTful: too sloppy



Celery to the rescue?
Agenda
1. Internal API. What's the deal

2. How to use Celery for internal API

3. Organizing Celery-based API. Routing

4. Celery-based API. Benefits, pitfalls and

   security notes
2. How to use Celery for internal API
             Celery Worker
2. How to use Celery for internal API
  Classic celery client imports task function
2. How to use Celery for internal API
Classic celery client imports makes you:
● install everything
   ○ slows down installation

● import everything
   ○ python processes grow in size
   ○ circular dependencies
   ○ tight coupling

● share everything
   ○ ... with outsource developers
2. How to use Celery for internal API
            "Task implementation is not to
             be exposed without necessity"
                           William of Ockham
2. How to use Celery for internal API
  Low-level interface defines only task name
2. How to use Celery for internal API
Convenient API requires some boilerplate code
2. How to use Celery for internal API
Make it even more convenient with
introspection
2. How to use Celery for internal API
Introspection magic with celery-api




http://github.com/imankulov/celery-api
3. Celery-based API. Routing
3. Celery-based API. Routing
Creating API endpoint

1. Common package for all API endpoints
2. Every worker imports celery instance
3. Expose tasks for worker with
   CELERY_IMPORTS
4. Launch worker with
   celery worker -A module_name
                     -Q foo
                     -n foo
3. Celery-based API. Routing
Send tasks with

add.apply_async(queue='foo',
                args=(1, 1))
3. Celery-based API. Routing
Define routing rules
3. Celery-based API. Routing
Task in the wire
{ "body": <base64 encoded string with task name and args>,
  "properties": {
     "body_encoding": "base64",
     "delivery_info": {
        "priority": 0, "routing_key": "foo", "exchange":
"foo"
     },
     "delivery_mode": 2,
     "delivery_tag": <UUID>
  },
  ...}
3. Celery-based API. Routing
Task in the wire


     "delivery_info": {
       "priority": 0,
       "routing_key": "foo",
       "exchange": "foo"
     }
3. Celery-based API. Routing
AMQP for dummies
3. Celery-based API. Routing
Define all queues (API endpoints) you have
4. Celery-based API. Benefits
High level protocol.

Takes the responsibility for

                   data serialization
                   routing
                   exception handling
4. Celery-based API. Benefits
Support for wide range of brokers.

You can switch between brokers with no major
changes in your code

Brokers:
RabbitMQ Redis SQLAlchemy
Django MongoDB Amazon SQS
CouchDB Beanstalk
4. Celery-based API. Benefits
Built-in extra stuff which you were afraid to ever
dream of.

asynchronous execution        parallel execution
asynchronous parallel execution
delayed execution         throttling
automatic retrying of failed tasks
limiting the time of execution
autoscaling API inspection
public key cryptography (message signing)
4. Celery-based API. Pitfalls
Python-centric.


Celery is written in Python and meant to be
used with Python code exclusively
4. Celery-based API. Pitfalls
Increasing complexity.

Celery codebase          46k LOC
Kombu codebase           19k LOC

Compare
Flask codebase           10k LOC
Werkzeug codebase        28k LOC
Jinja2 codebase            16k LOC
4. Celery-based API. Security note




Never ever expose Celery broker to the
Web!
4. Celery-based API. Security note

1. Try
>>> redis_url = 'redis://:pycon2013@broker.imankulov.name'
>>> celery = Celery(broker=redis_url, result=redis_url)



2. Get the contents of /etc/pycon_secret

3. Exchange it for a secret prize ;)

More Related Content

PDF
Scaling up task processing with Celery
ODP
Introduction to Python Celery
PPTX
Asynchronous Task Queues with Celery
PDF
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
PDF
Introduction to Celery
PDF
Celery: The Distributed Task Queue
PDF
Data processing with celery and rabbit mq
PDF
Celery with python
Scaling up task processing with Celery
Introduction to Python Celery
Asynchronous Task Queues with Celery
Why and how Pricing Assistant migrated from Celery to RQ - Paris.py #2
Introduction to Celery
Celery: The Distributed Task Queue
Data processing with celery and rabbit mq
Celery with python

What's hot (20)

PDF
Celery introduction
PDF
Celery
PPTX
Controlling multiple VMs with the power of Python
PDF
Introduction to Ansible (Pycon7 2016)
PDF
Advanced task management with Celery
KEY
Php resque
KEY
Background Jobs with Resque
PDF
Background processing with Resque
PDF
Introducing Ansible
PPTX
Gruntwork Executive Summary
KEY
GPerf Using Jesque
PDF
Python tools for testing web services over HTTP
PPTX
Ansible module development 101
PDF
Ansible Crash Course
PDF
Puppet in the Pipeline
PDF
Getting Started with Ansible
PPTX
Infrastructure Automation with Chef & Ansible
PDF
Rhebok, High Performance Rack Handler / Rubykaigi 2015
PDF
What Is Async, How Does It Work, And When Should I Use It?
PDF
Javascript TDD with Jasmine, Karma, and Gulp
Celery introduction
Celery
Controlling multiple VMs with the power of Python
Introduction to Ansible (Pycon7 2016)
Advanced task management with Celery
Php resque
Background Jobs with Resque
Background processing with Resque
Introducing Ansible
Gruntwork Executive Summary
GPerf Using Jesque
Python tools for testing web services over HTTP
Ansible module development 101
Ansible Crash Course
Puppet in the Pipeline
Getting Started with Ansible
Infrastructure Automation with Chef & Ansible
Rhebok, High Performance Rack Handler / Rubykaigi 2015
What Is Async, How Does It Work, And When Should I Use It?
Javascript TDD with Jasmine, Karma, and Gulp
Ad

Viewers also liked (16)

PDF
Celery - A Distributed Task Queue
PDF
Microservices in the Enterprise: A Research Study and Reference Architecture
PDF
도커(Docker) 메트릭스 & 로그 수집
PDF
An Introduction to Celery
PDF
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
PDF
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
PDF
Word Embeddings - Introduction
PPTX
New Ceph capabilities and Reference Architectures
PPT
TeorÍa Del Consumo
PDF
왜 레진코믹스는 구글앱엔진을 선택했나
PDF
RestMQ - HTTP/Redis based Message Queue
PPTX
Deep Learning - Convolutional Neural Networks - Architectural Zoo
PDF
REST vs. Messaging For Microservices
PDF
20170227 파이썬으로 챗봇_만들기
PDF
Developing applications with a microservice architecture (SVforum, microservi...
PPTX
The Future of Everything
Celery - A Distributed Task Queue
Microservices in the Enterprise: A Research Study and Reference Architecture
도커(Docker) 메트릭스 & 로그 수집
An Introduction to Celery
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Word Embeddings - Introduction
New Ceph capabilities and Reference Architectures
TeorÍa Del Consumo
왜 레진코믹스는 구글앱엔진을 선택했나
RestMQ - HTTP/Redis based Message Queue
Deep Learning - Convolutional Neural Networks - Architectural Zoo
REST vs. Messaging For Microservices
20170227 파이썬으로 챗봇_만들기
Developing applications with a microservice architecture (SVforum, microservi...
The Future of Everything
Ad

Similar to Celery for internal API in SOA infrastructure (20)

PPTX
python_development.pptx
PDF
PDF
PyCon India 2012: Celery Talk
KEY
Django Celery
PDF
Python RESTful webservices with Python: Flask and Django solutions
PPTX
Build restful ap is with python and flask
PDF
Panoramic view of web APIs
PDF
Practical Celery
PDF
Gearman: A Job Server made for Scale
PDF
PHP projects beyond the LAMP stack
PDF
Celery by dummy
PDF
Gearman - Northeast PHP 2012
PDF
Django at Scale
PDF
Queue Everything and Please Everyone
PDF
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
PDF
How Optimizely Scaled its REST API with asyncio
PPT
Introduction to Django-Celery and Supervisor
ODP
Large Scale Processing with Django
PDF
Scalable Django Architecture
KEY
Scaling Rails With Torquebox Presented at JUDCon:2011 Boston
python_development.pptx
PyCon India 2012: Celery Talk
Django Celery
Python RESTful webservices with Python: Flask and Django solutions
Build restful ap is with python and flask
Panoramic view of web APIs
Practical Celery
Gearman: A Job Server made for Scale
PHP projects beyond the LAMP stack
Celery by dummy
Gearman - Northeast PHP 2012
Django at Scale
Queue Everything and Please Everyone
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
How Optimizely Scaled its REST API with asyncio
Introduction to Django-Celery and Supervisor
Large Scale Processing with Django
Scalable Django Architecture
Scaling Rails With Torquebox Presented at JUDCon:2011 Boston

Celery for internal API in SOA infrastructure

  • 1. Celery for internal API in SOA infrastructure Roman Imankulov PyCon Russia, Feb 2013
  • 2. 1. Internal API. What's the deal SOA service architecture example
  • 3. 1. Internal API. What's the deal ● DCOM: too Microsoftish ● CORBA: too Enterprisy ● SOAP: XML inside ● RESTful: too sloppy Celery to the rescue?
  • 4. Agenda 1. Internal API. What's the deal 2. How to use Celery for internal API 3. Organizing Celery-based API. Routing 4. Celery-based API. Benefits, pitfalls and security notes
  • 5. 2. How to use Celery for internal API Celery Worker
  • 6. 2. How to use Celery for internal API Classic celery client imports task function
  • 7. 2. How to use Celery for internal API Classic celery client imports makes you: ● install everything ○ slows down installation ● import everything ○ python processes grow in size ○ circular dependencies ○ tight coupling ● share everything ○ ... with outsource developers
  • 8. 2. How to use Celery for internal API "Task implementation is not to be exposed without necessity" William of Ockham
  • 9. 2. How to use Celery for internal API Low-level interface defines only task name
  • 10. 2. How to use Celery for internal API Convenient API requires some boilerplate code
  • 11. 2. How to use Celery for internal API Make it even more convenient with introspection
  • 12. 2. How to use Celery for internal API Introspection magic with celery-api http://github.com/imankulov/celery-api
  • 14. 3. Celery-based API. Routing Creating API endpoint 1. Common package for all API endpoints 2. Every worker imports celery instance 3. Expose tasks for worker with CELERY_IMPORTS 4. Launch worker with celery worker -A module_name -Q foo -n foo
  • 15. 3. Celery-based API. Routing Send tasks with add.apply_async(queue='foo', args=(1, 1))
  • 16. 3. Celery-based API. Routing Define routing rules
  • 17. 3. Celery-based API. Routing Task in the wire { "body": <base64 encoded string with task name and args>, "properties": { "body_encoding": "base64", "delivery_info": { "priority": 0, "routing_key": "foo", "exchange": "foo" }, "delivery_mode": 2, "delivery_tag": <UUID> }, ...}
  • 18. 3. Celery-based API. Routing Task in the wire "delivery_info": { "priority": 0, "routing_key": "foo", "exchange": "foo" }
  • 19. 3. Celery-based API. Routing AMQP for dummies
  • 20. 3. Celery-based API. Routing Define all queues (API endpoints) you have
  • 21. 4. Celery-based API. Benefits High level protocol. Takes the responsibility for data serialization routing exception handling
  • 22. 4. Celery-based API. Benefits Support for wide range of brokers. You can switch between brokers with no major changes in your code Brokers: RabbitMQ Redis SQLAlchemy Django MongoDB Amazon SQS CouchDB Beanstalk
  • 23. 4. Celery-based API. Benefits Built-in extra stuff which you were afraid to ever dream of. asynchronous execution parallel execution asynchronous parallel execution delayed execution throttling automatic retrying of failed tasks limiting the time of execution autoscaling API inspection public key cryptography (message signing)
  • 24. 4. Celery-based API. Pitfalls Python-centric. Celery is written in Python and meant to be used with Python code exclusively
  • 25. 4. Celery-based API. Pitfalls Increasing complexity. Celery codebase 46k LOC Kombu codebase 19k LOC Compare Flask codebase 10k LOC Werkzeug codebase 28k LOC Jinja2 codebase 16k LOC
  • 26. 4. Celery-based API. Security note Never ever expose Celery broker to the Web!
  • 27. 4. Celery-based API. Security note 1. Try >>> redis_url = 'redis://:[email protected]' >>> celery = Celery(broker=redis_url, result=redis_url) 2. Get the contents of /etc/pycon_secret 3. Exchange it for a secret prize ;)