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

The 12Factor Django App

The 12Factor Django App

Short talk I gave at the 2012 Chicago Barcamp

Adam "Cezar" Jenkins

September 22, 2012
Tweet

Other Decks in Technology

Transcript

  1. What is 12factor The twelve-factor app is a methodology for

    building software- as-a-service apps that Sunday, September 23, 12
  2. Use declarative formats for setup automation, to minimize time and

    cost for new developers joining the project. Sunday, September 23, 12
  3. Have a clean contract with the underlying operating system, offering

    maximum portability between execution environments. Sunday, September 23, 12
  4. Are suitable for deployment on modern cloud platforms, obviating the

    need for servers and systems administration. Sunday, September 23, 12
  5. And can scale up without significant changes to tooling, architecture,

    or development practices. Sunday, September 23, 12
  6. I. Codebase What have we been doing? Sharing code between

    apps. Factor it out and make a library Keeping our code in rsynced tarballs Keeping a development repo and a production repo. Sunday, September 23, 12
  7. II. Dependencies Your dependencies should be spelled out. Never rely

    on a system dependency. E.g. , never assume Django is on the system. Sunday, September 23, 12
  8. II. Dependencies Your dependencies should be spelled out. Never rely

    on a system dependency. E.g. , never assume Django is on the system. Virtualenv and pip are your friend. Sunday, September 23, 12
  9. III. Config How do you handle .env environment variables? Development:

    $ touch project/.env $ echo "echo 'woah'" > project/.env $ cd project woah Autoenv: Sunday, September 23, 12
  10. III. Config Production: Use Honcho (python Foreman) web: python serve.py

    redis: redis-server Uses a Procfile Sunday, September 23, 12
  11. V. Build, release, run The build stage is a transform

    which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. Sunday, September 23, 12
  12. V. Build, release, run The build stage is a transform

    which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. The release stage takes the build produced by the build stage and combines it with the deploy’s current config. The resulting release contains both the build and the config and is ready for immediate execution in the execution environment. Sunday, September 23, 12
  13. V. Build, release, run The build stage is a transform

    which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. The release stage takes the build produced by the build stage and combines it with the deploy’s current config. The resulting release contains both the build and the config and is ready for immediate execution in the execution environment. The run stage (also known as “runtime”) runs the app in the execution environment, by launching some set of the app’s processes against a selected release. Sunday, September 23, 12
  14. VI. Processes Execute the app as one or more stateless

    processes $ python manage.py Sunday, September 23, 12
  15. VI. Processes Execute the app as one or more stateless

    processes $ python manage.py Processes share nothing Sunday, September 23, 12
  16. VII. Port binding Export services via port binding. ... run_gunicorn

    -b 0.0.0.0:$PORT Let nginx/apache handle routing to your app. Sunday, September 23, 12
  17. VIII. Concurrency The share-nothing, horizontally partitionable nature of twelve-factor app

    processes means that adding more concurrency is a simple and reliable operation. Sunday, September 23, 12
  18. VIII. Concurrency Twelve-factor app processes should never daemonize or write

    PID files. Instead, rely on the operating system’s process manager. Supervisor is very common in the Django world. Sunday, September 23, 12
  19. IX. Disposability The app should start quickly and shut down

    gracefully. SIGTERM causes shutdown Shutdown is handled by the process manager (supervisor) Sunday, September 23, 12
  20. IX. Disposability The app should start quickly and shut down

    gracefully. SIGTERM causes shutdown Shutdown is handled by the process manager (supervisor) The app should handle sudden death. Sunday, September 23, 12
  21. X. Dev/prod parity Keep development, staging, and production as similar

    as possible. Failing to do so increases: the time gap, personnel gap, and the tools gap. Sunday, September 23, 12
  22. X. Dev/prod parity Traditional app Twelve- factor app Time between

    deploys Weeks Hours Code authors vs code deployers Different People Same People Dev vs production environments Divergent As Similar as Possilbe Sunday, September 23, 12
  23. XI. Logs App never concerns itself with routing or storage

    of its output stream. Sunday, September 23, 12
  24. XI. Logs App never concerns itself with routing or storage

    of its output stream. Each process writes to stdout. Sunday, September 23, 12
  25. XI. Logs App never concerns itself with routing or storage

    of its output stream. Each process writes to stdout. The Python logger will work just fine Sunday, September 23, 12
  26. XI. Logs App never concerns itself with routing or storage

    of its output stream. Each process writes to stdout. The Python logger will work just fine Can see the whole ecosystem, searchable. Sunday, September 23, 12
  27. XI. Logs App never concerns itself with routing or storage

    of its output stream. Each process writes to stdout. The Python logger will work just fine Can see the whole ecosystem, searchable. OS decides where they go. Loggly, etc. Sunday, September 23, 12
  28. XI. Logs 2012-02-22T19:56:40+00:00 [postgres]: .... 2012-02-22T19:56:40+00:00 [router]: GET mysite.com/ ...

    2012-02-22T19:56:40+00:00 [nginx]: .... 2012-02-22T19:56:40+00:00 [worker]: .... Sunday, September 23, 12
  29. XII. Admin processes One off processes: manage.py syncdb Identical environment.

    Same code, config. Shipped with code. Sunday, September 23, 12