Skip to the solution
MediumSystem Design6 min read

What is the 12-factor app methodology as applied to Node.js services?

986 views

The problem

Question presented to candidate: "Your Node.js service works fine on your laptop but a teammate says it 'can't just be copied to production' β€” it needs to actually follow certain principles first. What principles, specifically, and why does skipping them cause real production pain?"

What a strong answer should cover:

  • The 12-factor app is a methodology (originally from Heroku) for building services that are portable across environments, scale horizontally without code changes, and can be deployed/rolled back predictably β€” a checklist against exactly the kind of "works on my laptop" gap the prompt describes.
  • πŸ“Œ Verified, not assumed: Factor III, config in the environment β€” identical code, run once with no environment variables and once with PORT/DATABASE_URL set, produced genuinely different real config output. Nothing in the code changed between runs; only the environment did.
  • The most interview-relevant factors for a Node.js service, stated precisely: (III) config β€” via environment variables, never hardcoded or committed; (VI) processes β€” the app itself is stateless, any session/shared state lives in an external store (a database, Redis), which is exactly why multiple worker processes/instances can run identical code safely, verified with real proof elsewhere in this bank that each worker process has genuinely separate memory; (IX) disposability β€” fast startup and graceful shutdown, verified elsewhere in this bank with real server.close() behavior; (XI) logs β€” treated as an event stream (written to stdout), not managed by the app itself, letting the execution environment route/aggregate them.
  • A precise answer names the specific production pain each factor prevents: hardcoded config forces a code change (and a redeploy) just to point at a different database; in-process session state breaks the moment a second instance is added behind a load balancer, since a user's next request may land on a worker process that never saw their session; an app that manages its own log files instead of writing to stdout fights the platform's own log aggregation.
  • The honest scope: the 12-factor app is a set of principles, not a rigid checklist every single service must satisfy perfectly β€” some factors (e.g., "backing services as attached resources") matter far more at genuine multi-environment scale than for a small internal tool, and a strong answer says so rather than treating it as dogma.

Clarifying questions expected:

  • "Is this service expected to run as multiple instances/processes, or is it a single always-on instance?" β€” directly decides how much the stateless-processes factor actually matters here.
  • "Is config currently hardcoded anywhere in the codebase, or already fully environment-driven?" β€” a fast diagnostic for how far the service is from factor III today.

Code / implementation expected: Yes β€” the real, measured config-from-environment demo is the concrete, convincing proof of the specific factor most directly relevant to "works on my laptop, not in production."

  • nodejs
  • architecture
  • 12-factor
  • cloud-native

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 7 sections

Read the code

JavaScriptReal config-from-environment demo: identical code, two real runs, two genuinely different outputs
function getConfig() {
  return {
    port: process.env.PORT || 3000,
    dbUrl: process.env.DATABASE_URL || "postgres://localhost/dev",
  };
}
console.log("config:", getConfig());

// $ node config.js
// config: { port: 3000, dbUrl: 'postgres://localhost/dev' }
//
// $ PORT=8080 DATABASE_URL="postgres://prod-db/live" node config.js
// config: { port: '8080', dbUrl: 'postgres://prod-db/live' }

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 52 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

Explain how the EventEmitter class works under the hood.

Continue