
There’s a particular pattern that makes me sigh and resign myself to adding “prod-like environment” to my meeting-bingo-card.
Whether it’s in application or infrastructure code, it’s bothered me for years, usually excused by some variation of the phrases:
These seem like perfectly reasonable calls to make if you’ve never been bitten before. I have. And it frustrates me when I see nonsense like:
if environment == "prod":
cluster_size = 10
else:
cluster_size = 2
It’s a fairly innocuous bit of code, an assumption that Production needs ten nodes and all other environments can comfortably get away with two, and there’s no point in spending money on capacity we’re unlikely to use - absolutely sound reasoning.
Wrong, at some point somebody will actually want ten nodes in a test environment because something weird happens when you have a 7th node in the application or when we get a 10 millionth request the app starts logging Vogon poetry. We have reached the “Prod like environment” requirement singularity.
Then someone makes a ’temporary’ change to the code to accommodate something the system was already designed to do. And so it begins….
if prod or performance_test:
I’ve always (after being bitten by my own silliness many moons ago) approached this from the other direction. Start with the system we intend to operate in production and work backwards, making deliberate decisions about where development and our Path to Live (PTL) environments need to differ.
If the system is designed to operate with ten nodes, make that the baseline.
# System defaults
cluster_size = 10
retention_days = 90
multi_az = true
cache_mode = "distributed"
Our non-production environments (dev, uat, int, and so on) can then override the things we’ve decided they don’t need.
# Development overrides
cluster_size = 2
retention_days = 7
multi_az = false
The resulting infrastructure might be identical to what we’d have created using a collection of environment-based conditionals, but we’ve fundamentally changed how we think about and set those decisions. Running two nodes is now an intentional departure from the production design, made because we’ve decided the reduced capacity is sufficient for the purpose of that environment and accepted the risks that come with it.
If I subsequently need ten nodes to test something, I can remove that particular deviation without affecting the other characteristics of the environment. I can even set it to 11.
The same applies to application code. If we’ve chosen to use a distributed cache in production and a local cache in development, the choice of cache implementation should be configurable. With a local cache, what we’re testing isn’t comparable to production, so we shouldn’t expect production behaviour there. Coupling the choice to the environment name unnecessarily restricts where and how we can exercise that portion of the service.
Production-shaped defaults shouldn’t mean production-connected defaults. Credentials, customer data, account identifiers and live integration endpoints need to be explicitly configured and appropriately isolated. I just want to reproduce production behaviour without accidentally becoming part of production.
One of the more uncomfortable consequences of if prod logic is that we can inadvertently arrange for the least rehearsed behaviour to be exercised in the environment with the largest blast radius. A policy might have been perfectly valid in isolation, but how confident are we that applying it won’t interfere with some other part of the system? Have we run our deployment process against the larger topology? Have we tested the application’s behaviour when one of those additional production dependencies becomes unavailable? Is production going to work with that special only-in-prod feature enabled?
Of course we can unit-test the conditionals but testing and exercising the resulting system is a rather different proposition if you’ve locked things behind the context in which they run.
The same problem extends into incident investigation. Being able to stand up a temporary environment, reproduce a particular set of production characteristics and repeatedly exercise a problematic behaviour is an incredibly useful engineering capability. It’s considerably less useful when half those characteristics are buried behind a collection of if prod statements.
We can’t reproduce everything about production. Real traffic, data volumes, usage patterns and years of accumulated state make that difficult even with the best infrastructure-as-code implementation. There will always be some things we need to validate carefully in live. I’d prefer not to add unnecessarily to that list through decisions we’ve made about the structure of our own code.
Obviously this is something you don’t just intrinsically know, and if you’re reading this and saying you did you’ve forgotten what it was like being a junior engineer. In collecting my notes for this post I came across a couple of places I remembered getting guidance on this from in one way or another.
In 2010’s Book of the Year, Continuous Delivery by Jez Humble and David Farley, they wrote at length about making the expected production configuration the default and overriding it in other environments where necessary.
Kief Morris has a suspiciously convenient example in Infrastructure as Code in which a container cluster requires fewer worker nodes in development and more in production. The infrastructure implementation remains reusable, with the differences supplied through configuration.
There’s plenty of support for this approach in more modern engineering guidance, too.
HashiCorp’s guidance on Terraform module composition advocates for dependency inversion and directly describing the resources and dependencies required, rather than creating modules with complex conditional branches. Microsoft’s Well-Architected guidance recommends using the same infrastructure-as-code assets across environments, parameterising characteristics such as resource size, count and configuration. Whilst AWS’s resilience lifecycle guidance takes a pragmatic view of pre-production environments, recognising that earlier environments can have fewer capabilities and lower costs, with subsequent environments becoming increasingly representative of production.
All of this is fairly well-established engineering practice and yet we all still seem remarkably comfortable embedding environmental assumptions into code.
As someone in the engineering leadership space, I think there’s a responsibility to create systems that lend themselves to being tested. If not just because I’ve never been one for spending time on bridge calls discussing incidents.
We readily accept this principle when writing application code. We talk about dependency injection, separation of concerns and designing for testability. I think we should apply that same thinking to the system as a whole, including its infrastructure, configuration, and the deployment mechanisms.
When we choose to deviate from the production design somewhere in our Path to Live, we’re making a trade-off. We should be knowingly accepting reduced confidence in particular aspects of the system in exchange for lower costs, or a simpler development experience. Which is fine, provided we understand what we’re accepting and haven’t made it unnecessarily difficult to revisit that decision. Explicitly overriding configuration values is the conscious decision to deviate away from what’s running in production for one reason or another.
I’d much rather see:
# Reduced from 10 to 2 to manage PTL costs and fit in local dev stack
cluster_size = 2
The reasoning is visible, the limitation is clear and understood, another engineer can reverse the decision when they need to. (Or someone gets more RAM if Sam Altman permits)