Slide 1

Slide 1 text

Service Oriented Flask Building awesome software that scales. @rdegges

Slide 2

Slide 2 text

Yo, I’m Randall! “I’m just a happy programmer that likes to hack stuff.” Developer Evangelist, Stormpath

Slide 3

Slide 3 text

(I also founded / built an API company that handles billions of API requests.)

Slide 4

Slide 4 text

I <333 python.

Slide 5

Slide 5 text

“Uh, why did I attend this talk again?”

Slide 6

Slide 6 text

(And there are lots of ways to do it.) Scaling software is hard.

Slide 7

Slide 7 text

How to write awesome software that scales using SOA.

Slide 8

Slide 8 text

● What SOA is, exactly. ● Why SOA is awesome. ● The most import rule. ● How to build reliable services. ● How to make faster services. ● How to scale Flask services. ● The biggest SOA issue. ● How to have more fun. What I’ll Cover

Slide 9

Slide 9 text

0x00 What is SOA?

Slide 10

Slide 10 text

Big App Small Apps Website / API billing users api frontend

Slide 11

Slide 11 text

Users Service GET /users Returns a list of all user accounts. GET /users/ Returns the user identified by uid. POST /users Create a new user. POST /users/authenticate Authenticate a user. PUT /users/ Update a user account identified by uid. PATCH /users/ Update a user account identified by uid. DELETE /users/ Delete a user identified by uid.

Slide 12

Slide 12 text

Isolated

Slide 13

Slide 13 text

Database(s) Cache(s) Web Server(s) Load Balancer (s) Worker(s) Queue(s)

Slide 14

Slide 14 text

0x01 Why SOA?

Slide 15

Slide 15 text

Less scope, less problems.

Slide 16

Slide 16 text

Easily distribute work. billing users frontend api

Slide 17

Slide 17 text

Easier to scale. Database(s) Database(s)

Slide 18

Slide 18 text

Easier to test.

Slide 19

Slide 19 text

0x02 The Most Important Rule

Slide 20

Slide 20 text

“Keep it simple.”

Slide 21

Slide 21 text

PLAN YOUR ATTACK!

Slide 22

Slide 22 text

Write your docs -- first.

Slide 23

Slide 23 text

You may not need an API client.

Slide 24

Slide 24 text

0x03 How to Build Reliable Services

Slide 25

Slide 25 text

integration tests > unit tests

Slide 26

Slide 26 text

import urllib2 from flask import Flask from flask.ext.testing import LiveServerTestCase class MyTest(LiveServerTestCase): def create_app(self): app = Flask(__name__) app.config['TESTING'] = True app.config['LIVESERVER_PORT'] = 8943 return app def test_server_is_up_and_running(self): response = urllib2.urlopen(self.get_server_url()) self.assertEqual(response.code, 200)

Slide 27

Slide 27 text

Foreman / Docker Make Testing with SOA Easier web: gunicorn myproject:app -k gevent -b 0.0.0.0:$PORT servicea: cd ~/servicea && gunicorn myproject:app -k gevent -b 0.0.0.0:$PORT serviceb: cd ~/serviceb && gunicorn myproject:app -k gevent -b 0.0.0.0:$PORT $ foreman start 16:39:22 web.1 | started with pid 82834 16:39:22 worker.1 | started with pid 82835 16:39:22 blah.1 | started with pid 82836

Slide 28

Slide 28 text

“Keep development, staging and production as similar as possible.” Development Production

Slide 29

Slide 29 text

(postgression can help) $ curl -v http://api.postgression.com postgres://user:password@host/db Tests postgres

Slide 30

Slide 30 text

AUTO- DEPLOY YOUR CODE (or else)

Slide 31

Slide 31 text

Travis!

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Plan for downtime. Service B Service A

Slide 34

Slide 34 text

Monitor your stuff.

Slide 35

Slide 35 text

New Relic!

Slide 36

Slide 36 text

0x04 SOA Rules

Slide 37

Slide 37 text

Always use HTTPS! Free certs from StartSSL!

Slide 38

Slide 38 text

from flask import Flask from flask_sslify import SSLify app = Flask(__name__) sslify = SSLify(app) Flask-SSLify

Slide 39

Slide 39 text

Flask-BasicAuth from flask import Flask from flask.ext.basicauth import BasicAuth app = Flask(__name__) app.config['BASIC_AUTH_USERNAME'] = 'john' app.config['BASIC_AUTH_PASSWORD'] = 'matrix' app.config['BASIC_AUTH_FORCE'] = True basic_auth = BasicAuth(app)

Slide 40

Slide 40 text

There is only one serialization format: JSON.

Slide 41

Slide 41 text

Keep deployment simple. $ git push heroku master

Slide 42

Slide 42 text

0x05 Speeding Things Up

Slide 43

Slide 43 text

Your webserver isn’t slow.

Slide 44

Slide 44 text

Use caching layers.

Slide 45

Slide 45 text

Service A Code Cache CDN Clients

Slide 46

Slide 46 text

from flask import Flask from flask.ext.cache import Cache app = Flask(__name__) cache = Cache(app, config={'CACHE_TYPE': 'simple'}) Flask-Cache

Slide 47

Slide 47 text

0x06 Scaling Services

Slide 48

Slide 48 text

What’s your bottleneck? ● CPU? ● IO? ● Memory?

Slide 49

Slide 49 text

Scale vertically first, then horizontally.

Slide 50

Slide 50 text

Web ? Web Vertical Scaling

Slide 51

Slide 51 text

Web ? Web Web Web Horizontal Scaling

Slide 52

Slide 52 text

Put your data in the right place.

Slide 53

Slide 53 text

Dynamo Postgres Web Server 1ms 25ms

Slide 54

Slide 54 text

Queue Everything Service A Code Cache Queue Service B Code Cache Queue

Slide 55

Slide 55 text

from flask import Flask form flask.ext.rq import RQ, job app = Flask(__name__) RQ(app) @job def process(i): pass process.delay(3) Flask-RQ

Slide 56

Slide 56 text

Outsource what you can. ● Stormpath ● Twilio ● Loggly ● Sendgrid ● NewRelic ● Ducksboard Less code, less *.

Slide 57

Slide 57 text

billing users api frontend My Site logs stripe stormpath api frontend loggly

Slide 58

Slide 58 text

0x07 The Biggest SOA Problem

Slide 59

Slide 59 text

Users! billing users api frontend logs

Slide 60

Slide 60 text

frontend users username password submit ? Hmm.

Slide 61

Slide 61 text

Flask-Security ● Works with SQLAlchemy. ● Configurable. ● Password reset. ● Basic Authentication (with email / password). ● Token authentication. ● Self hosted. ● You have to build the API around it yourself.

Slide 62

Slide 62 text

frontend users Flask-Security username password submit Flow Flask-Login

Slide 63

Slide 63 text

from flask.ext.stormpath import StormpathManager app = Flask(__name__) stormpath_manager = StormpathManager(app) Flask-Stormpath

Slide 64

Slide 64 text

0x09 Having MORE Fun

Slide 65

Slide 65 text

Let’s Be Real Programming can be boringggg.

Slide 66

Slide 66 text

Own your code.

Slide 67

Slide 67 text

Release code.

Slide 68

Slide 68 text

Tell people about it.

Slide 69

Slide 69 text

Thank You randall@stormpath.com 818-217-9229