Chapter 6

"Don't Test on Prod, Bro!": The Staging Environment

Subhash Choudhary · 14 min read

The journey so far has been about fighting external forces. We fought server limitations, traffic spikes, and the laws of physics. They were the glorious battles of scale, the consequences of our own success. This chapter is about a different kind of battle. It's about the fight against our own worst enemy: ourselves.

As a company grows, you move from a two-person army to a small platoon. You hire your first engineers. The speed of development increases, but so does the complexity and the risk. The informal, "move fast and break things" culture that got you off the ground can quickly become the very thing that burns your company to the ground.

This is the story of our first self-inflicted disaster, and the crucial lesson we learned about building a safety net before you learn to fly.

Part 1: The Bug That Broke Everything

With our new, scalable architecture in place, we started hiring. Our team grew from just Suumit and me to a handful of talented, enthusiastic engineers. The energy was incredible. We were shipping new features faster than ever before. A new payment option, a product filter, a better way to manage orders-the pace was exhilarating.

Our process for deploying code was, in hindsight, terrifyingly simple. An engineer would write some code on their laptop, test it to see if it worked for them, and then push it to our central code repository on GitHub. A simple script would then automatically take this new code and deploy it directly to our live, production servers-the servers that our hundred thousand sellers and their millions of customers were using.

From a developer's brain to a live user's screen in under five minutes. We thought this was the pinnacle of agility. In reality, it was like doing a trapeze act without a safety net.

One Tuesday afternoon, a new junior developer-let's call him Rohan-was tasked with adding a simple feature: a button to sort products by price. He was a sharp kid, and he built the feature in a couple of hours. On his laptop, using his test store which had about 15 products, it worked perfectly. The products sorted instantly. Confident in his work, he pushed the code.

Five minutes later, my phone exploded.

It wasn't a server alert. It was Suumit. And he was not calm.

"Subhash! Our top sellers are down! gavranmisal.com, Jain Shikanji-all the big guys! Their stores won't load. They're calling me, they're losing money! WHAT HAPPENED?"

I jumped to my monitoring dashboard. The servers weren't down. The CPU was fine, the memory was fine. This wasn't a scaling issue. This was a code issue. A bug.

We scrambled. We checked the recent code pushes and saw Rohan's change. Looking at his code, the logic seemed fine, but my eyes caught one specific database query. It was a simple ORDER BY price clause. But the way it was written, it would be incredibly inefficient on stores with thousands of products. On Rohan's test store with 15 items, it was instant. On gavranmisal.com's store with 2,000 items, the query would time out, crashing the page load process. He had accidentally shipped a bug that only affected our biggest and most important customers.

The next ten minutes were pure, adrenaline-fueled chaos. We had to perform an emergency "revert," a process of rolling back the code to the previous version, all while our live system was on fire and our support channels were flooding with angry messages from sellers.

We eventually fixed it. The stores came back online. But the damage was done. We had let our users down. And it wasn't because of a traffic spike or a hardware failure. It was 100% our own fault.

That evening, Suumit and I had a tense call.

"This can never happen again," he said, his voice sharp. "We looked like amateurs. Pushing code straight from a laptop to the live site is insane. It's like a chef trying out a new recipe for the first time on the Prime Minister. We need a safety net. A place to test things properly before they go live."

He was right. We had been flying without a parachute. It was time to grow up.


#### **The Solution: Building a Safety Net**

The root of our problem was that we had only two places where our code existed: on a developer's laptop, and in front of live customers. There was no step in between. A professional software team needs a proper assembly line, with quality checks at each stage. This is the Software Development Lifecycle.

Technical Deep Dive: The Environments

Think of a high-end restaurant. They don't just cook and serve. They have a rigorous process that involves three distinct environments.

1. The Development Environment (The Test Kitchen)

This is the developer's laptop.

2. The Production Environment (The Dining Room)

This is the live server that real users interact with.

