Microservices
with Django
Florian Demmer - www.floriandemmer.com
... examples and thoughts
Django Friends Meetup, 2015-08-06
What? Why?
motivation
- separation of concerns
- from code reuse to service reuse
- scaling: performance by adding instances
- high availability?
- security?
microservices
- nothing new, actually: unix philosophy
- evolution: library vs. service
- service components
- independently deployed services
- communicating over high level API
kind of like Software as a Service
a don't-example
We want a centralized accounts service!
MongoDB + Flask = REST API
- API provides JSON of accounts model
- CRUD is not enough:
- /account/<id>/add_device
- /account/<id>/verify_email
- performance: single SQL query was replaced by multiple HTTP requests
a do-example
We want to send a newsletter!
let's buy <some SoA>
use it with Celery tasks and requests
- setup via Web UI
- send mail as JSON via HTTP/REST
- CRUD is enough
- performance:
no problem is done async
it depends...
does the problem really fit the solution?
- sending email is already async by design
- using an account to log in is not
so... security?
We want to keep the Internet out of our office CRM/CMS/...!
- Web UI for external users with access to limited dataset
- Web UI for internal office use with access to full sensitive data
the plan
2x Django + Celery (Redis)
- RPC characteristics
- Celery (async/sync, retry, worker queues)
- Redis ("good enough", multiple queues, JSON or pickle ...)
the code
# frontend service: webservice.py
@shared_task
def update_or_create_user(username, password):
...
# backend service: tasks.py
from celery import signature
update_or_create_user = signature('webservice.update_or_create_user')
# backend service: signals.py
from tasks import update_or_create_user
ref_id = update_or_create_user(username, make_password(password))
the horror!
- multiple git repositories
- sharing common code is hard
- keep communication endpoints clean
- multiple IDEs/vagrant boxes at once
- timeouts and failure
What do you think?
slides.com/fdemmer
flickr.com/fdemmer
@fdemmer
Django Friends Meetup, 2015-08-06
By Florian Demmer
Django Friends Meetup, 2015-08-06
- 1,391