際際滷

際際滷Share 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

What's hot (20)

Celery introduction
Celery introductionCelery introduction
Celery introduction
Ionel Mrie Cristian
Celery
CeleryCelery
Celery
Fatih Erikli
Controlling multiple VMs with the power of Python
Controlling multiple VMs with the power of PythonControlling multiple VMs with the power of Python
Controlling multiple VMs with the power of Python
Yurii Vasylenko
Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)
Ivan Rossi
Advanced task management with Celery
Advanced task management with CeleryAdvanced task management with Celery
Advanced task management with Celery
Mahendra M
Php resque
Php resquePhp resque
Php resque
Chaitanya Kuber
Background Jobs with Resque
Background Jobs with ResqueBackground Jobs with Resque
Background Jobs with Resque
homanj
Background processing with Resque
Background processing with ResqueBackground processing with Resque
Background processing with Resque
Nicolas Blanco
Introducing Ansible
Introducing AnsibleIntroducing Ansible
Introducing Ansible
Francesco Pantano
Gruntwork Executive Summary
Gruntwork Executive SummaryGruntwork Executive Summary
Gruntwork Executive Summary
Yevgeniy Brikman
GPerf Using Jesque
GPerf Using JesqueGPerf Using Jesque
GPerf Using Jesque
ctoestreich
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTP
Mykhailo Kolesnyk
Ansible module development 101
Ansible module development 101Ansible module development 101
Ansible module development 101
yfauser
Ansible Crash Course
Ansible Crash CourseAnsible Crash Course
Ansible Crash Course
Peter Sankauskas
Puppet in the Pipeline
Puppet in the PipelinePuppet in the Pipeline
Puppet in the Pipeline
Puppet
Getting Started with Ansible
Getting Started with AnsibleGetting Started with Ansible
Getting Started with Ansible
Ahmed AbouZaid
Infrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & AnsibleInfrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & Ansible
wajrcs
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Masahiro Nagano
What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?
emptysquare
Javascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and GulpJavascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and Gulp
All Things Open
Controlling multiple VMs with the power of Python
Controlling multiple VMs with the power of PythonControlling multiple VMs with the power of Python
Controlling multiple VMs with the power of Python
Yurii Vasylenko
Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)
Ivan Rossi
Advanced task management with Celery
Advanced task management with CeleryAdvanced task management with Celery
Advanced task management with Celery
Mahendra M
Background Jobs with Resque
Background Jobs with ResqueBackground Jobs with Resque
Background Jobs with Resque
homanj
Background processing with Resque
Background processing with ResqueBackground processing with Resque
Background processing with Resque
Nicolas Blanco
Gruntwork Executive Summary
Gruntwork Executive SummaryGruntwork Executive Summary
Gruntwork Executive Summary
Yevgeniy Brikman
GPerf Using Jesque
GPerf Using JesqueGPerf Using Jesque
GPerf Using Jesque
ctoestreich
Python tools for testing web services over HTTP
Python tools for testing web services over HTTPPython tools for testing web services over HTTP
Python tools for testing web services over HTTP
Mykhailo Kolesnyk
Ansible module development 101
Ansible module development 101Ansible module development 101
Ansible module development 101
yfauser
Puppet in the Pipeline
Puppet in the PipelinePuppet in the Pipeline
Puppet in the Pipeline
Puppet
Getting Started with Ansible
Getting Started with AnsibleGetting Started with Ansible
Getting Started with Ansible
Ahmed AbouZaid
Infrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & AnsibleInfrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & Ansible
wajrcs
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Masahiro Nagano
What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?
emptysquare
Javascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and GulpJavascript TDD with Jasmine, Karma, and Gulp
Javascript TDD with Jasmine, Karma, and Gulp
All Things Open

Viewers also liked (16)

Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
Duy Do
Microservices in the Enterprise: A Research Study and Reference Architecture
Microservices in the Enterprise: A Research Study and Reference ArchitectureMicroservices in the Enterprise: A Research Study and Reference Architecture
Microservices in the Enterprise: A Research Study and Reference Architecture
Jesus Rodriguez
貉(Docker) 覃碁Ν & 襦蠏 讌
貉(Docker) 覃碁Ν & 襦蠏 讌貉(Docker) 覃碁Ν & 襦蠏 讌
貉(Docker) 覃碁Ν & 襦蠏 讌
Daegwon Kim
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to Celery
Idan Gazit
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK MeetupScaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Kacper Gunia
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Apache Spark - Intro to Large-scale recommendations with Apache Spark and PythonApache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Christian Perone
Word Embeddings - Introduction
Word Embeddings - IntroductionWord Embeddings - Introduction
Word Embeddings - Introduction
Christian Perone
New Ceph capabilities and Reference Architectures
New Ceph capabilities and Reference ArchitecturesNew Ceph capabilities and Reference Architectures
New Ceph capabilities and Reference Architectures
Kamesh Pemmaraju
Teora Del ConsumoTeora Del Consumo
Teora Del Consumo
albarrason
讌貊覩轟る 蟲蠍煙讌
 讌貊覩轟る 蟲蠍煙讌  讌貊覩轟る 蟲蠍煙讌
