Chapter 110% of the exam45 min
Heroku Platform
How Heroku builds and runs apps: buildpacks, slugs, releases, dynos, the Procfile, config vars and the router; the ways to deploy, from Git to pipelines and review apps; scaling for traffic spikes with dyno tiers, autoscaling, background workers, caching and connection pooling; and building with add-ons, buildpacks and Heroku Buttons from the Elements Marketplace.
By the end of this chapter you can
- Demonstrate an understanding of the basics behind building and deploying apps on Heroku
- Architect scalable solutions that are performant even during high-traffic spikes
- Architect and deploy solutions using add-ons and other components from the Elements Marketplace, including various add-ons, buildpacks, and Heroku Buttons
How Heroku builds and runs apps
Scenario: A ticketing company moves its app to Heroku. The architect explains what happens between git push and a running app.
From code to dynos
| Step | What happens |
|---|---|
| Source | Code, a dependency file (such as package.json) and a Procfile |
| Build | A buildpack installs dependencies and produces a slug (or an OCI image with Cloud Native Buildpacks) |
| Release | The build plus config vars and add-ons, numbered v1, v2 and so on |
| Run | The dyno manager runs the release in dynos, lightweight Linux containers |
| Route | The router sends HTTP requests to web dynos, which listen on $PORT |
- The Procfile declares process types:
webgets HTTP traffic,releaseruns before each release, and others such asworkerstart when you scale them. - Config vars are environment variables. Changing one creates a new release and restarts the app.
- Each dyno has an ephemeral filesystem: files written there vanish on restart and aren't visible to other dynos.
- Dynos restart at least once a day, and on every deploy. On shutdown they get SIGTERM, then SIGKILL after 30 seconds.
- Logplex collects logs from all dynos and the router, but keeps only the latest 1,500 lines.
- One-off dynos (
heroku run) run admin tasks with the same code and config.
Ways to deploy
| Method | Fits |
|---|---|
Heroku Git (git push heroku main) |
Simple deploys; keep the real repository elsewhere |
| GitHub integration | Manual or automatic deploys from a branch, optionally after CI passes |
Container Registry or heroku.yml |
Teams that must ship Docker images; you maintain the OS layer |
| Pipelines | Review, staging and production apps; promote the same build without rebuilding |
| Review apps | A temporary app for every pull request, defined in app.json |
| Heroku CI | Tests on every push in a throwaway app |
- Stacks are the base operating system images; Heroku-24 is the default.
- Generations: Heroku's original platform is Cedar, which runs the Common Runtime and Private Spaces. The newer Fir generation runs only in Fir Private Spaces so far, and doesn't yet support features such as autoscaling, Heroku CI and Heroku Buttons.
Exam tip: "A file saved by the app is gone after a restart" is the ephemeral filesystem. "Production must run exactly what was tested in staging" is pipeline promotion.
Scaling for traffic spikes
Scenario: The ticketing app sells out in minutes. Last time, requests timed out after 30 seconds and the database ran out of connections.
Dyno tiers
| Tier | Scale out | Zero-downtime deploys | Autoscaling | Notes |
|---|---|---|---|---|
| Eco | No | No | No | Sleeps after 30 minutes without web traffic; personal apps only |
| Basic | No | No | No | Never sleeps; one dyno per process type |
| Standard (1X, 2X) | Yes | Preboot | No | Shared compute |
| Performance (M to 2XL) | Yes | Preboot | Yes | Dedicated compute |
| Private and Shield | Yes | Rolling deploys | Yes | Only in Private or Shield Spaces |
Techniques
| Technique | Use it when |
|---|---|
Horizontal scaling (heroku ps:scale web=6) |
More requests at the same time |
| Vertical scaling (bigger dynos) | Each process needs more memory or CPU |
| Autoscaling | Web traffic rises and falls. It covers web dynos only, on Performance, Private and Shield, aiming at a target p95 response time |
| Worker dynos and a queue | Slow work such as PDFs, email or external calls. The web dyno queues the job and replies at once |
| Concurrency in each dyno | A concurrent web server (threads or processes) serves more requests per dyno |
| Caching and a CDN | Repeated reads and static files; Heroku has no HTTP cache of its own |
| Followers and connection pooling | The database is the bottleneck. Adding dynos can make it worse |
Router limits
- The router waits 30 seconds for the first byte of a response, then returns H12. This isn't configurable, and the dyno keeps working, so set a shorter timeout in the app.
- When too many requests queue for an app, the router returns H11 "Backlog too deep". Fix it with more concurrency, faster requests, then more dynos.
- Upload large files straight to object storage, not through the dyno.
Safe releases and testing
- Preboot starts new web dynos before switching traffic on the Common Runtime; rolling deploys replace dynos in batches in Private Spaces. Run at least two web dynos.
- Load-test before big events. On the Common Runtime, tests above 10,000 requests per second need Heroku's approval in advance.
Exam tip: "Requests fail after exactly 30 seconds" is the router timeout: move the slow work to a worker. "Autoscale the workers" isn't native: Heroku's autoscaling covers web dynos only, so use an add-on.
The Elements Marketplace
Scenario: The ticketing team needs logging, email, a CDN and a PDF tool, and wants a one-click way for partners to deploy a demo.
Add-ons
- Add-ons are services such as databases, logging, monitoring, email and search, run by Heroku or by partners. Provision them from Elements, the Dashboard or the CLI (
heroku addons:create service:plan). - Provisioning sets config vars, such as
DATABASE_URL, and restarts the app. Providers may change these values, so read them at runtime. - An add-on can be attached to other apps with the same owner (
heroku addons:attach), up to 100 apps. Billing stays with the owning app.detachremoves one attachment;destroyremoves the add-on everywhere. - In Private Spaces, some add-ons run inside the space; others are only "compatible" and run outside it, over the internet.
Buildpacks
- Official buildpacks cover Ruby, Node.js, Python, Java, PHP, Go, Scala, Clojure and .NET, written as
heroku/<language>. Third-party buildpacks aren't supported by Heroku; review them before use. - Use several buildpacks for mixed apps, such as Node.js assets in a Ruby app. Put the primary language last, because the last buildpack sets the default process types.
- Classic buildpacks produce slugs; Cloud Native Buildpacks produce OCI images and are the only kind on Fir.
Heroku Buttons and app.json
- A Heroku Button deploys an app from a GitHub repository with one click, using the
app.jsonfile in its root. app.jsondescribes config vars, add-ons, buildpacks, dyno formation and scripts. It's also used by review apps and Heroku CI.app.jsonis read only when an app is created, and Button apps don't update when the repository changes.
Exam tip: "Partners need a one-click demo deployment" is a Heroku Button with
app.json. "Two apps need the same database" means attaching the add-on, not copying its URL.
Hands-on exercise
Prepare a ticketing app for a sales spike
Design deployment, scaling and add-ons for an app that faces sudden traffic peaks.
Use a free Salesforce Developer Edition org — never a production org.
0 of 5 steps done
How to check your work: PDF generation moves to worker dynos fed by a queue (Key-Value Store), so web requests return in well under 30 seconds; uploads go to object storage because dyno filesystems are ephemeral; web dynos move to Performance with autoscaling on p95 response time and a concurrent web server; static assets go through a CDN; the database gets connection pooling and read followers; deployments use a pipeline that promotes the tested build, with preboot for zero downtime; the team load-tests in advance, asking Heroku first above 10,000 requests per second.
Domain quiz: Heroku Platform
10 questions from the 21 in this domain, different each time and checked as you go. Anything you miss is saved to Mistakes review.
- Questions
- 10
- Time
- Untimed
- Feedback
- After each answer
New questions each time you start, drawn from the bank, unseen ones first.