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

12 Factors of Pain and Suffering

12 Factors of Pain and Suffering

Want to build simple, scalable and reliable web applications? In this presentation, you'll learn all about the 12-Factor App, and the principles it teaches.

You'll also see firsthand how even an experienced programmer has screwed up these best practices in every way imaginable, sometimes with terrible consequences.

Randall Degges

November 14, 2019
Tweet

More Decks by Randall Degges

Other Decks in Programming

Transcript

  1. @rdegges
    @rdegges
    12 Factors of
    PAIN and SUFFERING
    Sad Programming Stories

    View Slide

  2. @rdegges
    HEY, I'm Randall.
    Head of DevRel @ OKTA
    Python/JS/Go Hacker
    Open Source *
    Author
    Builder

    View Slide

  3. @rdegges
    The 12 Factor App
    "A methodology for building simple,
    scalable, and dependable web applications."

    View Slide

  4. @rdegges
    I fucked up.

    View Slide

  5. @rdegges
    I. Codebase
    One codebase tracked in revision
    control, many deploys.
    Code
    Prod
    Stag
    Dev1
    Dev2

    View Slide

  6. @rdegges
    818-217-9229 - Randall Degges
    - Granada Hills, CA 94519
    - He is *not* a spam bot
    - Etc...
    $$$
    I hate your website.
    CODEBASE
    newsketchdomain.com
    CODEBASE1 CODEBASE2
    domain1.com
    domain2.com
    CTRL-C
    +
    CTRL-V

    View Slide

  7. @rdegges

    View Slide

  8. @rdegges
    II. Dependencies
    Explicitly declare and isolate
    dependencies.

    View Slide

  9. @rdegges
    How It Works
    {
    "dependencies": {
    "axios": "^0.18.0",
    "dotenv": "^6.1.0",
    "express": "^4.16.4",
    "vue": "^2.5.17"
    }
    }
    Declare Dependencies
    $ npm install
    + [email protected]
    + [email protected]
    + [email protected]
    + [email protected]
    added 57 packages from 41 contributors and audited
    128 packages in 2.064s
    found 0 vulnerabilities
    Isolate Dependencies

    View Slide

  10. @rdegges
    Image
    Optimization API
    Shrink this image!
    OK! Let me optimize
    that for you.
    VPS
    - optipng
    - jpegoptim
    - gifsicle
    - ...
    code
    optipng
    gifsicle

    View Slide

  11. @rdegges
    III. Config
    Store config in the environment
    DATABASE_URL
    MAX_THREADS
    API_KEY
    PRICE

    View Slide

  12. @rdegges
    Asterisk
    Fuuuuuuuuuuuuuck.
    Phone calls
    SMS
    Etc.
    Rackspace
    API Key
    Code
    GitHub!
    - $25,000

    View Slide

  13. @rdegges
    IV. Backing Services
    Treat backing services as attached
    resources.
    Production
    Deploy
    Postgres
    S3 SMTP
    smtp://[email protected]
    https://[email protected]
    postgres://[email protected]/db

    View Slide

  14. @rdegges
    Blog
    MySQL
    I should make a blog.
    I should give out pirated
    movies to everyone.
    ERROR: No disk space
    MySQL

    View Slide

  15. @rdegges
    V. Build, Release, Run
    Strictly separate build and run
    stages.
    Code
    Build
    Config
    Release

    View Slide

  16. @rdegges
    WEbsite Code
    $ scp -r my_project [email protected]:
    Web Server
    $ ssh [email protected]
    (root) $ vim my_project/admin.php

    View Slide

  17. @rdegges

    View Slide

  18. @rdegges
    VI. Processes
    Execute the app as one or more
    stateless processes.

    View Slide

  19. @rdegges
    Conference
    Call App
    Datacenter
    Conference
    Call App
    Conference
    Call App
    818-217-9229
    555-444-3333
    925-431-1337

    View Slide

  20. @rdegges
    VII. Port Binding
    Expose services via port binding.
    0.0.0.0:80
    localhost:443
    Routing
    App $PORT
    App $PORT
    App $PORT

    View Slide

  21. @rdegges
    VIII. Concurrency
    Scale out via the process model.
    web.1
    web.2
    worker.1
    worker.2
    worker.3
    worker.4
    clock.1
    Process Types
    Scale

    View Slide

  22. @rdegges
    IRC Web App
    daemon();
    process
    process
    process
    process
    IRC Room
    IRC Room
    IRC Room

    View Slide

  23. @rdegges
    IX. Disposability
    Maximize robustness with fast
    startup and graceful shutdown.
    Shut down gracefully when SIGTTERM is received
    Shut down within a few seconds
    Be robust against sudden death

    View Slide

  24. @rdegges
    Process 1
    Load
    Balancer
    Requests
    Process 2
    Process 3
    Process 4
    Process 5
    def startup():
    download_audio()
    listen(env["PORT"])

    View Slide

  25. @rdegges
    X. Dev/Prod Parity
    Keep development, staging, and
    production as similar as possible.
    Code
    Postgres
    SQLite
    VARCHARRRRR

    View Slide

  26. @rdegges
    XI. Logs
    Treat logs as event streams.
    App







    ...
    Order by timestamp
    Write to STDOUT
    Consist of *all* process and backing services logs

    View Slide

  27. @rdegges
    Conference
    Call Web App
    Conference
    Call API
    Asterisk
    Asterisk
    Asterisk





    ...
    calls.csv
    $ scp [email protected]:calls.csv .
    Log Router

    View Slide

  28. @rdegges
    XII. Admin Processes
    Run admin/management tasks
    as one-off processes.
    Code
    Admin
    Task
    Run a database migration
    $ python manage.py migrate
    Prod
    Infrastructure

    View Slide

  29. @rdegges
    Run a database migration
    New Feature
    Alter Table DB Migration Code
    "I only need to do this
    once… So..."
    $ python manage.py migrate
    Deploy Feature!

    View Slide

  30. @rdegges
    @rdegges
    Thank You
    rdegges.com
    developer.okta.com
    @oktadev

    View Slide