Environments: local, ephemeral, staging
Environments trade realism against isolation, so one environment is not enough. Local gives the fastest loop, ephemeral per PR isolates a change, shared staging integrates, and production is used only for smoke and synthetic checks. An ephemeral environment is usually partial: the changed service and its closest neighbours. External dependencies are replaced by HTTP stubs, and a real sandbox stays as a small set outside the PR gates.
System design · Data and environments
A test on staging failed because a neighbouring team deployed their branch. Where to run tests then?
Morning, the run on staging is red. The log shows that the transfer service returned 500, and the cause was not the change in the tested PR but the neighbouring team's branch, deployed to the same environment. A test system is not only test code but also the environments where it runs. This lesson covers which environments exist, how they differ and how to handle external dependencies.
How it works and why. There is a trade-off between realism and isolation, so one environment is not enough. A local setup (docker compose) gives the fastest loop for writing a test. An ephemeral environment per PR isolates the check of a change from all the others. A shared staging integrates with real dependencies, but everyone works there and data is shared. Production is used for smoke and synthetic checks after a release. Each step is more realistic than the previous one but less isolated, and a mistake there costs more.
Four environments and what each is for
An ephemeral environment is brought up for every PR (for example, a separate namespace in Kubernetes) and destroyed after the merge or by a TTL. A plus: no conflicts between teams, clean data, destructive tests are possible. A minus: cost and bring-up time, and starting all 40 microservices for every PR is hard and expensive. So in practice a compromise is common: bring up the changed service and its nearest neighbours, and take the rest from the shared staging or replace them with stubs. The data needs thought too: whether an empty database with seed data comes up, or a test creates everything itself. One more price: the bring-up time is part of PR feedback time, so it is measured as well.
External dependencies. A payment provider, a KYC service and bank APIs often have a sandbox with limits: request limits, instability, the impossibility of reproducing some errors (say, a timeout or a duplicate callback). So for most tests an HTTP-level stub (WireMock or similar) is put in with controlled responses: success, refusal, timeout, repeated delivery. The real sandbox is kept for a small set of integration tests that run separately and do not block a PR.
A payment provider in a test
A controlled stub
Responses under control: success, refusal, timeout. Fast and stable. The price: a stub can drift from the real API.
A real sandbox
Real provider behaviour. The price: limits, instability, some errors cannot be reproduced.
Example: a provider timeout. You need to check that the service behaves correctly when the provider answers in 8 seconds while the client timeout is 5. The real sandbox cannot reproduce this, and a stub does it easily. Before the action the test registers a stub through WireMock's admin API and removes it afterwards:
{
"request": { "method": "POST", "urlPath": "/v1/payments" },
"response": { "status": 200, "fixedDelayMilliseconds": 8000 }
}
In WireMock such conditions are set by a response delay (fixedDelayMilliseconds) or by connection faults (the fault field, for example CONNECTION_RESET_BY_PEER). WireMock (from 3.1) can send a callback itself through a webhook (serveEventListeners), and a repeated delivery is often simpler to send from the test.
Limits and alternatives. A stub can drift from the real API: the provider changed the response format and the tests are still green. Contract tests (checking the stub against the real provider) and the same small sandbox suite protect against this. If several tests in parallel register stubs on the same WireMock, they interfere: the conditions must be unique (say, by an order identifier) or each worker needs its own instance. Production checks are only safe scenarios: reading, or actions with a dedicated synthetic account under strict limits, because real money does not come back.
Common mistakes.
- A single environment for development, tests and demos: someone else's action breaks a run at any moment.
- Bringing up everything per PR: the cost and bring-up time eat the benefit of isolation.
- Testing payment logic only on the provider's sandbox: errors and timeouts cannot be reproduced there, and its instability blocks a PR.
- Stubs never checked against the real API: tests are green while the integration is broken.
- Not cleaning up ephemeral environments: bills grow and cluster resources run out.
Check yourself
Why are not all 40 microservices brought up for each PR, and what is done instead?
Answer
It is expensive and slow: the bring-up time is part of PR feedback time. The changed service and its nearest neighbours are brought up, and the rest is taken from the shared staging or replaced with stubs.
A payment provider stub always returns success, and the provider changed its response format. What protects against green tests on a broken integration?
Answer
Contract tests that compare the stub with the real API, and a small separate set of tests on the real sandbox that runs outside the PR gates. The stub gives speed and control, while the sandbox catches the divergence.