01. The Production Mindset
The ultimate test of an application isn’t how smoothly it runs on your local machine — it is how it handles thousands of parallel users, hostile network drops, and malicious traffic scripts without crashing.
The Operational Reality: If a bug crashes your local server, you refresh the page. If a bug crashes your production server at 3:00 AM, your business stops, alerts fire, and costs spike. Seniors spend just as much time engineering the containment walls around code as they do writing the code itself.
What Changes Between Local and Production
| Concern | Local Development | Production |
|---|---|---|
| Errors | Stack trace in terminal, refresh and retry | Error must be caught, logged, and reported without crashing |
| Data | SQLite or local Postgres, throwaway data | Managed database with backups, migrations, and point-in-time recovery |
| Secrets | .env file in root, committed to git | Injected at runtime, encrypted at rest, never in version control |
| Traffic | You, maybe 2-3 tabs | Hundreds to thousands of concurrent connections |
| Uptime | Server crashes, you restart it | Server crashes, an orchestrator restarts it automatically |
| Logs | console.log in terminal | Structured JSON streamed to a log aggregator |
| Deployments | npm start in terminal | CI/CD pipeline validates, builds, and deploys on merge |
The Pre-Deployment Checklist
Before any code touches production, verify:
□ Environment variables validated at startup (not at first use)
□ No secrets in client bundles (grep for NEXT_PUBLIC_ with secret values)
□ Database migrations tested against a staging database
□ Error boundaries catch all rendering failures
□ Rate limiting active on public API endpoints
□ Health check endpoint returns 200 under normal conditions
□ Logging outputs structured JSON, not console.log strings
□ Build succeeds in a clean environment (no node_modules cached)
02. Environment Variables & Secret Isolation
The quickest way to get your server exploited or your database drained is letting API keys leak into public files or client bundles.
The Client-Side Leak
When using frameworks like Next.js or Astro, any environment variable prefixed with NEXT_PUBLIC_ or PUBLIC_ is baked directly into the JavaScript bundle sent to the user’s browser. Anyone can open DevTools → Network → find your bundle → search for your key.
The Rule:
- Variables with
NEXT_PUBLIC_orPUBLIC_prefix → Visible to everyone (use for public URLs only) - Variables WITHOUT these prefixes → Server-only, hidden from users (use for API keys, secrets)
The Rule: Client vs. Server Secrets
| Prefix | Where It Lives | Accessible From | Use Case |
|---|---|---|---|
NEXT_PUBLIC_* | JavaScript bundle | Browser (anyone) | Public URLs, published IDs, feature flags |
*_SECRET_KEY | Server runtime | Your server only | API keys, database credentials, JWT secrets |
*_URL (no prefix) | Server runtime | Your server only | Database connections, internal API endpoints |
The test: Open your deployed app, open DevTools, search the page source and network tab for the variable name. If you find it, it’s leaking.
Runtime Validation at Boot
Do not wait for your server to crash halfway through a user’s checkout because an environment variable was missing. Validate every secret at startup.
Tell your AI tool: “Add environment variable validation at application startup. Check that DATABASE_URL, STRIPE_SECRET_KEY, and JWT_SECRET are present and properly formatted. If any are missing, log a clear error and refuse to start.”
Why this matters: A server that crashes on startup is better than one that crashes mid-request. You find the problem immediately, not after users are affected.
Secret Rotation
Secrets should be rotated periodically. The pattern:
- Generate a new key in the provider dashboard (Stripe, Supabase, etc.)
- Add the new key as a new environment variable in your hosting platform
- Deploy with the new key
- Verify the deployment works
- Delete the old key from the hosting platform
- Only then delete the old key from the provider dashboard
Never delete the old key before verifying the new one works. This is the most common deployment outage for teams new to secret rotation.
03. CI/CD: The Automated Pipeline
Seniors do not manually drag files to servers or run git push production from their terminals. Every change is validated through automated Continuous Integration / Continuous Deployment.
The Git Flow Pattern
The flow:
mainbranch is locked — no direct commits allowed- Create a feature branch (e.g.,
feature/auth-refresh-tokens) - Open a Pull Request — CI automatically validates (lint, test, build)
- Merge to
main— triggers deployment to production
Rules:
mainis locked. No direct commits. All changes go through Pull Requests.- Every PR triggers a full validation suite (lint, test, build).
- Merging to
maintriggers deployment to production automatically.
The Validation Pipeline
Tell your AI tool: “Create a GitHub Actions workflow that runs on pull requests to main. It should: install dependencies, run the linter, run type checking, run tests, and build the project. If any step fails, the PR cannot be merged.”
The Deployment Pipeline
Tell your AI tool: “Create a GitHub Actions workflow that runs when code is pushed to main. It should: download the build artifact, deploy to Vercel using the VERCEL_TOKEN secret, and only run if the validation workflow passed.”
Branch Protection Rules
In your GitHub repository settings, configure:
| Rule | Setting |
|---|---|
| Require pull request before merging | ✅ |
| Require approvals (at least 1) | ✅ |
| Require status checks to pass | ✅ (lint, test, build) |
| Require branches to be up to date | ✅ |
| Require conversation resolution | ✅ |
| Do not allow bypassing above settings | ✅ (even for admins) |
This means no one can push broken code to production — not even the repository owner.
04. Docker: Immutable Environments
The phrase “But it worked perfectly on my local machine” has no place in senior engineering. Different OS versions, mismatched Node.js versions, and conflicting global packages break apps instantly. Docker solves this by packaging your code, runtime, and dependencies into a single immutable container.
The Production Dockerfile
A Dockerfile is a recipe that tells Docker how to build your application into a container. Think of it as a script that installs dependencies, compiles your code, and sets up the runtime.
Tell your AI tool: “Create a production Dockerfile with multi-stage builds. Stage 1: install dependencies and build the project. Stage 2: copy only the built files and production dependencies. Add a health check endpoint. Expose port 3000.”
Why multi-stage builds matter: The build stage installs dev tools (TypeScript, ESLint) that aren’t needed in production. The final image only contains what’s needed to run, reducing size from ~800MB to ~150MB.
Why Multi-Stage Builds Matter
The builder stage installs dev dependencies (TypeScript, ESLint, testing libraries) and compiles your code. The runner stage copies only the compiled output and production dependencies. This reduces your final image from ~800MB to ~150MB.
The .dockerignore File
Without this, Docker copies your node_modules, .git, and other unnecessary files into the build context, slowing everything down.
Tell your AI tool: “Create a .dockerignore file that excludes node_modules, .git, .env files, dist, coverage, markdown files, and IDE config from the Docker build context.”
Docker Compose for Local Development
Docker Compose lets you run multiple services (your app + database) together with one command.
Tell your AI tool: “Create a docker-compose.yml with two services: my app (build from Dockerfile, port 3000) and PostgreSQL (port 5432). The app should wait for the database to be healthy before starting. Use a named volume for database data.”
Senior Moves for Docker
- Never run as root. Add
USER nodebeforeCMDin your Dockerfile. Running as root inside a container is a security risk if the container is compromised. - Pin your base image versions.
node:20-alpineis fine.node:latestis a disaster waiting to happen — the base image changes and your app breaks silently. - Use .dockerignore religiously. Every file you exclude makes the build faster and the image smaller.
- Health checks are non-negotiable. Without them, orchestrators (Kubernetes, Docker Swarm, ECS) can’t tell if your container is alive or stuck.
05. Rate Limiting & API Protection
An unprotected API is an invitation for abuse. Without rate limiting, a single malicious script can exhaust your database connections, burn your API budget, or crash your server.
The Rate Limiting Stack
Implementation
Tell your AI tool: “Add rate limiting to all API endpoints. Use Redis for shared state across server instances. Set these limits:
- Global: 100 requests per minute per IP
- Auth endpoints (login, signup): 5 attempts per 15 minutes
- Return proper 429 status codes with retry-after headers”
What the rate limiter does:
- Counts requests from each IP address
- Blocks requests that exceed the limit
- Returns a 429 “Too Many Requests” response
- Resets the counter after the time window expires
What Rate Limiting Headers Look Like
What rate limiting headers look like:
Your API responses should include these headers:
RateLimit-Limit: 100— Maximum requests allowed per windowRateLimit-Remaining: 67— Requests left in current windowRateLimit-Reset: 1710000060— Unix timestamp when the window resets
The client can read these headers and show the user how many requests they have left — or proactively slow down before hitting the limit.
06. Production Telemetry: Logging & Monitoring
Once your application is live, you need clear eyes on how it behaves under load. You cannot fix what you cannot measure.
Structured Logging over console.log
Standard console.log() outputs unformatted strings that are impossible to parse or query when thousands of operations happen simultaneously. Structured JSON logging lets monitoring tools index, filter, and alert on specific events.
Tell your AI tool: “Add structured logging. Every log entry should be a JSON object with timestamp, level (info/warn/error), message, and context (userId, requestId, any relevant data). In production, output as JSON. In development, pretty-print for readability.”
Good logging example:
When a payment succeeds, log: { "timestamp": "2026-07-11T10:00:00Z", "level": "info", "message": "Payment processed", "userId": "abc123", "amount": 49.99, "paymentId": "pay_xyz" }
Bad logging example:
console.log("payment failed") — No timestamp, no context, impossible to search or debug.
Log Levels: When to Use Each
Log Levels: When to Use Each
| Level | When | Example |
|---|---|---|
debug | Development only, verbose troubleshooting | ”Query returned 42 rows” |
info | Normal operations worth recording | ”User signed up”, “Payment processed” |
warn | Unexpected but non-fatal events | ”Rate limit approaching”, “Deprecated API called” |
error | Failures that need attention | ”Payment failed”, “Database connection lost” |
The rule: In production, debug logs are typically disabled. info is your baseline. warn and error trigger alerts.
Global Error Handlers
Implement global exception barriers to catch unhandled errors gracefully — your service stays running for other users while your team gets notified immediately.
Tell your AI tool: “Add global error handlers that:
- Catch unhandled promise rejections and log them with context
- Catch uncaught exceptions, log them, and exit gracefully
- Handle SIGTERM for graceful shutdown (stop accepting requests, finish in-flight work, close database connections, exit)”
Why this matters: Without global handlers, an unhandled error crashes the entire server. With them, the error is logged, the server stays running for other users, and your monitoring tool alerts you.
External Monitoring Services
| Service | Purpose | Free Tier |
|---|---|---|
| Sentry | Error tracking, stack traces, release tracking | 5k errors/month |
| Logflare | Structured log aggregation | 1GB/month |
| BetterStack | Uptime monitoring + incident management | 20 monitors |
| Grafana Cloud | Metrics, logs, traces, dashboards | 10k metrics |
The Monitoring Dashboard
At minimum, track these metrics:
07. Database Migrations in Production
Running prisma migrate dev or drizzle push from your local machine against a production database is a ticking time bomb. Migrations in production need a controlled, reversible process.
The Migration Pipeline
1. Write migration file locally
│
▼
2. Test against staging database (copy of production schema)
│
▼
3. Commit migration file to git
│
▼
4. CI/CD pipeline runs migration against production
(only AFTER deployment succeeds)
│
▼
5. Verify migration completed (check logs, run smoke tests)
The Safe Migration Pattern
Tell your AI tool: “Create a migration that adds a contributions table with columns: id (UUID primary key), user_id (foreign key to users), amount (integer, must be positive), cycle_id (foreign key to cycles), created_at (timestamp with default). Also create a rollback function that drops the table if needed.”
Why this matters: Every migration needs a rollback path. If something goes wrong mid-deployment, you need to undo the change cleanly.
Senior Moves for Migrations
- Never drop a column in the same migration that adds its replacement. Deploy the new column first, update all code to use it, then drop the old column in a separate migration.
- Always write a
downfunction. If a migration fails mid-way, you need a clean rollback path. - Test migrations against a copy of production data. Schema-only tests miss constraint violations, foreign key issues, and data type mismatches.
- Use lock timeouts. A migration that locks a table for 10 minutes will block every user request. Set
lock_timeout = '5s'on PostgreSQL migrations.
08. Incident Response: When Things Break
Things will break. The difference between a junior and a senior is how fast they detect, respond, and recover.
The Incident Response Playbook
DETECT (automated)
│ Monitoring alerts fire (error rate spike, latency spike, uptime check fails)
│
├──▶ ASSESS (human, < 5 minutes)
│ Is it user-facing? How many users affected? Is data at risk?
│
├──▶ COMMUNICATE (if user-facing)
│ Status page update: "Investigating reports of [issue]"
│
├──▶ MITIGATE (fastest path to recovery)
│ ├── Rollback the last deployment? (most common fix)
│ ├── Scale up resources? (traffic spike)
│ ├── Feature flag off the broken feature?
│ └── Failover to backup region? (infrastructure failure)
│
├──▶ RESOLVE
│ Service restored. Confirm via monitoring dashboards.
│
└──▶ RETROSPECTIVE (within 48 hours)
What caused it? Why didn't CI catch it? How do we prevent it next time?
The Rollback Pattern
The fastest recovery is almost always rolling back to the last known good state:
# Vercel — instant rollback to previous deployment
npx vercel rollback
# Docker — roll back to previous image tag
docker pull myapp:v2.3.0 # Previous version
docker-compose up -d # Restart with old image
# Database — if migration caused the issue
npx drizzle-kit migrate:rollback
The rule: If you can’t rollback within 5 minutes, your deployment process is too complex.
Key Takeaways
| Concept | Mental Model | Senior Insight |
|---|---|---|
| Production Mindset | Containment walls around code | Pre-deployment checklist catches 90% of outages before they happen |
| Environment Variables | Secrets in server only, never client | Validate at boot, rotate periodically, never delete old before verifying new |
| CI/CD | Automated validation before every merge | Lint → Type Check → Test → Build — all must pass, no exceptions |
| Docker | Immutable, reproducible environments | Multi-stage builds, .dockerignore, health checks, never run as root |
| Rate Limiting | Protect your API from abuse | Three layers: edge, gateway, application — each with different limits |
| Structured Logging | JSON over console.log | Timestamp + level + message + context — queryable, alertable, indexable |
| Migrations | Test against staging, deploy via CI | Never drop columns in the same migration that adds replacements |
| Incident Response | Detect → Assess → Mitigate → Retrospective | The fastest fix is almost always a rollback |