Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Scaling Django with Distributed Systems
Andrew Godwin
April 07, 2017
Programming
3
1.6k
Scaling Django with Distributed Systems
A talk I gave at PyCon Ukraine 2017.
Andrew Godwin
April 07, 2017
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
89
Async, Python, and the Future
andrewgodwin
1
370
How To Break Django: With Async
andrewgodwin
1
290
Taking Django's ORM Async
andrewgodwin
0
340
The Long Road To Asynchrony
andrewgodwin
0
380
The Scientist & The Engineer
andrewgodwin
1
370
Pioneering Real-Time
andrewgodwin
0
150
Just Add Await: Retrofitting Async Into Django
andrewgodwin
2
1.1k
Terrain, Art, Python and LiDAR
andrewgodwin
1
230
Other Decks in Programming
See All in Programming
mrubyを1300円のボードで動かそう
yuuu
0
180
2022 FrontEnd Training
mixi_engineers
1
280
C言語でメモリ管理を考えた話
hkawai
0
190
Monadic Java
mariofusco
4
260
スモールチームがAmazon Cognitoでコスパよく作るサービス間連携認証
tacke_jp
2
350
Reinventing the wheel ... as a service
mariofusco
3
260
Git Rebase
bkuhlmann
7
1k
Android入門
hn410
0
310
Jetpack Compose 頑張らないPreviewParameterProvider
horie23
0
100
クリエイティブ系のウェブサイト制作で役立つCSS技法 / CSS for develop creative website
clockmaker
2
1.5k
코드 품질 1% 올리기
pluu
1
960
Learning DDD輪読会#4 / Learning DDD Book Club #4
suzushin54
1
130
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
319
19k
WebSockets: Embracing the real-time Web
robhawkes
57
5k
How to name files
jennybc
39
58k
GraphQLの誤解/rethinking-graphql
sonatard
24
6.2k
Atom: Resistance is Futile
akmur
255
20k
What the flash - Photography Introduction
edds
61
9.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
238
11k
Design by the Numbers
sachag
271
17k
The World Runs on Bad Software
bkeepers
PRO
56
5.2k
jQuery: Nuts, Bolts and Bling
dougneiner
56
6.4k
Happy Clients
brianwarren
89
5.5k
Principles of Awesome APIs and How to Build Them.
keavy
113
15k
Transcript
None
Andrew Godwin Hi, I'm Django core developer Senior Software Engineer
at Used to complain about migrations a lot
Distributed Systems
c = 299,792,458 m/s
Early CPUs c = 60m propagation distance Clock ~2cm 5
MHz
Modern CPUs c = 10cm propagation distance 3 GHz
Distributed systems are made of independent components
They are slower and harder to write than synchronous systems
But they can be scaled up much, much further
Trade-offs
There is never a perfect solution.
Fast Good Cheap
None
Load Balancer WSGI Worker WSGI Worker WSGI Worker
Load Balancer WSGI Worker WSGI Worker WSGI Worker Cache
Load Balancer WSGI Worker WSGI Worker WSGI Worker Cache Cache
Cache
Load Balancer WSGI Worker WSGI Worker WSGI Worker Database
CAP Theorem
Partition Tolerant Consistent Available
PostgreSQL: CP Consistent everywhere Handles network latency/drops Can't write if
main server is down
Cassandra: AP Can read/write to any node Handles network latency/drops
Data can be inconsistent
It's hard to design a product that might be inconsistent
But if you take the tradeoff, scaling is easy
Otherwise, you must find other solutions
Read Replicas (often called master/slave) Load Balancer WSGI Worker WSGI
Worker WSGI Worker Replica Replica Main
Replicas scale reads forever... But writes must go to one
place
If a request writes to a table it must be
pinned there, so later reads do not get old data
When your write load is too high, you must then
shard
Vertical Sharding Users Tickets Events Payments
Horizontal Sharding Users 0 - 2 Users 3 - 5
Users 6 - 8 Users 9 - A
Both Users 0 - 2 Users 3 - 5 Users
6 - 8 Users 9 - A Events 0 - 2 Events 3 - 5 Events 6 - 8 Events 9 - A Tickets 0 - 2 Tickets 3 - 5 Tickets 6 - 8 Tickets 9 - A
Both plus caching Users 0 - 2 Users 3 -
5 Users 6 - 8 Users 9 - A Events 0 - 2 Events 3 - 5 Events 6 - 8 Events 9 - A Tickets 0 - 2 Tickets 3 - 5 Tickets 6 - 8 Tickets 9 - A User Cache Event Cache Ticket Cache
Teams have to scale too; nobody should have to understand
eveything in a big system.
Services allow complexity to be reduced - for a tradeoff
of speed
Users 0 - 2 Users 3 - 5 Users 6
- 8 Users 9 - A Events 0 - 2 Events 3 - 5 Events 6 - 8 Events 9 - A Tickets 0 - 2 Tickets 3 - 5 Tickets 6 - 8 Tickets 9 - A User Cache Event Cache Ticket Cache User Service Event Service Ticket Service
User Service Event Service Ticket Service WSGI Server
Each service is its own, smaller project, managed and scaled
separately.
But how do you communicate between them?
Service 2 Service 3 Service 1 Direct Communication
Service 2 Service 3 Service 1 Service 4 Service 5
Service 2 Service 3 Service 1 Service 4 Service 5
Service 6 Service 7 Service 8
Service 2 Service 3 Service 1 Message Bus Service 2
Service 3 Service 1
A single point of failure is not always bad -
if the alternative is multiple, fragile ones
Channels and ASGI provide a standard message bus built with
certain tradeoffs
Backing Store e.g. Redis, RabbitMQ ASGI (Channel Layer) Channels Library
Django Django Channels Project
Backing Store e.g. Redis, RabbitMQ ASGI (Channel Layer) Pure Python
Failure Mode At most once Messages either do not arrive,
or arrive once. At least once Messages arrive once, or arrive multiple times
Guarantees vs. Latency Low latency Messages arrive very quickly but
go missing more Low loss rate Messages are almost never lost but arrive slower
Queuing Type First In First Out Consistent performance for all
users First In Last Out Hides backlogs but makes them worse
Queue Sizing Finite Queues Sending can fail Infinite queues Makes
problems even worse
You must understand what you are making (This is surprisingly
uncommon)
Design as much as possible around shared-nothing
Per-machine caches On-demand thumbnailing Signed cookie sessions
Has to be shared? Try to split it
Has to be shared? Try sharding it.
Django's job is to be slowly replaced by your code
Just make sure you match the API contract of what
you're replacing!
Don't try to scale too early; you'll pick the wrong
tradeoffs.
Thanks. Andrew Godwin @andrewgodwin channels.readthedocs.io