We are designing a progressive rollout strategy for a database index. The stages are:
- Shadow: Build the new index alongside the existing one without affecting the application.
- Partial: Start using the new index for a small subset of queries or users.
- Backfill: If the index is being built on existing data, we might need to backfill historical data.
- Swap: Switch the application to use the new index for all queries.
- Rollback: Have a plan to revert to the old index if something goes wrong.
Let's break down each step:
-
Shadow:
- Create the new index in the database (e.g., CREATE INDEX CONCURRENTLY for PostgreSQL to avoid locking).
- The application continues to use the old index. The new index is being built in the background.
- We monitor the build process and ensure it completes without issues.
-
Partial:
- Begin routing a small percentage of read queries (or a specific set of users) to use the new index.
- This can be done by modifying the application logic (feature flag) or by using a proxy that routes based on some criteria.
- We monitor the performance and correctness of the new index for this subset.
-
Backfill:
- If the index is on a table that already has a lot of data, and we are using a method that doesn't automatically index existing rows (like some online index builds in some databases), we might need to backfill.
- However, note that in many databases (like PostgreSQL with CREATE INDEX CONCURRENTLY) the index is built for existing data during the shadow phase. So backfill might not be needed separately.
- But if we are using a different strategy (like creating an index on a new table and then swapping), then we might need to backfill the historical data. In that case, we do the backfill during the shadow phase or in a separate step.
-
Swap:
- Once we are confident in the new index, we switch all traffic to use it.
- This might involve:
a. Changing the application to use the new index (by removing the old index from queries, or changing the index name if we are using the same name and have dropped the old one).
b. Or, if we have two indexes and we want to drop the old one, we can drop the old index after ensuring the new one is used.
- We must do this in a way that minimizes downtime. For example, we can use a rolling deployment if the index change is in the application code.
-
Rollback:
- If after the swap we notice issues, we need to revert to the old index quickly.
- This is why we keep the old index until we are sure the new one is working. We don't drop the old index until we are confident.
- The rollback plan might involve:
a. Reverting the application code to use the old index (if we changed the code to use the new index by name).
b. If we dropped the old index, we might have to recreate it, which could be time-consuming. Therefore, it's better to keep the old index until the end.
But note: In some databases, having two indexes on the same columns might be redundant and can slow down writes. So we might want to drop the old index at some point.
Let's consider a more detailed plan for a PostgreSQL example:
Step 1: Shadow
- Run: CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
- This builds the index without blocking writes. We monitor the progress.
Step 2: Partial
- We change the application to use the new index for a small set of queries (maybe by adding a hint or by changing the query for a specific user segment). Alternatively, if we are using an ORM, we might change the mapping for a subset of models.
Step 3: Backfill
- In the case of PostgreSQL's concurrent index build, the index is built for existing rows during the shadow phase. So no separate backfill is needed.
Step 4: Swap
- We change the application to use the new index for all queries. This might be done by:
a. Removing the old index from the queries (if we had set the index by name) and letting the optimizer choose (but note: we want to use the new index, so we might have to set the index name in the query or change the schema to make the new index the one that matches).
b. Alternatively, we can drop the old index and then the new index will be the only one. But dropping an index can lock the table (unless we use CONCURRENTLY). So we might do:
DROP INDEX CONCURRENTLY old_index_name;
However, note that if we drop the old index and the new index is not yet being used by all queries, we might have a period where some queries don't use an index. So we should only drop the old index after we are sure the new index is used by all queries.
A safer approach:
- First, change the application to use the new index by name (so we are sure it's used). Then, after monitoring, drop the old index.
Step 5: Rollback
- If we haven't dropped the old index, then we can simply revert the application to use the old index (by changing the index name in the queries) and then drop the new index.
- If we have dropped the old index, then we have to recreate it. This is why we keep the old index until we are very confident.
But note: if we are using the index by name in the queries, then we have two indexes with two different names. We can switch the name by using a view or by changing the query. Alternatively, we can rename the indexes:
- We can have the new index with the same name as the old one by doing:
CREATE INDEX CONCURRENTLY new_index_name ...;
Then, when we are ready, we drop the old index and rename the new index to the old name? But we cannot have two indexes with the same name.
Alternatively, we can do:
Step 1: Create new index with a temporary name.
Step 2: When ready, drop the old index and rename the new index to the old name. But renaming an index is fast (metadata only). However, we must be cautious because during the rename, queries that use the old index by name will break.
So a better approach might be:
- In the application, we use a configuration or a feature flag to switch between two index names. We set the flag to use the new index name for a subset, then for all. Then we can drop the old index. And then, if we need to rollback, we revert the flag to use the old index name (but the old index is gone). So we must keep the old index until we are ready to drop it, and only drop it when we are sure we don't need to rollback.
Alternatively, we can do:
- Use the same index name by creating the new index with the same name after dropping the old one? But that would cause a period without an index.
So the safest is:
- Create new index with a new name (shadow).
- Use the new index by name for a subset of queries (partial).
- Use the new index by name for all queries (swap). At this point, we are using the new index by name and the old index is still there but not used.
- We can then drop the old index (using CONCURRENTLY to avoid locking). After dropping, we are only using the new index.
- If we need to rollback after step 4, we have to recreate the old index, which might take time.
Therefore, we might want to keep the old index for a while after step 3 (swap) and only drop it after a certain period of stability.
Alternatively, we can do:
- After step 3 (all queries using new index by name), we can change the application to not specify the index by name and rely on the optimizer. Then we can drop the old index. But then if the new index is not chosen by the optimizer for some queries, we might have a problem.
So the approach of specifying the index by name in the queries (using an index hint) is more controlled but might not be portable across databases.
Given the complexity, let's assume we are using PostgreSQL and we can use index hints (via SET enable_indexscan = on or by using /*+ Index(table index) */ if using a tool that supports it, but note that PostgreSQL doesn't support index hints natively. So we might have to rely on the optimizer or use a different method.
Alternatively, we can change the table to have the new index as the one that matches the query pattern by having the same expression and column order. Then the optimizer will choose it if it's more efficient.
Given the above, let's design a plan that doesn't rely on index hints (since they are not standard in PostgreSQL) and instead relies on the optimizer:
Step 1: Shadow
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
Step 2: Partial
We don't change the queries, but we monitor the new index usage. We can set pg_stat_user_indexes to see if the new index is being used. But since we haven't changed the queries, the old index is still being used. So we need to change the queries to use the new index for a subset.
How? We can change the application code for a subset of users to use a different query that specifies the new index? Without index hints, we can't force an index. So we might have to rely on the optimizer to choose the new index if it's more efficient.
Alternatively, we can create the new index with the same name as the old one? But we can't have two indexes with the same name.
Another idea: we can create a new index with a different name and then change the query to use a different index by using a different column order or expression? That's not reliable.
So in PostgreSQL, without index hints, we have to rely on the optimizer. Therefore, the partial step might not be about forcing the index but about having the new index available and then checking if the optimizer uses it for some queries (by analyzing the query plans). Then we can adjust the index (like adding INCLUDE columns, changing the order) to make it more attractive.
Alternatively, we can use a different approach: we can create a new table with the same structure and the new index, then use a trigger to keep it in sync, and then switch the application to use the new table. But that's a different strategy (table swap) and might be more complex.
Given the constraints of PostgreSQL, let's assume we are allowed to use index hints via an extension or by using a different database that supports hints. Or we are using an ORM that allows specifying the index.
For the sake of this design, let's assume we can specify the index in the query (using a hint). Then:
Step 1: Shadow
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
Step 2: Partial
For a subset of users (or a subset of queries), we change the query to use the new index by hint.
Step 3: Backfill
Not needed because we used CONCURRENTLY.
Step 4: Swap
Change all queries to use the new index by hint.
Step 5: Rollback
If we haven't dropped the old index, we can change the queries back to use the old index by hint.
Step 6: Cleanup
After a period of stability, drop the old index with:
DROP INDEX CONCURRENTLY old_index_name;
But note: if we are using hints, we are forcing the index. So even if the new index is not optimal for some queries, we are forcing it. That might be a problem.
Alternatively, we can do:
Step 1: Shadow
Create the new index with a different name.
Step 2: Partial
We don't change the queries. We just monitor the new index usage. We hope that the optimizer will start using it for some queries if it's better. We can also adjust the random_page_cost and other settings to make the new index more attractive.
Step 3: Swap
We change the queries to use the new index by hint for all queries.
Step 4: Rollback
Change the queries back to the old index by hint.
Step 5: Cleanup
Drop the old index.
This is a common pattern: create the index, let the optimizer use it if it's better (for some queries) and then force it for all.
But without hints, we have to rely on the optimizer and the index being more efficient. So the partial step is more about monitoring and less about controlling.
Given the above, let's design a plan that works without hints and with the optimizer:
Step 1: Shadow
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
Step 2: Partial (monitor and adjust)
We monitor the query plans and index usage. We might adjust the index (add INCLUDE columns, change the column order) to make it more efficient for our queries. We also monitor the performance of the queries that we hope will use the new index.
Step 3: Swap (force the index by making it the only one that matches)
We can drop the old index and then rename the new index to the old index name? But that would cause a period without an index.
Alternatively, we can create the new index with the same columns and same name by doing:
DROP INDEX CONCURRENTLY old_index_name;
CREATE INDEX CONCURRENTLY old_index_name ON table_name (column_name);
But that would rebuild the index, which we already have. So we can do:
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
DROP INDEX CONCURRENTLY old_index_name;
ALTER INDEX new_index_name RENAME TO old_index_name;
This way, we end up with an index named `old_index_name` that is the new index. The application doesn't need to change because the index name is the same.
However, note that during the DROP and RENAME, there is a short period where the index `old_index_name` does not exist. But if we are using the index by name in the queries, then during that period, queries might fail.
So we must do:
Step 3a: Change the application to not use the index by name (if it was) and rely on the optimizer? But then we are back to the same problem.
Therefore, the rename approach is only safe if the application does not specify the index by name and relies on the optimizer.
Given the complexity, let's assume the application does not specify the index by name. Then:
Step 1: Shadow
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
Step 2: Partial
We monitor and adjust. We might also adjust the query patterns to use the new index (by rewriting queries to match the new index's column order or expression).
Step 3: Swap
We drop the old index and rename the new index to the old name.
Step 4: Rollback
If we have already dropped the old index and renamed the new one, then to rollback we have to recreate the old index and then rename again? That would be:
CREATE INDEX CONCURRENTLY old_index_name ON table_name (column_name); -- but this is the old index definition, which might be different from the new one?
Then we would have to drop the new index and rename the old one? But note: the new index might have a different definition (like column order). So we have to be cautious.
Alternatively, we don't drop the old index until we are sure. So:
Step 3: We rename the new index to the old name, but we keep the old index with a different name? We cannot have two indexes with the same name.
So we must drop the old index before renaming? Then we lose the old index.
Therefore, we should not drop the old index until we are ready to clean up. And we should not rename the new index to the old name until we have dropped the old index? But then we have a gap.
A better approach:
Step 1: Create new index with a new name.
Step 2: When ready, we change the application to use the new index by name (if we are using hints) or we adjust the queries to match the new index and let the optimizer choose.
Step 3: After we are sure the new index is used, we drop the old index.
Step 4: Then we can rename the new index to the old name if we want to keep the naming convention.
But if we are not using hints, then we have to rely on the optimizer. So we might not need to change the index name.
Given the above, let's design a plan that works for an application that does not use index hints and relies on the optimizer:
Step 1: Shadow
CREATE INDEX CONCURRENTLY new_index_name ON table_name (column_name);
Step 2: Partial
We monitor the query plans and index usage. We might adjust the index (add INCLUDE, change column order) to make it more efficient. We also monitor the performance of the queries.
Step 3: Swap
We drop the old index. Then the new index is the only one. The optimizer will use it if it's the best.
But wait: what if the old index was used by some queries because of the column order? Then after dropping the old index, the new index might not be used if the column order is different? So we must ensure the new index is at least as good as the old one for all queries.
Therefore, we must design the new index to match the query patterns. And we must test that the optimizer chooses the new index for all the queries that used the old index.
Step 4: Rollback
If we drop the old index and then the new index is not used by some queries, we have to recreate the old index. That might be time-consuming.
So we should keep the old index until we are sure the new index is used by all queries. How do we know? We can monitor `pg_stat_user_indexes` and `pg_stat_user_tables` to see if the new index is being used and the old index is not.
Therefore:
Step 3: We do not drop the old index until we see that the new index is being used for all the queries that previously used the old index. We can do this by:
SELECT * FROM pg_stat_user_indexes WHERE relname = 'table_name' AND indexrelname IN ('old_index_name', 'new_index_name');
We want to see that `idx_scan` for the new index is increasing and for the old index is decreasing (or zero for new queries).
Then, when we are confident, we drop the old index.
Step 5: Cleanup
After dropping the old index, we can rename the new index to the old name if we want.
But note: if we are using the index by name in the application (like in query hints), then we cannot drop the old index until we change the application to use the new index by name. And then we can drop the old index.
So the plan depends on whether we use index hints or not.
Given the complexity and the fact that the original question does not specify the database or the application's use of index hints, I will provide a generic plan that can be adapted:
Generic Progressive Rollout for Database Index:
-
Shadow:
- Create the new index in the database in a way that does not block production (e.g., CONCURRENTLY in PostgreSQL, ONLINE in MySQL, etc.).
- The application continues to use the old index.
-
Partial:
- Route a small percentage of read traffic (or a specific set of users) to use the new index. This can be done by:
a. Using a feature flag in the application to change the query for a subset of users (if using index hints or if the index name is in the query).
b. Or, if not using hints, adjust the query for a subset of users to match the new index's column order or expression so that the optimizer chooses the new index.
- Monitor the performance and correctness for this subset.
-
Backfill:
- If the index is on a table with existing data and the index creation method does not automatically index existing rows (rare in modern databases for online index builds), then we need to backfill. However, for most online index builds, this is done during the shadow phase. So this step might be omitted.
-
Swap:
- Gradually increase the traffic using the new index until 100%.
- This can be done by expanding the feature flag or by changing the application to use the new index for all queries (by hint or by query rewrite).
- Alternatively, if we are not using hints and we have ensured the new index is used by the optimizer for all queries, we can simply drop the old index and the new index will be used.
-
Rollback:
- At any point before dropping the old index, we can revert to the old index by:
a. Turning off the feature flag (if using hints or query rewrite for the new index).
b. If we have dropped the old index, we must recreate it. This is why we keep the old index until we are confident.
-
Cleanup:
- After a period of stability (e.g., a few days) and once we are confident the new index is working, we can drop the old index (using an online method if available).
Additional considerations:
-
Write performance: Having two indexes might slow down writes. So we should consider the write load and the impact of having two indexes during the rollout. We might need to adjust the rollout speed based on write load.
-
Storage: Two indexes take twice the storage. Ensure we have enough disk space.
-
Database-specific features: Use the database's online index creation and drop features to avoid locking.
-
Monitoring: Continuously monitor the index usage, query performance, and system resources (CPU, I/O, disk space).
-
Testing: Test the entire process in a staging environment that mirrors production.
-
Documentation: Document the steps and the rollback plan.
This is a high-level plan and should be adapted to the specific database and application architecture.
I'll design a comprehensive progressive index rollout strategy with clear stages and safety measures. This approach minimizes risk while allowing validation at each step.
Progressive Database Index Rollout Strategy
Phase 0: Pre-Rollout Preparation
1. Benchmarking
- Test new index on staging with production-like data
- Measure query performance improvements (target queries only)
- Assess write performance impact (additional index maintenance)
- Verify EXPLAIN plans show desired index usage
2. Feature Flag Setup
- Create config flag: `USE_NEW_INDEX_PERCENT` (0-100)
- Add query hints or route logic to support dynamic index selection
- Implement index usage telemetry (pg_stat_statements, slow query logs)
3. Backup & Monitoring
- Ensure recent full backup exists
- Set up dashboards for:
* Index scan counts (old vs new)
* Query latency percentiles
* Write throughput/IOPS
* Storage growth
- Configure alerts for regression thresholds (>10% slowdown, error spikes)
4. Rollback Documentation
- Document exact rollback commands/timing
- Prepare rollback runbook with contact info
- Test rollback in staging
Stage 1: Shadow Index (Zero Risk)
Goal: Build index without affecting production traffic
PostgreSQL Example:
-- Create concurrently (non-blocking)
CREATE INDEX CONCURRENTLY CONCURRENTLY idx_new_table_column
ON table_name (column_name)
WHERE <optional conditions>;
-- For partial indexes or covering indexes:
CREATE INDEX CONCURRENTLY idx_new_covering
ON table_name (column1, column2)
INCLUDE (column3, column4);
MySQL Example:
-- ALGORITHM=INPLACE, LOCK=NONE (MySQL 5.6+)
CREATE INDEX idx_new_column ON table_name (column_name)
ALGORITHM=INPLACE, LOCK=NONE;
Validation:
- Monitor
pg_stat_progress_create_index (PostgreSQL) or information_schema.INNODB_LOCKS (MySQL)
- Verify index appears in
pg_indexes/information_schema.statistics
- No application changes - index remains unused
- Duration: Typically 1-24 hours depending on table size
Stage 2: Partial Rollout (Controlled Exposure)
Goal: Route 1-5% of eligible queries to new index
Implementation Options:
Option A: Application-Level Routing (Most Flexible)
# Feature flag + query routing
if should_use_new_index(user_id): # Hash-based or random
query = f"SELECT /*+ INDEX(table idx_new_column) */ * FROM table WHERE ..."
else:
query = "SELECT * FROM table WHERE ..." # Let optimizer choose
Option B: Database Query Rewrite (PostgreSQL)
-- Using pg_hint_plan extension or in-app query modification
SET LOCAL pg_hint_plan.enable_hint = on;
SELECT /*+ Index(table idx_new_column) */ * FROM table WHERE condition;
Option C: Proxy Layer (PgBouncer/HAProxy)
-- Route based on user/job type in connection pool
-- Or use statement-level routing with query rewriting middleware
Validation Metrics (5-15 minute windows):
- ✅ New index scan count increasing
- ✅ Query latency within 5% of baseline for sampled queries
- ✅ No increase in lock wait time or deadlocks
- ✅ Write performance within 3% of baseline
Gradual Increase:
Day 1: 1% traffic → 24h监控
Day 2: 5% traffic → 24h监控
Day 3: 25% traffic → 24h监控
Day 4: 50% traffic → 24h监控
Day 5: 100% traffic (if all metrics green)
Stage 3: Backfill (If Needed)
Only required if: Building partial index with WHERE clause and existing data needs indexing
PostgreSQL Partial Index Backfill:
-- 1. Create UNLOGGED table for backfill (faster, no WAL)
CREATE UNLOGGED TABLE backfill_temp AS
SELECT * FROM main_table WHERE NOT (your_partial_condition);
-- 2. Build index on temp table
CREATE INDEX CONCURRENTLY idx_backfill ON backfill_temp(column);
-- 3. Swap with minimal lock
BEGIN;
LOCK TABLE main_table IN SHARE MODE; -- Brief lock
INSERT INTO main_table
SELECT * FROM backfill_temp
ON CONFLICT DO NOTHING;
COMMIT; -- Lock released quickly
-- 4. Cleanup
DROP TABLE backfill_temp;
Alternative: Batched Updates
-- Process in batches to avoid long locks
UPDATE main_table SET indexed_column = indexed_column
WHERE id BETWEEN x AND y
AND NOT (partial_condition);
Validation:
- ✅ All existing rows now satisfy partial index condition
- ✅ Query results identical before/after backfill
- ✅ No long-running transactions (> 30s)
Stage 4: Full Swap (Cutover)
Goal: 100% traffic to new index, deprecate old index
Step 1: Warm-up Period
-- Monitor for 24h with 100% traffic but keep old index
-- Ensure:
-- * Query plans use new index (check pg_stat_user_indexes)
-- * No regression in P99 latency
-- * Cache hit rates stable
Step 2: Old Index Deprecation
-- Option A: Gradually reduce old index usage (if query hints used)
-- Remove hints pointing to old index in code deploy
-- Option B: If old index still being used by optimizer:
ALTER SYSTEM SET enable_seqscan = off; -- Temporarily for testing
-- Verify all queries use new index via pg_stat_statements
RESET enable_seqscan;
Step 3: Old Index Removal (After 48h stability)
-- PostgreSQL
DROP INDEX CONCURRENTLY IF EXISTS old_index_name;
-- MySQL
ALTER TABLE table_name DROP INDEX old_index_name, ALGORITHM=INPLACE, LOCK=NONE;
Step 4: Update Documentation
- Update ER diagrams, data dictionary
- Document new index purpose and query patterns it serves
- Update query optimization guidelines
Stage 5: Rollback Plan (Emergency Procedures)
Immediate Rollback (< 15 min detection):
-- If still in partial rollout (old index exists):
1. Set feature flag to 0% (immediate via config reload)
2. Verify traffic returns to old index
3. Investigate root cause
-- If already swapped and old index dropped:
1. Recreate old index CONCURRENTLY:
CREATE INDEX CONCURRENTLY old_index_name ON table_name(columns);
2. Update application to force old index via hints
3. Monitor for 30min, then decide:
a) Keep both indexes (if storage/cost allows)
b) Drop new index after validation
Rollback Triggers:
critical:
- error_rate_increase: ">5%"
- p99_latency_increase: ">200ms"
- deadlock_rate: ">1/minute"
- replication_lag: ">30s"
warning:
- write_throughput_drop: ">10%"
- storage_growth: ">5% in 24h"
- cache_hit_rate_drop: ">15%"
Communication Plan:
- Detection: Automated alert → On-call engineer
- Assessment: 5min to confirm trigger → 5min to decide rollback
- Execution: 5-15min to execute rollback commands
- Verification: 15min to confirm metrics return to baseline
- Post-mortem: Within 24 hours
Success Criteria & Metrics
| Stage | Success Criteria | Rollback Threshold |
|---|
| Shadow | Index created without errors | Build fails or >24h |
| Partial (1%) | No error increase, latency within 5% | Error rate ↑5%, latency ↑10% |
| Partial (25%) | Write throughput within 3%, new index scans >50% | Write throughput ↓5% |
| Full Swap | All target queries use new index | Any target query not using new index |
| Stabilization | 48h no regression | Any critical alert triggers |
Special Considerations
1. Multi-Region/Replica Rollout:
Step 1: Build on read replicas first (shadow)
Step 2: Partial on replicas (no write impact)
Step 3: Promote replica with new index → test
Step 4: Cutover primary
2. Large Tables (>1TB):
3. Write-Heavy Tables:
- Monitor
idx_scan vs idx_tup_ins/upd/del ratio
- Consider
CREATE INDEX CONCURRENTLY ... WITH (deduplicate_items = OFF) if using BRIN
- Test with
SET random_page_cost = 1.1; if SSD storage
4. Zero-Downtime Requirement:
- All DDL must be
CONCURRENTLY (PostgreSQL) or ALGORITHM=INPLACE, LOCK=NONE (MySQL)
- Never drop old index until new index proven for 48h
- Use connection pool draining during cutover if changing index names
Rollback Decision Tree
Rollback needed?
├─ YES → Is old index still present?
│ ├─ YES → Feature flag to 0% → Monitor 15min → Success?
│ │ └─ NO → Recreate old index CONCURRENTLY → Force old index
│ └─ NO → Recreate old index → Force old index → Drop new index after stability
└─ NO → Continue monitoring → After 48h: Drop old index CONCURRENTLY
Post-Rollout Checklist
This strategy provides multiple safety nets at each stage, allowing you to validate performance gains while maintaining the ability to rollback within minutes. The key is never dropping the old index until you have 48+ hours of 100% stable operation with the new index.