What Is a Production Environment? Dev, Staging, Prod
A developer adds a discount-code box to an online shop's checkout. On their laptop it works every time. Minutes after it goes live, customers see errors: the live shop runs a different version of its database, the program that stores orders, and one request behaves differently there. This lesson explains what a production environment is, the rehearsals that come before it, and the rules that keep it safe.
Four places your code runs
An environment is a complete place where software runs: the computers, their operating system, the database and other services the app talks to, and the settings that tie them together. Teams keep several and move every change through them in order.
Development (dev) is usually your own laptop, where you write code and try ideas. Nobody else sees it, so breaking it is harmless.
Test, or CI (continuous integration, from lesson 1), is where a CI system builds and tests every change you push, often on a fresh, temporary machine. Nothing is left over from your laptop there, so it catches changes that only worked because of your setup (lesson 4).
Staging, or pre-production, is a close copy of production: the same software and versions, set up the same way, usually on fewer or smaller machines, with no real users or real customer data. It is the dress rehearsal.
Production (prod, or live) is the environment real users rely on, holding real data such as their accounts and orders. A mistake here costs money and trust.
In the flowchart, a gate sits between each pair of environments: a change moves on only when the tests there pass, or goes back to the laptop. The bar on the right makes the point: the further a mistake travels, the more people see it.
Some teams add more environments, such as one for manual testing, and a small team may skip staging.
What should differ, and what must not
The table compares the three long-lived environments. Staging uses made-up data or masked data: a copy with names, emails and card numbers replaced by fakes.
| Question | Development | Staging | Production |
|---|---|---|---|
| Who uses it | One developer | The team and testers | Real customers |
| Data used | Made-up test data | Made-up or masked data | Real customer data |
| How many servers | Your laptop | A few, often smaller | As many as the traffic needs |
| Who may change it | The developer, any time | The pipeline, after CI passes | Only the pipeline, after staging checks |
Four things should differ. Size: production needs the most servers. Credentials, the passwords and keys an app uses to reach its database and other services: each environment gets its own, so a leaked staging password cannot open production. Data: only production holds real customer data. Feature flags, on/off switches for features (lesson 9), may be on in staging and off in production.
Three things must stay identical. The code. The build artifact, the packaged result of a build that you install and run, such as a container image. And the versions of the operating system and of the dependencies, the ready-made code libraries your app relies on. If any of these differ, staging no longer predicts production.
Why "it works on my machine" happens
"It works on my machine" is a classic developer excuse, and it is usually true: the code did work, on a machine that differs from production in ways nobody wrote down.
Typical differences: another version of the programming language or a library, another operating system, a setting or file that exists only on the laptop, or a handful of test records instead of millions of real ones. Each can make the same code behave differently.
A common trap is a different kind of database on the laptop, such as the lightweight SQLite, while production runs PostgreSQL. The Twelve-Factor App, a well-known set of rules for building apps, warns that small differences between them can break code that passed its tests.
The cure is dev/prod parity: keeping development, staging and production as alike as possible, one of the twelve factors (lesson 3). Containers help: an image packs the app with its dependencies and operating system files, so even a Mac or Windows laptop can run the same setup as production (see What Is Docker?). Infrastructure as code, from lesson 1, builds every environment from the same description files.
Rules for production
Teams protect production with a few firm rules.
- Restricted access. Only a few people and tools, such as the pipeline, may change it, each with just the access the job needs: least privilege (see Cloud identity and access management explained).
- No testing on real customer data. Real data holds names, addresses and payment details, so tests use made-up or masked data.
- Changes only through the pipeline. The pipeline is the automated chain of build, test and deploy steps. A fix typed by hand on a server skips the tests, leaves no record, makes production differ from staging, and may be wiped out by the next deploy.
- Monitoring. Errors and response times are watched constantly, and a person is alerted when they look wrong (see Monitoring and Alerting Basics).
- A way to roll back. To roll back is to return quickly to the last version that worked. Plan it before a release, not during an outage (lesson 10).
Configuration travels separately from code
If the build is identical everywhere, how does the app in staging know to use the staging database? Through configuration (config): the settings that change between environments, such as database addresses, credentials and feature-flag values. The Twelve-Factor App defines config as everything likely to vary between deploys.
The app reads its config when it starts, from outside the build: a settings file placed in each environment, or environment variables, named settings the operating system hands to a program at start. Secrets such as passwords belong in a protected secret store, never in the code (lesson 14).
That lets you build once and deploy the same artifact everywhere. In the diagram, the same build box appears in all three environments; only the config card beside it changes.
Rebuilding for production could pull in a different library version, so you would ship something nobody tested (lesson 6). Twelve-Factor's own test: could you publish your code today without exposing a single password? If not, config is hiding in it.
Check yourself
A bug appears in production but never on the developer's laptop or in CI. What do you compare first, and why?
The environments themselves: language, library and operating system versions, the config, and the size of the data. If the same code behaves differently, something around the code differs.
To make tests realistic, a team copies the real customer database into staging. What is wrong with that, and what should they use?
More people and tools can reach staging than production, so real names and payment details end up less protected. Made-up or masked data gives realistic tests without exposing anyone.
Why should production run the exact build that passed in staging, not a fresh build of the same code?
A fresh build can pick up different dependency versions, so it is not what you tested. Reusing the tested artifact keeps staging a true preview of production.