Changing a shape that is already in production
Adding a field is easy. Renaming one while a million documents and two deployed versions disagree is the actual job.
The safe shape for any schema change is expand, migrate, contract. Deploy code that writes both the old and new field and reads either. Backfill the existing documents in batches. Then, once nothing reads the old field, deploy code that drops it and remove it from the data. Three deploys instead of one, no downtime, and every intermediate state is a state your running code can handle.
Write the backfill so it can be run twice. It will be: it will time out, or hit a rate limit, or be killed halfway, and someone will start it again. Batch by id, record progress, make each batch idempotent, and log the count. A backfill you are afraid to re-run is a backfill you will be afraid to finish.
You should now be able to
- Plan an expand-migrate-contract change
- Write a backfill that can be run twice safely
Loading…