Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Service Oriented Flask

Service Oriented Flask

OMG! Services! Flask! Yey!

Randall Degges

August 17, 2014
Tweet

More Decks by Randall Degges

Other Decks in Programming

Transcript

  1. Service Oriented Flask
    Building awesome software that scales.
    @rdegges

    View Slide

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

    View Slide

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

    View Slide

  4. I <333 python.

    View Slide

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

    View Slide

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

    View Slide

  7. How to write
    awesome
    software that
    scales using
    SOA.

    View Slide

  8. ● 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

    View Slide

  9. 0x00 What is SOA?

    View Slide

  10. Big App Small Apps
    Website / API
    billing
    users
    api
    frontend

    View Slide

  11. 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.

    View Slide

  12. Isolated

    View Slide

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

    View Slide

  14. 0x01 Why SOA?

    View Slide

  15. Less scope, less
    problems.

    View Slide

  16. Easily
    distribute
    work.
    billing
    users frontend
    api

    View Slide

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

    View Slide

  18. Easier to test.

    View Slide

  19. 0x02 The Most Important Rule

    View Slide

  20. “Keep it simple.”

    View Slide

  21. PLAN
    YOUR
    ATTACK!

    View Slide

  22. Write your
    docs -- first.

    View Slide

  23. You may not
    need an API
    client.

    View Slide

  24. 0x03 How to Build Reliable Services

    View Slide

  25. integration tests > unit tests

    View Slide

  26. 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)

    View Slide

  27. 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

    View Slide

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

    View Slide

  29. (postgression can help)
    $ curl -v http://api.postgression.com
    postgres://user:[email protected]/db
    Tests postgres

    View Slide

  30. AUTO-
    DEPLOY YOUR
    CODE
    (or else)

    View Slide

  31. Travis!

    View Slide

  32. View Slide

  33. Plan for downtime.
    Service B
    Service A

    View Slide

  34. Monitor your stuff.

    View Slide

  35. New Relic!

    View Slide

  36. 0x04 SOA Rules

    View Slide

  37. Always use
    HTTPS!
    Free certs from StartSSL!

    View Slide

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

    View Slide

  39. 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)

    View Slide

  40. There is only one
    serialization
    format: JSON.

    View Slide

  41. Keep deployment
    simple.
    $ git push heroku master

    View Slide

  42. 0x05 Speeding Things Up

    View Slide

  43. Your webserver
    isn’t slow.

    View Slide

  44. Use caching layers.

    View Slide

  45. Service A
    Code
    Cache
    CDN
    Clients

    View Slide

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

    View Slide

  47. 0x06 Scaling Services

    View Slide

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

    View Slide

  49. Scale vertically first,
    then horizontally.

    View Slide

  50. Web
    ? Web
    Vertical Scaling

    View Slide

  51. Web
    ?
    Web
    Web
    Web
    Horizontal Scaling

    View Slide

  52. Put your data in the
    right place.

    View Slide

  53. Dynamo Postgres
    Web Server
    1ms 25ms

    View Slide

  54. Queue Everything
    Service A
    Code
    Cache Queue
    Service B
    Code
    Cache Queue

    View Slide

  55. 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

    View Slide

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

    View Slide

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

    View Slide

  58. 0x07 The Biggest SOA Problem

    View Slide

  59. Users!
    billing
    users
    api
    frontend
    logs

    View Slide

  60. frontend users
    username
    password
    submit
    ?
    Hmm.

    View Slide

  61. 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.

    View Slide

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

    View Slide

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

    View Slide

  64. 0x09 Having MORE Fun

    View Slide

  65. Let’s Be Real
    Programming can be
    boringggg.

    View Slide

  66. Own your
    code.

    View Slide

  67. Release
    code.

    View Slide

  68. Tell people
    about it.

    View Slide

  69. Thank You
    [email protected]
    818-217-9229

    View Slide