3. The Staging Environment (The Dress Rehearsal)

This was the critical piece we were missing. A Staging environment is a complete, parallel universe that is an exact mirror of your Production environment.

This was our safety net. A place where Rohan could have deployed his new code and we could have tested it against a copy of a store with 2,000 products. The bug would have been instantly obvious. The page would have crashed in Staging, and not a single real customer would have ever been affected.

We knew what we had to do. We needed to build a perfect replica of our main stage, just for rehearsals.

Part 2: Building the Mirror

The decision to build a staging environment was a pivotal moment for us. It was a declaration that we were moving from a chaotic garage band to a professional orchestra. An orchestra needs a dedicated space for rehearsals, and we were about to build ours.

But building a useful staging environment is much harder than it sounds. It's not just about spinning up another server and deploying your code to it. A bad staging environment can be worse than none at all, giving you a false sense of security. To be an effective safety net, our dress rehearsal stage had to be a perfect, down-to-the-millimeter mirror of the real thing.

Technical Deep Dive: The Importance of Identical Environments

This is the golden rule of staging: your staging environment must be as identical to your production environment as possible.

Why? Because subtle differences are where bugs love to hide.

You can't have a dress rehearsal for a massive Broadway play on a tiny high school stage with cardboard props and expect to find all the problems. You need a stage with the same dimensions, the same lighting, and the same acoustics.

For us, this meant a significant new investment. We had to replicate our entire production architecture:

This effectively doubled our server costs. For a bootstrapped startup, this was a painful expense. But we reframed it in our minds. It wasn't a cost; it was an insurance premium. We were paying a predictable monthly fee to insure ourselves against the massive, unpredictable cost of a catastrophic, reputation-damaging production outage.

Technical Deep Dive: The Challenge of Seeding and Sanitizing Data

We had built a perfect, empty stage. But a dress rehearsal is useless without actors and props. A staging environment is useless if it's not populated with realistic, large-scale data. This is, without a doubt, the hardest part of maintaining a useful staging environment.

Our bug with Rohan's feature happened because he tested on a store with 15 products, while the production bug only appeared on stores with over 1,000 products. To catch these kinds of bugs, our staging environment needed data that mirrored the scale and complexity of production.

The obvious, but dangerously wrong, solution is to simply clone your production database and load it into staging. You must never, ever do this.

Your production database contains your users' most sensitive information: their names, their phone numbers, their email addresses, their private order histories. Copying this data into a less-secure staging environment that multiple developers have access to is a massive security and privacy violation. It's not just bad practice; it could be illegal.

So, we had a dilemma: we needed the production data's scale, but we couldn't use the production data itself.

The solution was to build a Seeding and Sanitization Pipeline. It was an automated script that performed a two-step process every night:

Step 1: Seeding

The script would start by taking a full backup of our live production database using pg_dump. This gave us a complete, structurally perfect snapshot of our data at that moment.

Step 2: Sanitization

This was the critical step. Before loading this backup into the staging database, the script would run it through a "sanitizer" that would cleanse it of all sensitive information:

This process was complex to build and required constant maintenance. But it gave us the holy grail of staging environments: a database that was a perfect mirror of production's scale and complexity, but with zero sensitive user data.

Now, Rohan could have tested his new sorting feature on the staging server against the sanitized copy of gavranmisal.com's 2,000-product store. The bug would have crashed the staging site, he would have fixed it, and no real customer would have ever been the wiser.

We had our test kitchen (the developer laptops), our main dining room (Production), and now a fully equipped, professional dress rehearsal stage (Staging). The final piece of the puzzle was to create a formal, safe, and repeatable process for moving code between them. We needed an assembly line. We needed a deployment pipeline.

Part 3: The Assembly Line

We had built our environments. We had the test kitchen (Dev), the dress rehearsal stage (Staging), and the main dining room (Production). This was a massive step forward. But having the rooms isn't enough; you need a safe and efficient way to move dishes between them.

