Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

12 Factor Scala

857 views

Published on

Slides from ScalaDays 2015 Amsterdam

Published in: Internet
  • Hello there! Get Your Professional Job-Winning Resume Here! http://bit.ly/topresum
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

12 Factor Scala

  1. 1. 12 Factor App Best Practices for Scala Deployment
  2. 2. 2005 2015 WAR files JAR files App Servers Microservices Java Scala Hot-Deploy Continuous Deploy
  3. 3. Joe Kutner @codefinger JVM Platform Owner @Heroku Joe Kutner
  4. 4. 12 Factor App a methodology Scalability Maintainability Portability
  5. 5. • Immutable • Ephemeral • Declarative • Automated
  6. 6. https://github.com/sbt/sbt-native-packager
  7. 7. addSbtPlugin( "com.typesafe.sbt" % "sbt-native-packager" % "0.7.6" ) project/plugins.sbt
  8. 8. $ sbt stage
  9. 9. without further ado…
  10. 10. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  11. 11. Thank You! Goodbye! (just kidding)
  12. 12. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  13. 13. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  14. 14. Use Version Control
  15. 15. BAD
  16. 16. BAD App #1 App #2
  17. 17. my-project ! build.sbt ! app ! conf ! public ! my-library ! build.sbt ! src ! main ! scala
  18. 18. my-project ! build.sbt ! app ! conf ! public ! my-library ! my-sub-project ! build.sbt ! src ! main ! scala BAD
  19. 19. Submodules
  20. 20. $ git submodule add https://github.com/jkutner/play-sub-project
  21. 21. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  22. 22. Don’t check JAR files into Git
  23. 23. Never rely on implicit existence of system-wide packages Explicitly declare and isolate dependencies
  24. 24. global local BAD GOOD Dependencies ~/.m2 ~/.ivy2 (ok in dev) target/
  25. 25. Vendoring GOOD
  26. 26. $ sbt stage ... $ tree target/universal/stage/lib/ target/universal/stage/lib/ "## ch.qos.logback.logback-classic-1.1.1.jar "## ch.qos.logback.logback-core-1.1.1.jar "## com.fasterxml.jackson.core.jackson-annotations-2.3... "## com.fasterxml.jackson.core.jackson-core-2.3.2.jar "## com.fasterxml.jackson.core.jackson-databind-2.3.2.jar "## com.google.guava.guava-16.0.1.jar ...
  27. 27. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  28. 28. Don’t check passwords into Git
  29. 29. Or was it “DUH”? 😳
  30. 30. Can you make your app open source at any moment, without compromising any credentials? Litmus Test
  31. 31. • Resource handles to the database, Memcached, and other backing services • Credentials to external services such as Amazon S3 or Twitter • Per-deploy values such as the canonical hostname for the deploy Anything that changes between deployment environments: (does not include things like conf/routes) Configuration is…
  32. 32. Configuration belongs in the environment, not in the application Configuration should be strictly separated from code
  33. 33. db.default.url=${DATABASE_URL} conf/application.conf
  34. 34. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  35. 35. db.default.url=${DATABASE_URL} conf/application.conf
  36. 36. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  37. 37. build, release, run
  38. 38. $ sbt stage ... $ sbt deployHeroku ... # in the cloud! $ target/universal/stage/bin/my-app build release run
  39. 39. $ sbt run BAD (in production)
  40. 40. simple build tool
  41. 41. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  42. 42. DEMO!
  43. 43. $ sbt stage ... $ sbt deployHeroku ... # in the cloud! $ target/universal/stage/bin/scaladays build release run
  44. 44. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  45. 45. Processes should be stateless
  46. 46. sticky sessions 😞
  47. 47. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  48. 48. The twelve-factor app is completely self-contained The web app exports HTTP as a service by binding to a port
  49. 49. .war Traditional Deployment
  50. 50. .jar Modern Deployment
  51. 51. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  52. 52. Actors Futures Agents RELAX BRO, I GOT THIS…
  53. 53. Scale Up Scale Out
  54. 54. web.1 web.2 worker.1 clock.1 Workload Diversity NumberofProcesses worker.2 worker.3
  55. 55. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  56. 56. Graceful shutdown Quick startup Resilience to failure
  57. 57. Servers are not pets Servers are cattle
  58. 58. Application Servers are not disposable
  59. 59. Microservices are disposable
  60. 60. Easy to replace Decoupled from external infrastructure Easy to modify
  61. 61. Microservices
  62. 62. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  63. 63. dev sqlite postgres stage mysql postgres prod postgres postgres = ≠ = = ≠ =
  64. 64. dev jetty jetty stage tomcat jetty prod jboss jetty = ≠ = = ≠ =
  65. 65. dev jetty {} stage tomcat {} prod jboss {} = ≠ = = ≠ =
  66. 66. Dropwizard
  67. 67. disposable reproducible parity
  68. 68. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  69. 69. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  70. 70. Admin tasks should be run in isolated processes
  71. 71. $ heroku run console Running `console` attached to terminal... up, run.2581 Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCode... Failed to created JLineReader: java.lang.NoClassDefFou... Falling back to SimpleReader. Welcome to Scala version 2.11.1 (OpenJDK 64-Bit Server... Type in expressions to have them evaluated. Type :help for more information. scala>
  72. 72. web1 web2 web3 admin
  73. 73. $ heroku run sbt console ?
  74. 74. simple build tool
  75. 75. addSbtPlugin( "com.typesafe.sbt" % "sbt-native-packager" % "0.7.6" ) project/plugins.sbt
  76. 76. console: target/universal/stage/bin/my-app -main scala.tools.nsc.MainGenericRunner -usejavacp Procfile worker: target/universal/stage/bin/my-app -main com.example.MyWorker
  77. 77. The 12 Factors • Codebase • Dependencies • Config • Backing services • Build, release, run • Processes • Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes
  78. 78. http://12factor.net http://jkutner.github.io
  79. 79. Add sbt-native-packager1. Run `sbt stage`2. Deploy to Heroku?3. Go to the sbt-native-packager talk4. What next?
  80. 80. Joe Kutner @codefinger JVM Platform Owner @Heroku http://www.slideshare.net/jkutner/12-factor-scala

×