Zero-Migration Encryption: Building Drop-in Encrypted Field in Django
summary
Sentry builds EncryptedFields, a custom Field base class that replaces CharField, JSONField and TextField as a drop-in, no database migration solution, handling both old plain-text and new encrypted values in a hybrid state by encrypting in get_prep_value and decrypting in from_db_value. Values carry a magic prefix plus a key ID so key rotation uses the right key; keys are mounted as files. Backfill runs as a separate job for billions of rows, runtime cost is <1 ms, tens of encrypts/sec and hundreds of decrypts/sec, and filtering on encrypted columns is intentionally unsupported.
AI-written summary · about
chapters (12)
- 0:00Introduction to zero-migration encryption
- 1:09Encryption challenge at Sentry
- 3:19Requirements for encryption solution
- 4:51Zero-downtime migration constraints
- 7:19Traditional migration approach
- 9:05Django field abstraction for encryption
- 10:09No-migration encryption concept
- 11:13Handling encrypted and plaintext data
- 12:16Key rotation and multi-key support
- 14:03Encryption for JSON fields
- 15:07Implementation and performance
- 16:53Q&A session