Our old method of a developer manually running a script to push code directly to production was like a chef sprinting from the test kitchen straight into the dining room, holding a flaming pan. It was fast, exciting, and guaranteed to eventually end in disaster.

We needed to replace this chaotic sprint with a calm, orderly, and predictable process. We needed to build an assembly line for our code. In the tech world, this is called a Deployment Pipeline.

Technical Deep Dive: The Deployment Pipeline

A deployment pipeline is an automated process that takes code from a developer's laptop and safely moves it through a series of quality checks before it is finally delivered to users.

Think of it as the assembly line in a modern car factory. The developer's code is the raw steel. The pipeline is the series of conveyor belts and robotic arms that automatically move the steel from one station to the next. At each station, tests are run and quality checks are performed. Only a car that passes every single check at every station is allowed to roll out to the showroom.

The goal of a pipeline is to make deployments boring. A deployment should not be a moment of high-stakes drama and prayer. It should be a routine, predictable, and repeatable event. Boring is good. Boring means the site isn't on fire.

We designed our first, simple deployment pipeline with a series of deliberate, manual and automated steps.

Step 1: The Pull Request on GitHub

The entire process starts when a developer finishes writing their code. Instead of pushing it directly into the main codebase (the master branch), they now open a Pull Request (PR) on GitHub.

A PR is a formal request: "I have completed my work on this feature. Here is the code. Please review and approve it to be merged into the main project." This is the entry point to our assembly line. It's the raw steel arriving at the factory door.

Step 2: The Human Quality Check (Code Review)

This was a huge cultural shift for us. Before a single line of new code could proceed down the assembly line, it had to be reviewed and approved by at least one other engineer on the team.

This "second pair of eyes" is an incredibly powerful quality check. The reviewer looks for things like:

This simple, human-centric step forces collaboration and shared ownership of the code. It's a powerful way to catch bugs before they ever reach a server.

Step 3: Automated Testing & Deployment to Staging

Once a human has approved the Pull Request, the machines take over. We set up an automated system (using a tool called GitHub Actions) that would trigger automatically:

The feature was now live on our perfect, mirrored dress rehearsal stage, populated with sanitized production-scale data.

Step 4: The Final Dress Rehearsal (Manual QA on Staging)

This was the final and most critical checkpoint. A human tester (either the developer themselves or a dedicated QA person) would now have to manually test the feature on the staging server.

They would follow a checklist, acting like a real user. Does the "sort by price" button work? Does it work on a store with 5 products? Does it work on our sanitized copy of a store with 5,000 products? Does it work on a mobile browser? Does it break anything else on the page?

Only after the feature passed this rigorous, real-world manual testing could it be approved for the final step. This is the step that would have caught Rohan's bug and saved us from a production outage.

Step 5: The Push to Production

Deploying to production was now the final, deliberate, and calm step. It was no longer a frantic, ad-hoc event. Once a feature was approved on Staging, we would merge the code into our master branch. This merge would trigger the final stage of the pipeline, which would push the code to our live production servers.

This process transformed our team. It replaced chaos with order, anxiety with confidence. We were no longer just a group of coders; we were becoming a professional engineering organization.


## Key Takeaways
  • A staging environment is a non-negotiable insurance policy against self-inflicted outages. The cost of building and maintaining it is tiny compared to the cost of a production failure.
  • Your staging environment must be a MIRROR of production. Identical hardware, software, and architecture are essential to catch real-world bugs.
  • NEVER use raw production data in staging. Build an automated pipeline to seed your staging database with sanitized and anonymized data to protect your users' privacy.
  • A deployment pipeline replaces chaotic manual steps with a reliable, automated process. It enforces quality checks like code reviews, automated testing, and manual QA.
  • The goal of a good process is to make deployments BORING. Boring means predictable. Predictable means reliable. And for a growing business, reliability is everything.