讌貊覩轟る 蟲蠍煙讌
襴 螳
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
Gleicon Moraes
Deep Learning - Convolutional Neural Networks - Architectural Zoo
Deep Learning - Convolutional Neural Networks - Architectural ZooDeep Learning - Convolutional Neural Networks - Architectural Zoo
Deep Learning - Convolutional Neural Networks - Architectural Zoo
Christian Perone
REST vs. Messaging For Microservices
REST vs. Messaging For MicroservicesREST vs. Messaging For Microservices
REST vs. Messaging For Microservices
Eberhard Wolff
20170227 伎朱 豈覺_襷り鍵
20170227 伎朱 豈覺_襷り鍵20170227 伎朱 豈覺_襷り鍵
20170227 伎朱 豈覺_襷り鍵
Kim Sungdong
Developing applications with a microservice architecture (SVforum, microservi...
Developing applications with a microservice architecture (SVforum, microservi...Developing applications with a microservice architecture (SVforum, microservi...
Developing applications with a microservice architecture (SVforum, microservi...
Chris Richardson
The Future of Everything
The Future of EverythingThe Future of Everything
The Future of Everything
Michael Ducy
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
Duy Do
Microservices in the Enterprise: A Research Study and Reference Architecture
Microservices in the Enterprise: A Research Study and Reference ArchitectureMicroservices in the Enterprise: A Research Study and Reference Architecture
Microservices in the Enterprise: A Research Study and Reference Architecture
Jesus Rodriguez
貉(Docker) 覃碁Ν & 襦蠏 讌
貉(Docker) 覃碁Ν & 襦蠏 讌貉(Docker) 覃碁Ν & 襦蠏 讌
貉(Docker) 覃碁Ν & 襦蠏 讌
Daegwon Kim
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to Celery
Idan Gazit
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK MeetupScaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
Kacper Gunia
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Apache Spark - Intro to Large-scale recommendations with Apache Spark and PythonApache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Apache Spark - Intro to Large-scale recommendations with Apache Spark and Python
Christian Perone
Word Embeddings - Introduction
Word Embeddings - IntroductionWord Embeddings - Introduction
Word Embeddings - Introduction
Christian Perone
New Ceph capabilities and Reference Architectures
New Ceph capabilities and Reference ArchitecturesNew Ceph capabilities and Reference Architectures
New Ceph capabilities and Reference Architectures
Kamesh Pemmaraju
Teora Del ConsumoTeora Del Consumo
Teora Del Consumo
albarrason
讌貊覩轟る 蟲蠍煙讌
 讌貊覩轟る 蟲蠍煙讌  讌貊覩轟る 蟲蠍煙讌
讌貊覩轟る 蟲蠍煙讌
襴 螳
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
Gleicon Moraes
Deep Learning - Convolutional Neural Networks - Architectural Zoo
Deep Learning - Convolutional Neural Networks - Architectural ZooDeep Learning - Convolutional Neural Networks - Architectural Zoo
Deep Learning - Convolutional Neural Networks - Architectural Zoo
Christian Perone
REST vs. Messaging For Microservices
REST vs. Messaging For MicroservicesREST vs. Messaging For Microservices
REST vs. Messaging For Microservices
Eberhard Wolff
20170227 伎朱 豈覺_襷り鍵
20170227 伎朱 豈覺_襷り鍵20170227 伎朱 豈覺_襷り鍵
20170227 伎朱 豈覺_襷り鍵
Kim Sungdong
Developing applications with a microservice architecture (SVforum, microservi...
Developing applications with a microservice architecture (SVforum, microservi...Developing applications with a microservice architecture (SVforum, microservi...
Developing applications with a microservice architecture (SVforum, microservi...
Chris Richardson
The Future of Everything
The Future of EverythingThe Future of Everything
The Future of Everything
Michael Ducy

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

Developing Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & PythonDeveloping Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & Python
SmartBear
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
apidays
Build your APIs with apigility
Build your APIs with apigilityBuild your APIs with apigility
Build your APIs with apigility
Christian Varela
DEVNET-1128 Cisco Intercloud Fabric NB Api's for Business & Providers
DEVNET-1128	Cisco Intercloud Fabric NB Api's for Business & ProvidersDEVNET-1128	Cisco Intercloud Fabric NB Api's for Business & Providers
DEVNET-1128 Cisco Intercloud Fabric NB Api's for Business & Providers
Cisco DevNet
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Lucas Jellema
Using OSGi for script deployment in Apache Sling
Using OSGi for script deployment in Apache SlingUsing OSGi for script deployment in Apache Sling
Using OSGi for script deployment in Apache Sling
Radu Cotescu
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdfMastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Steve Wortham
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
Tatiana Al-Chueyr
Introducing the All New RESTful API for API Management
Introducing the All New RESTful API for API ManagementIntroducing the All New RESTful API for API Management
Introducing the All New RESTful API for API Management
WSO2
Coding 100-session-slides
Coding 100-session-slidesCoding 100-session-slides
Coding 100-session-slides
Cisco DevNet
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Gavin Pickin
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJS
Luigi Saetta
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure FunctionsSharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
S辿bastien Levert
Apigility-Powered APIs on IBM i
Apigility-Powered APIs on IBM iApigility-Powered APIs on IBM i
Apigility-Powered APIs on IBM i
chukShirley
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
ru_Parallels
Hacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sitesHacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sites
Mikhail Egorov
Resting with OroCRM Webinar
Resting with OroCRM WebinarResting with OroCRM Webinar
Resting with OroCRM Webinar
Oro Inc.
API SECURITY
API SECURITYAPI SECURITY
API SECURITY
Tubagus Rizky Dharmawan
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄 Kong
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄  KongK仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄  Kong
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄 Kong
Alex Demchenko
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Tom Johnson
Developing Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & PythonDeveloping Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & Python
SmartBear
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
2022 apidays LIVE Helsinki & North_Using OpenAPI to configure your API Gateway
apidays
Build your APIs with apigility
Build your APIs with apigilityBuild your APIs with apigility
Build your APIs with apigility
Christian Varela
DEVNET-1128 Cisco Intercloud Fabric NB Api's for Business & Providers
DEVNET-1128	Cisco Intercloud Fabric NB Api's for Business & ProvidersDEVNET-1128	Cisco Intercloud Fabric NB Api's for Business & Providers
DEVNET-1128 Cisco Intercloud Fabric NB Api's for Business & Providers
Cisco DevNet
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Part 2 of the REAL Webinars on Oracle Cloud Native Application Development (J...
Lucas Jellema
Using OSGi for script deployment in Apache Sling
Using OSGi for script deployment in Apache SlingUsing OSGi for script deployment in Apache Sling
Using OSGi for script deployment in Apache Sling
Radu Cotescu
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdfMastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Mastering Cypress API Testing_ A Comprehensive Guide with Examples.pdf
Steve Wortham
Introducing the All New RESTful API for API Management
Introducing the All New RESTful API for API ManagementIntroducing the All New RESTful API for API Management
Introducing the All New RESTful API for API Management
WSO2
Coding 100-session-slides
Coding 100-session-slidesCoding 100-session-slides
Coding 100-session-slides
Cisco DevNet
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin PickinItb 2021 - Bulding Quick APIs by Gavin Pickin
Itb 2021 - Bulding Quick APIs by Gavin Pickin
Gavin Pickin
Rest API with Swagger and NodeJS
Rest API with Swagger and NodeJSRest API with Swagger and NodeJS
Rest API with Swagger and NodeJS
Luigi Saetta
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure FunctionsSharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
SharePoint Fest Seattle - SharePoint Framework, Angular & Azure Functions
S辿bastien Levert
Apigility-Powered APIs on IBM i
Apigility-Powered APIs on IBM iApigility-Powered APIs on IBM i
Apigility-Powered APIs on IBM i
chukShirley
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
仂从仍舒亟 亳舒亳仍舒 亞仂仂于舒 仆舒 PHDays
ru_Parallels
Hacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sitesHacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sites
Mikhail Egorov
Resting with OroCRM Webinar
Resting with OroCRM WebinarResting with OroCRM Webinar
Resting with OroCRM Webinar
Oro Inc.
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄 Kong
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄  KongK仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄  Kong
K仂仆亳亠仄 仄亳从仂亠于亳仆 舒亳亠从 仆舒 Symfony 亳 于磶于舒亠仄 Kong
Alex Demchenko
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Tom Johnson

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://: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 ;)