Lesson 9 of 9 · 26m
Schema Changes, Migrations, and Capacity Discipline
Run online DDL safely, choose migration tools, batch destructive changes, preserve replication health, and plan capacity before the cliff.
The schema migration that causes an outage is rarely exotic. It is a normal ALTER TABLE on a table that became large while nobody updated the migration playbook. Production schema work is capacity engineering, replication engineering, and rollback design compressed into one operation. If the plan is 'run it at night and watch', the plan is incomplete.
Know what kind of ALTER you are running
MySQL supports several DDL algorithms depending on operation and version:
ALTER TABLE orders
ADD COLUMN risk_score SMALLINT UNSIGNED NULL,
ALGORITHM=INPLACE,
LOCK=NONE;INSTANT operations can avoid rebuilding table data for certain metadata-only changes. INPLACE may still rebuild internally while permitting varying concurrency. COPY creates a new table copy and can block or overload the server. Exact support depends on MySQL version, column type, index operations, and ordering. Test on a restored production-scale copy.
Where supported, inspect performance_schema.metadata_locks and process state during a rehearsal. DDL can queue behind one long transaction and then block every new statement behind itself.
Use external online-schema-change tools intentionally
pt-online-schema-change and gh-ost reduce blocking by building a shadow table and synchronizing changes, then cutting over. They are not magic. Both add write overhead, storage requirements, replication load, triggers or binlog interaction complexity, and cutover risk.
gh-ost --host=db-primary.internal --user=osc --password="$GH_OST_PASSWORD" \
--database=appdb --table=orders --alter='ADD COLUMN risk_score SMALLINT UNSIGNED NULL' \
--max-load=Threads_running=50 --critical-load=Threads_running=100 \
--chunk-size=1000 --executeThe important controls are pause/resume behavior, replication-lag thresholds, load limits, cutover locking, cleanup, and rollback semantics. Read the tool's exact failure model before trusting it in production.
Large data changes need batches and invariants
Backfills and destructive updates should be chunked by primary-key range, not LIMIT offsets that rescans growing prefixes:
UPDATE orders
SET risk_score = 0
WHERE order_id >= 1000000
AND order_id < 1001000
AND risk_score IS NULL;Record progress outside memory, throttle based on replication lag and user-facing latency, and make batches idempotent. A batch job that cannot resume is an incident generator.
Capacity planning is trend analysis, not guessing
Track table growth, index growth, buffer-pool working set, binlog growth, connection ceiling, disk headroom, and backup duration. Forecast using business events, not just linear extrapolation. A migration that doubles write volume or a launch that triples order creation invalidates quiet-month assumptions.
SELECT TABLE_NAME, ROUND((DATA_LENGTH + INDEX_LENGTH)/1024/1024/1024, 2) AS gb, TABLE_ROWS
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'appdb'
ORDER BY DATA_LENGTH + INDEX_LENGTH DESC;TABLE_ROWS is approximate for InnoDB. Use it for sizing triage, not exact counts.
Rollback means data ownership
Reversing ADD COLUMN is easy compared with recovering writes made to a new structure after cutover. Decide whether post-cutover writes are discarded, dual-written, reverse-backfilled, or recovered through a controlled restore before execution.
Field notes
- Metadata locks can make a fast DDL effectively blocking because it queues behind long transactions.
- Shadow-table approaches need enough free disk for the copy plus indexes and binlog growth.
- Adding an index is write amplification forever, not only during migration.
DROP COLUMNand destructive cleanup deserve delayed deployment after application compatibility is proven.- Replicas must survive migration load; a successful source migration that destroys replication health is not success.
Was this lesson helpful?