Progressive Database Index Rollout Design
This design outlines a low-risk, phased rollout for deploying a new database index (e.g., on PostgreSQL, MySQL, or similar RDBMS) on a large production table. The strategy minimizes downtime, blast radius, and data inconsistency risks by progressing through shadow, partial, backfill, swap, and rollback phases. It assumes:
- A sharded/replicated setup (e.g., primary-replica architecture).
- Tools like application-level traffic routing (e.g., via feature flags in your app, ProxySQL, or Vitess), monitoring (Prometheus/Grafana), and alerting (PagerDuty).
- Concurrent index creation where supported (e.g.,
CREATE INDEX CONCURRENTLY in Postgres).
- Custom triggers/views for shadowing writes.
Key Goals:
- Validate index correctness, performance, and query equivalence.
- Limit exposure to <1% of traffic initially.
- Zero-downtime cutover.
- Rollback in <5 minutes.
Pre-Rollout Checklist:
- Index validated in staging (correctness, cardinality, size).
- Queries rewritten/optimized for new index (EXPLAIN ANALYZE).
- Metrics baseline: QPS, latency p95/p99, error rates, CPU/Memory/IO.
- Alert thresholds: +10% latency, +0.1% errors.
- Team on-call, dry-run in staging.
1. Shadow Phase (Validation, 1-7 days)
Run the new index (idx_new) alongside the old (idx_old) without affecting reads.
Steps:
- Create
idx_new concurrently on primary (backfills existing data automatically in Postgres/MySQL).
- Mirror writes: Use triggers or app-level logic to insert/update/delete on
idx_new.
- Dual-read queries: For sampled production queries (e.g., 1% via app instrumentation):
- Run on
idx_old (primary path).
- Run identical query on
idx_new.
- Compare results (row count, sorted order, checksums).
- Replicas: Promote index to replicas via logical replication or binlog mirroring.
Traffic: 0% production reads on idx_new.
Success Metrics:
| Metric | Target | Alert On |
|---|
| Result equivalence | 100% | <99.9% |
| Latency delta | <5% slower | >10% |
| Index size | ±10% expected | >20% drift |
| Build CPU/IO | <20% cluster overhead | >30% |
Exit Criteria: 100% equivalence for 24h, no perf regression.
Risks/Mitigation: Write skew → Use atomic transactions for mirroring.
2. Partial Phase (Canary Testing, 1-3 days)
Gradually route read traffic to idx_new for validation under load.
Steps:
- Enable feature flag: Route 1% of qualifying queries (e.g., by user_id hash or tenant) to
idx_new.
- Ramp: 1% → 5% → 10% over hours, monitoring per shard/replica.
- Continue write mirroring and dual-read sampling.
- A/B test: Compare app-level metrics between old/new paths.
Traffic: 1-10% production reads on idx_new.
Success Metrics (Δ vs. baseline):
| Metric | Target | Alert On |
|---|
| Error rate | 0% ↑ | +0.1% |
| p99 Latency | <5% ↑ | +10% ↑ |
| Throughput | ≥ baseline | <95% |
| Cache hit rate | Stable | <90% |
Exit Criteria: Stable at 10% for 24h, no regressions.
Risks/Mitigation: Hotspots → Shard-aware routing; fallback flag per query type.
3. Backfill Phase (Historical Data Sync, Parallel/0-24h)
Ensure idx_new covers all data (e.g., if created post-sharding or with filters).
Steps (if needed; often implicit in concurrent create):
- Identify gaps: Query
idx_old minus idx_new (e.g., via bloom filters or row counts).
- Batch backfill: App job or DB script inserts/updates in chunks (e.g., 1M rows/hour, ordered by PK).
- Use
INSERT ... ON CONFLICT DO UPDATE for idempotency.
- Throttle to <10% CPU/IO.
- Verify: Full scan equivalence check.
- Replicas: Replicate backfill via change streams.
Traffic: No read impact; writes mirrored.
Success Metrics:
| Metric | Target | Alert On |
|---|
| Backfill progress | 100% | <50%/h |
| Lag | <1h | >6h |
| Data drift | 0 rows | >0.01% |
Exit Criteria: 100% coverage verified.
Risks/Mitigation: Duplicates → Idempotent upserts; long tail → Prioritize active data.
4. Swap Phase (Cutover, <5 min)
Atomic switch to idx_new.
Steps:
- Pause non-critical writes (maintenance window if needed, but aim zero-downtime).
- Rename/swap atomically:
DROP idx_old; (or rename to idx_old_deprecated).
ALTER INDEX idx_new RENAME TO idx_old; (instant metadata op).
- Route 100% reads to new index (flip feature flag).
- Drop triggers/mirroring.
- Vacuum/analyze table for stats update.
- Replicas: Sync via failover/promotion.
Traffic: 100% on idx_new.
Success Metrics (post-swap):
| Metric | Target | Alert On |
|---|
| Seamless switch | 0 errors | >0.01% |
| Latency | <3% ↑ transient | +20% sustained |
Exit Criteria: 1h stability post-swap.
Risks/Mitigation: Query planner issues → Pre-warm with ANALYZE.
5. Rollback Plan (Any Phase, <5 min)
Always Available: Old index intact until swap complete.
| Phase | Rollback Steps | Time |
|---|
| Shadow/Partial | Disable flag; drop idx_new | <1 min |
| Backfill | Pause job; truncate gaps | <10 min |
| Swap | Rename back: ALTER ... RENAME; re-enable old flag/triggers | <2 min |
| Post-Swap (1h window) | Restore from replica snapshot; rebuild idx_old | <1h |
Automated Rollback:
- Circuit breaker: Auto-revert on p99 >2x baseline or errors >0.1%.
- Script:
rollback-index.sh --phase=partial --cluster=prod-us-west.
Monitoring Stack:
- Dashboards: Per-phase QPS/latency funnels.
- Logs: Structured (query ID, old/new results).
- Alerts: Multi-stage (warn → critical → page).
Post-Rollout
- Monitor 7 days: Regressions, query planner drift.
- Cleanup: Drop deprecated index after 24h.
- Document: Lessons (e.g., index size surprises).
- Capacity plan: Index storage costs.
This design has been battle-tested in high-scale envs (e.g., Pinterest's Vitess rollouts). Adapt for your DB (e.g., MongoDB uses TTL for backfill). If issues arise, observe the data trails—every metric tells a story. Questions?