Creating Scalable JVM/Java Apps on Heroku

Preview:

Citation preview

Deploying Scaleable JVM Apps on HerokuJoe Kutner JVM Languages Owner at Heroku @codefinger

Java Scala

Groovy Clojure JRuby

.war

Traditional WAR file deployment…

Why WAR?

Use to introduce a

demo, video, Q&A, etc.

What does modern Java deployment look like?

Modern Java deployment is…

Containerless

Modern containerless deployment…

Let’s see it in action…

java -jar myapp.jar

java -jar jetty-runner.jar /my/app/

$ heroku deploy:war myapp.war

Use to introduce a

demo, video, Q&A, etc. The Twelve Factor App

The Twelve Factor App is…• a manifesto • a methodology • a collection of experiences

Its goals are…• scalability • maintainability • portability

Its does this through…declarative setup …explicit dependencies …external configuration

!clean contracts …between your app and its platform …reduced coupling !minimum divergence …between each deployment …between dev and prod environments

• Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes

Twelve Factors

• Codebase • Dependencies • Config • Backing services • Build, release, run • Processes

12factor.net

I. CodebaseOne codebase, many deploys

Test

Staging

Production

I. CodebaseOne codebase, many deploys

Test

Staging

Production

$ git push heroku-test master

$ git push heroku-stg master

$ git push heroku-prod master

II. DependenciesExplicitly declared

II. DependenciesExplicitly declared

<dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> </dependency>

II. DependenciesExplicitly declared

!libraryDependencies ++= Seq( "postgresql" % "postgresql" % "9.0-801.jdbc4" )

II. DependenciesExplicitly declared

<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> </dependency>

II. DependenciesExplicitly declared

libraryDependencies ++= Seq( "com.twitter" % "finagle-http" % "6.18.0", )

III. ConfigurationStore config in the environment, not the app

III. ConfigurationStore config in the environment, not the app

! <dbUrl>jdbc://postgres...</dbUrl>

III. ConfigurationStore config in the environment, not the app

URI dbUri = new URI(System.getenv(“DATABASE_URL"));!

III. ConfigurationStore config in the environment, not the app

$ heroku config:set JAVA_OPTS=“-Xmx512m”

IV. Backing ServicesTreat backing services as attachable resources

IV. Backing ServicesTreat backing services as attachable resources

IV. Backing ServicesTreat backing services as attachable resources

$ heroku addons:add papertrail

V. Build, release, run• A build step vendors dependencies, prepares assets, etc. • A release step creates a package from build and config. • A runtime step executes, without special knowledge.

V. Build, release, run

$ mvn package

V. Build, release, run

$ git push heroku master

V. Build, release, run

$ sbt stage deployHeroku

VI. ProcessesStateless

VI. ProcessesStateless

Don’t write to the local filesystem.

VII. Port binding• Export services via port binding • Self-contained, not relying on runtime injection by a web server

java -Dserver.port=$PORT -jar …

VIII. Concurrency• Scale out • Scale up

VIII. Concurrency• Scale out • Scale up

$ heroku ps:scale web=3

VIII. Concurrency

web.1

web.2

worker.1 clock.1

Workload Diversity

Num

ber o

f Pro

cess

es

worker.2

worker.3

IX. DisposabilityFast startup and graceful shutdown • Spring Boot and Play do this for you… • as opposed to big App Servers that take several minutes to boot

X. Dev/prod parityKeep dev, staging, prod and everything in between as similar as possible

X. Dev/prod parityKeep dev, staging, prod and everything in between as similar as possible

dev!

sqlite!

postgres

stage!

mysql!

postgres

prod!

postgres!

postgres

=!

≠!

=

=!

≠!

=

XI. LogsTreat logs as event streams

XI. LogsTreat logs as event streams

2014-09-10T22:38:43.427311+00:00 heroku[worker]: ... 2014-09-10T22:38:49.629790+00:00 heroku[web.1]: Starting process... 2014-09-10T22:39:22.056033+00:00 heroku[router]: at=info method=... 2014-09-10T22:38:43.427311+00:00 heroku[postgres]: ... 2014-09-10T22:38:43.427311+00:00 heroku[api]: Release v34 created... 2014-09-10T22:38:49.629790+00:00 heroku[web.1]: Starting process ... 2014-09-10T22:39:22.056033+00:00 heroku[router]: GET mysite.com/ ...

Event Stream

DB Web Worker

XII. Admin ProcessesAdmin and management tasks should be one-off processes

XII. Admin ProcessesAdmin and management tasks should be one-off processes

web1

web2

web3

admin

• Port binding • Concurrency • Disposability • Dev/prod parity • Logs • Admin processes

Twelve Factors• Codebase • Dependencies • Config • Backing services • Build, release, run • Processes

Further reading…!Heroku DevCenter: devcenter.heroku.com !Spring Boot Example: https://github.com/kissaten/spring-boot-executable !Link to slideshare !Twitter: @codefinger

Thank You!Joe Kutner JVM Languages Owner at Heroku @codefinger