Prompt
How do I set up staging, preview deploys, and rollbacks for a SaaS app?
Latest observation
A solid setup usually has three layers:
- Staging — a safe, stable environment that mirrors production
- Preview deploys — short-lived environments per branch/PR
- Rollbacks — a fast way to revert production if something breaks
Here’s a practical way to design it for a SaaS app.
1) Environment model
Use separate environments with clear purposes:
- Local: developer machines
- Preview: per pull request / branch
- Staging: production-like, for final verification
- Production: real users
Recommended rules
- Preview: ephemeral, auto-created, auto-destroyed
- Staging: long-lived, closely matches prod, used for QA and release checks
- Production: only deploy from approved commits/tags
2) Staging setup
Staging should be as close to production as possible.
What should match production
- Same app build artifact/container image
- Same runtime version
- Same infrastructure shape where possible
- Same auth flow types, background jobs, caches, queues
- Same observability stack
- Similar DB engine/version
What should differ
- Smaller size/cost
- Non-production credentials
- Sanitized or synthetic data
- Separate third-party sandbox accounts
- Fewer workers/replicas if needed
Good staging practices
- Deploy automatically after merge to main or release branch
- Run migration checks and smoke tests there
- Use staging as the final gate before production
- Protect it from manual drift: same IaC, same deployment pipeline
3) Preview deploys
Preview environments are ideal for PR validation and product review.
Common pattern
When a PR is opened:
- Build the app
- Create a unique environment, such as:
pr-123.preview.example.com- or
preview-branch-name.example.com
- Deploy the code and any required services
- Comment the URL back on the PR
When the PR closes:
- Destroy the environment
What to include
- App frontend/backend
- Feature flags in preview mode
- A temporary DB or isolated schema
- Seeded data for testing
- Basic auth if exposed publicly
Preview data strategy
Choose one:
- Ephemeral DB per preview: best isolation, more expensive
- Shared DB with per-preview schema: simpler, medium isolation
- Shared staging-like services with isolated app: cheaper, but riskier
For SaaS apps, a good default is:
- Shared non-prod infrastructure
- Separate database/schema per preview
- Short retention policy
Avoid
- Using production databases
- Long-lived preview environments
- Manual setup for each branch
4) Rollback strategy
Rollback is not just “redeploy the old code.” You need a plan for app code, config, and database changes.
Best rollback options
A. Blue-green deployment
- Run two production environments: blue and green
- Deploy to the inactive one
- Switch traffic after health checks
- If needed, switch back instantly
Best for:
- Fast rollback
- Low-risk releases
B. Rolling deployment with versioned releases
- Deploy new version gradually
- Keep the previous version ready
- Roll back by redeploying the previous artifact
Best for:
- Kubernetes, ECS, Nomad, etc.
C. Canary deployment
- Send a small % of traffic to the new version
- Increase gradually if healthy
- Roll back by cutting off the canary
Best for:
- Lower-risk experimentation
- Larger SaaS apps with mature observability
Golden rule
Always keep the last known good artifact/image so rollback is a redeploy, not a rebuild.
5) Database migrations and rollbacks
This is where many rollbacks fail.
Safe migration pattern: expand and contract
- Expand: add new columns/tables without removing old ones
- Deploy code that can work with both old and new schema
- Backfill data if needed
- Switch reads/writes to new schema
- Contract later: remove old columns after confidence
Avoid
- Destructive migrations in the same deploy as app changes
- Renaming/removing columns immediately
- Relying on down migrations for complex schema changes
If rollback is needed
- App rollback should work against the new schema
- Schema rollback is often not practical or safe
- Design migrations to be backward compatible
6) Suggested CI/CD flow
A common workflow:
On pull request
- Run tests and linting
- Build artifact
- Deploy preview env
- Run smoke/e2e tests
- Post preview URL to PR
On merge to main
- Build immutable release artifact
- Deploy to staging
- Run integration tests, migrations checks, smoke tests
- Require manual approval or automated checks
- Deploy to production
- Monitor metrics/errors
On release failure
- Auto-detect health check failure
- Abort rollout or cut traffic
- Revert to previous stable version
- Notify team
7) Release and rollback mechanics
Make deployments immutable
Build once, deploy many:
- Same image/artifact to preview, staging, prod
- Different environment config injected at runtime
Version everything
- App image tags:
app:1.42.7 - DB migration version
- Config version
- Release notes tied to commit SHA
Store deployment metadata
For every deploy, record:
- Commit SHA
- Artifact digest
- Migration version
- Deployer identity
- Time deployed
- Environment URL
This makes rollback reliable and auditable.
8) Feature flags help a lot
Feature flags reduce rollback pressure.
Use them to:
- Hide unfinished features
- Gradually expose risky changes
- Turn off broken functionality without redeploying
Good for:
- UI changes
- New backend logic
- Migrations that can be dark-launched
But don’t use flags as a substitute for:
- Bad architecture
- Permanent cleanup of old code
9) Data considerations for SaaS
Because SaaS apps often have customer data and tenant isolation, be careful with:
- Multi-tenant schemas
- Tenant-specific migrations
- Background jobs tied to release version
- Webhook payload compatibility
- Search index updates
- Cache invalidation during rollback
Recommended:
- Make jobs idempotent
- Version APIs and webhook contracts
- Keep old consumers alive until all producers are updated
- Have a way to drain queues during rollback if necessary
10) Practical default architecture
If you want a simple, robust default:
- GitHub/GitLab PRs create preview deploys automatically
- Main branch auto-deploys to staging
- Production deploys are:
- built once
- manually approved
- released via blue-green or canary
- Rollback is:
- switch traffic back or redeploy previous image
- DB migrations follow expand/contract
- Use feature flags for risky changes
11) Minimal checklist
Preview deploys
- Auto-create on PR
- Unique URL
- Isolated DB/schema
- Auto-destroy on close
Staging
- Mirrors production
- Uses sanitized data
- Runs smoke/e2e tests
- Requires approval before prod
Rollbacks
- Keep last known good artifact
- Health checks before traffic shift
- Backward-compatible migrations
- Monitoring and alerting
- One-click rollback
12) Tooling examples
You can implement this with many stacks:
- GitHub Actions / GitLab CI / CircleCI for pipelines
- Vercel / Netlify for frontend previews
- Kubernetes / ECS / Nomad for backend deployments
- Terraform / Pulumi for infrastructure
- LaunchDarkly / Unleash for feature flags
- Postgres schemas / branches / disposable DBs for isolated data
If you want, I can turn this into:
- a reference architecture diagram,
- a GitHub Actions pipeline example, or
- a step-by-step setup for your stack (e.g. Next.js + Postgres + AWS/Kubernetes).