The Twelve-Factor App in a Real SaaS
Most app problems after launch are not bugs in a feature. They are problems in how the app is run.
A setting that only lives on one laptop. A server that forgets who is logged in when it restarts. A bad release with no quick way back.
The Twelve-Factor App is a list of twelve rules that stops those problems. It was written by the team behind Heroku, and it still holds up for any web app.
This page shows how one SaaS I build measured up: a job-alert app with real users, payments and a database.
The twelve rules, in plain words
- One codebase. One code store. Test site and live site are copies of it, not forks.
- Dependencies. Every tool the app needs is written down, with its version, and installed with the app. Nothing relies on a tool that just happens to be on the machine.
- Config. Passwords and settings live outside the code, set per site.
- Backing services. The database, email and payments plug in by address. Swap one, change one setting.
- Build, release, run. Building, going live and running are three separate steps. Every release has a name you can go back to.
- Processes. The app keeps nothing in memory that it can’t lose. All lasting data sits in outside storage: the database, a file store, and so on.
- Port binding. The app serves itself on whatever port the host gives it.
- Concurrency. The app can handle more traffic by running more copies of itself. A bigger server is still allowed, but it shouldn’t be the only option.
- Disposability. A copy starts fast and shuts down cleanly. If one stops mid-job, the work can finish or safely run again.
- Dev/prod parity. My laptop, the test site and the live site run the same tools, so a test result means something.
- Logs. The app writes events out. The host collects them.
- Admin processes. One-off jobs, like database changes, run with the same code, tools and settings as the app. Starting one by hand is fine.
The first check: 8 out of 12
In September I ran a full audit of the app against all twelve. The result:
- 8 passed
- 3 passed only in part
- 1 could not be proven from the code
None of the gaps had broken anything yet. Each one was the kind of thing that breaks on a busy day.
What failed, and the fix
The tool versions were not pinned. The required version of Node was only written in the setup notes. Two machines could build with two different versions. Fix: the version is now set in the project files, so the wrong one is refused.
The app ignored the host’s port. It always started on its own fixed port. On most hosting platforms that means the app starts but nobody can reach it. Fix: it now takes the port the host gives it.
A test tool was set to “latest”. The local email catcher could change under me without any code change. Fix: pinned to one version.
Nobody could prove what was live. This was the big one. The host builds the test site and the live site separately. After a release, there was no quick way to check both ran the same code, or which release to go back to. Fix:
- Each site now reports the exact code version it runs.
- One command checks both sites against the main code and flags any difference.
- A written rollback plan: move the live site back to the last good release, with no rebuild.
- A rule for when the database changed too, since old code must still work with new tables.
Writing the plan was not the end. Checking it against the live setup, line by line, found a nightly backup that had failed three nights in a row without anyone noticing. More on that check: When to roll back a release.
Where it stands now
In November I ran the check again: 8 pass, 4 pass in part, none fail. The parts still open are small, and each is written down with its fix:
- Codebase. Some old branches still need clearing out.
- Config. No password or key is in the code, and each site gets its own from the host. But some setting names are missing from the example file a new developer starts from.
- Dev/prod parity. My laptop runs the same database version and add-ons as the live site. The tests run before every push, but not yet on a separate server.
- Admin processes. Database changes run one way only, from the host’s build, never from a laptop. Anything that deletes data needs an extra yes. The rerun flagged that the nightly backup depended on a key file missing from the code store.
Two that were already strong:
- Logs. Errors go to an error tracker with personal data removed before they leave the app.
- Processes. One gap from the first draft of this post is closed: rate limits used to be counted in each copy’s memory, so five copies let one person go five times over the cap. The count now lives in the database, shared by every copy.
Where I bend the rules, for now
This app is still early. Following every rule to the letter costs time that is better spent on users. So a few rules are bent on purpose, and each one is written down with the point where it gets fixed.
Two builds, not one. The strict rule says: build once, then move that same build from test to live. This app builds twice, once per site, because some settings get baked in at build time and the two sites need different ones. The version check proves both builds came from the same code.
My laptop is close to live, not identical. The database runs in Docker on my laptop, same version and add-ons as live. The app itself runs straight on the laptop, because the live host’s setup can’t run on a laptop. If the app ever moves off this host, one Docker image for every stage closes this gap.
Database changes run during the build. The strict rule keeps building and changing the database apart. Here the host’s build runs any new database changes first, so no laptop holds the keys to the live database. Changes only add, never remove in the same step, so the site keeps working while the new version goes live.
Six questions to ask your developer
You don’t need to know the twelve rules to test your own app. Ask:
- Can a new developer start the app from the written steps alone? If it only runs on one laptop, rules 1, 2 and 3 are failing.
- Where do the passwords and keys live? The right answer is “with the host, per site”. Never in the code.
- Which version is live right now, and how do you know? A guess is a fail.
- How do we go back to the last good release, and when did you last try it?
- If the server restarts, does anyone lose work or get logged out?
- When did the backup last run, and who would notice if it stopped?
What a new developer should get on day one: What a good handoff includes.
You don’t need to learn the twelve rules. I run the check against your code and your live setup, fix what fails, and write down any rule bent on purpose, with the point where it gets fixed.
The short version
Twelve-factor is not about style. It decides how fast you recover when something goes wrong, and whether a new developer can run your app on day one. Check it, fix what fails, write down what you bend, and check it again later.
Which of the twelve would your app fail today?
Stack: Next.js 16 (App Router) on Vercel, PostgreSQL 17 with pgvector on Neon, Clerk for auth, Stripe, Sentry, SMTP. Builds are started by hand from main (npm run ship); a dev branch builds to a separate Preview site with its own Neon database.
Factor by factor
| # | Factor | How the app meets it |
|---|---|---|
| I | Codebase | One repo. Local, dev and live are deploys of it. The Chrome extension is its own repo and its own app. |
| II | Dependencies | package-lock.json, "engines": { "node": "24.x" } and .nvmrc. A check script fails fast when a host tool (psql, hurl) is missing. |
| III | Config | Every secret and per-site setting is an env var, read through one validated config module. .env* is git-ignored; .env.example holds placeholders only. |
| IV | Backing services | Postgres, Clerk, Stripe, SMTP, AI providers and Sentry attach by URL or key. Postgres sits behind one Db port. |
| V | Build, release, run | Vercel builds immutable deployments. GET /api/version returns the commit; npm run release:status compares live and dev to origin/main. Rollback is vercel rollback. |
| VI | Processes | No session or business state in memory. Rate-limit counters live in a shared Postgres table. The only per-instance state is a 5-minute email cache. |
| VII | Port binding | next start --port ${PORT:-${APP_PORT:-7319}}. |
| VIII | Concurrency | Scale out by request. Background work runs as bounded, idempotent cron routes, not an in-process loop. |
| IX | Disposability | No startup writes, no in-process queue. AI calls have abort timeouts and capped retries. |
| X | Dev/prod parity | Local Docker runs pgvector/pgvector:pg17, same major and extension as Neon. Mailpit pinned to v1.31.1 stands in for SMTP. The app runs on the host locally, not in a container (see trade-offs). |
| XI | Logs | console.* to stdout, collected by Vercel. Sentry gets errors with emails, tokens and free text redacted in beforeSend. |
| XII | Admin processes | Migrations, seeds and backfills are npm scripts using the app’s own dependencies and config. |
The November rerun
8 pass, 4 partial, 0 fail. Open items:
| Factor | Partial because | Fix |
|---|---|---|
| I | Design rules synced in from another repo; stale sync branches | Keep the sync check in the push gate; delete merged branches |
| III | About 39 files read env directly; about 24 names missing from .env.example | Route reads through one env module; add placeholders |
| X | Tests run only in local hooks, no CI job | Add CI when the plan allows |
| XII | Nightly backup workflow needs a public key file that .gitignore excludes | Commit the public key, run the workflow once |
The gaps from the September audit
| Factor | Gap | Fix |
|---|---|---|
| II | Node version only in the README | engines.node + .nvmrc, plus a host-tool check |
| VII | npm start ignored PORT | PORT first, local port as fallback |
| X | mailpit:latest in Docker Compose | Pinned to v1.31.1 |
| V | No proof which commit is live, no rollback plan | Version route, release check, rollback doc |
Factor V in detail
The route is tiny. It reads what Vercel stamps on every build:
export function readReleaseInfo(env: ReleaseEnv): ReleaseInfo {
return {
commit: env.VERCEL_GIT_COMMIT_SHA?.trim() || null,
branch: env.VERCEL_GIT_COMMIT_REF?.trim() || null,
environment: env.VERCEL_ENV?.trim() || "local",
};
}
The release check fetches /api/version from both sites and compares each commit to origin/main. The dev branch is kept current with merge commits, so a site also counts as matching when its commit has the same git tree as main: same files, different commit.
origin/main 292314d
live 292314d production matches main
dev 3f65022 preview matches main
Rollback:
vercel lsto find the last good Production deployment.vercel rollback <deployment>moves the domain back. No rebuild.npm run release:statusconfirms live runs the older commit.- Vercel pauses auto-promotion after a rollback, so the fix goes live with
vercel promote.
Database changes do not roll back with the code. Migrations are forward-only. An additive migration (new table, new column) is safe under the old code. A drop or rename needs a database restore from the Neon snapshot taken right before the live migration, then the code rollback.
Factor XII in detail
Database updates run inside the Vercel build, before next build (buildCommand in vercel.json). No laptop holds production database keys.
- Production build: pending files run against prod. A file that drops, truncates or deletes rows fails the build unless
ALLOW_DELETEnames that exact file. - A Neon snapshot is taken first; until beta a pre-push backup stands in for it.
- Dev build: the same against dev’s own database.
- Files only add, so the running code keeps working until the new deployment goes live. A drop or rename takes two pushes: first the code stops using the column, then the file removes it.
- A pre-push hook blocks edits to any migration already on
main, and duplicate numbers fail a check.
Trade-offs (early stage)
One build per environment (V). Twelve-factor wants one build promoted from stage to production. Next.js inlines NEXT_PUBLIC_* values at build time, and the Clerk key, Sentry environment, launch stage and analytics key differ between dev and live. So each site gets its own build of the same commit. The release check is what proves the two builds match.
Local is not the live runtime (X). Locally, next dev runs on the host with Node 24; only Postgres and Mailpit run in Docker. Dev and live run on Vercel Functions, which can’t run locally. vercel build reproduces the build step before a push. Moving off Vercel would mean one Docker image for every stage, with Postgres kept as a managed service.
Migrations in the build step (V, XII). Strict twelve-factor keeps the build free of side effects and runs migrations as a release-phase admin process. Running them in buildCommand keeps production credentials off every laptop, and add-only files keep the live deployment safe while the new one builds.
Fixed since the first draft: per-instance rate limits (VI). Limits were counted in each function instance’s memory, so N warm instances let a user get N times the cap. They now live in a shared Postgres table, with memory only as a fallback.
More Posts
All posts
November 25, 2026
Why Your App Went Offline on Vercel's Free Plan (and How to Stay Under the Limits)
Vercel's free plan pauses every site on the account when one monthly limit runs out. What the limits mean in plain words, what uses them up, and the fixes, from a real case.
November 24, 2026
Why Your Project Starts With a Small Paid Step: Requirements and an Estimate Before the Build
A price given before the work is measured is a guess, and someone pays for it later. What a small paid first step gets you: written decisions, a cost range and a time range, and the choice to stop there.