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

The 12Factor App

The 12Factor App

Damien Mathieu

October 30, 2014
Tweet

More Decks by Damien Mathieu

Other Decks in Programming

Transcript

  1. @dmathieu
    Software Engineer @heroku
    The
    12Factor
    App
    1

    View Slide

  2. Twelve Factor
    • Can apply to any language and platform!
    • Speeds up deployment!
    • Makes scaling easier!
    • Keeps apps clean
    2

    View Slide

  3. Let’s take a look
    3

    View Slide

  4. One codebase
    to rule
    them all
    4

    View Slide

  5. Codebase
    Production
    Staging
    Feature A
    Feature B
    5

    View Slide

  6. 6
    Multiple
    codebases is a
    distributed
    system

    View Slide

  7. 7
    Create
    libraries

    View Slide

  8. Explicitly
    declare and
    isolate
    dependencies
    8

    View Slide

  9. 9
    source 'https://rubygems.org'
    !
    ruby '2.1.3'
    !
    gem 'rails'
    gem 'pg'
    gem 'puma'

    View Slide

  10. 10

    View Slide

  11. 11
    curl
    imagemagick

    View Slide

  12. 12
    Config never
    goes in
    source
    control

    View Slide

  13. Config
    Resource strings to databases
    Credentials to S3, Facebook,
    Twitter, …
    Security Tokens
    13

    View Slide

  14. $ cat .env!
    AWS_TOKEN=QWERTYUIOP!
    AWS_SECRET=12345 !
    GITHUB_APP_ID=1!
    GITHUB_SECRET=67890
    14

    View Slide

  15. $ foreman run rails console!
    >> puts ENV[‘AWS_TOKEN’]!
    > “QWERTYUIOP”
    15

    View Slide

  16. You can use
    a yml config
    file*
    *Just don’t store it in source control

    View Slide

  17. Treat backing
    services as
    attached
    resources
    17

    View Slide

  18. 18
    DATABASE_URL=

    postgres://example.com:5032

    View Slide

  19. Production deploy
    PostgreSQL Email Service Amazon S3

    View Slide

  20. 20
    One codebase per app
    Explicitly declare
    and isolate dependencies
    Don’t put config
    in source control
    Treat backing services
    as attached resources

    View Slide

  21. Build
    Release
    Run
    21

    View Slide

  22. Codebase
    Build
    Run Config
    22

    View Slide

  23. Codebase
    Build
    Run Config
    23

    View Slide

  24. Execute the
    app as one or
    more stateless
    processes
    24

    View Slide

  25. web.1 web.2
    Database
    25

    View Slide

  26. 26
    No sticky
    sessions

    View Slide

  27. 27
    Ephemeral
    filesystem

    View Slide

  28. 28
    Export
    services via
    port binding

    View Slide

  29. 29
    Self-contained
    app

    View Slide

  30. 30
    An app can be
    the backing
    service for an
    other

    View Slide

  31. Scale out
    via the
    process
    model
    31

    View Slide

  32. web.1
    web.2
    web.3
    worker.1
    worker.2
    clock.1
    32

    View Slide

  33. 33
    You can still
    do your own
    internal
    multiplexing

    View Slide

  34. 34
    Build, release, run
    One or more
    stateless processes
    Scale out via the
    process model
    Export services
    via port binding

    View Slide

  35. Maximize
    robustness with
    fast startup and
    graceful
    shutdown
    35

    View Slide

  36. 36
    Handle a
    request

    View Slide

  37. 37
    60s to boot
    is a lot

    View Slide

  38. 38
    Use the
    SIGTERM
    signal

    View Slide

  39. 39
    Send jobs
    back to the
    queue

    View Slide

  40. 40
    Build, release, run
    One or more stateless
    processes
    Scale out via the
    process model Fast startup

    View Slide

  41. Keep development,
    staging,
    and production
    as similar as
    possible
    41

    View Slide

  42. 42
    Use the same
    kind of
    machine

    View Slide

  43. 43
    With the
    same OS
    version

    View Slide

  44. 44
    And the same
    database
    server

    View Slide

  45. Logging
    45

    View Slide

  46. 46
    Production
    errors will
    happen

    View Slide

  47. 47
    Tail logs to
    find the
    cause

    View Slide

  48. 48
    Then use a
    logging
    service to
    dig in deeper

    View Slide

  49. action=create_user [email protected]
    action=log_user [email protected]
    49

    View Slide

  50. count#api-call-200=1
    50

    View Slide

  51. Don’t write
    logs to a
    file
    51

    View Slide

  52. Run admin/
    management
    tasks as
    one-off
    processes
    52

    View Slide

  53. 53
    Not in your
    app’s main
    processes

    View Slide

  54. 54
    Run console
    instances
    the same
    way

    View Slide

  55. 55
    Keep all your environments
    as similar as possible
    Run management tasks
    as one-off processes
    Logging

    View Slide

  56. Do you like?
    • Minimizing new developer overhead?!
    • Running in multiple environments?!
    • Easily scaling without tooling, architecture or
    development headaches?!
    • Having the latest update available to users at
    a moments notice?
    Read more at 12factor.net
    56

    View Slide

  57. Questions?
    [email protected]
    12factor.net
    57
    @dmathieu

    View Slide