LOAD-BEARINGfree
High-level designTier 12 · unit 8 of 11 · 14 min read

Encryption, keys, and the field you cannot index

The last lesson said what the obligations are. This one is the machinery underneath them, and it is worth its own fourteen minutes because "we encrypt everything" is the single most common sentence in this area and it means almost nothing on its own. Encryption is not a property you switch on. It is a set of specific controls, each defeating a specific attacker, and the interesting question is always the same one: who holds the key, and what else can they reach?

Three layers, three different threats

  • In transit. TLS on the public edge, which everyone does, and TLS inside the perimeter, which is what Lesson 12.03's service identity was really about. The threat is somebody on the path. This one is close to free now and there is no remaining argument for plaintext between services.
  • At rest, at the volume. Full-disk or storage-level encryption, usually one checkbox. Be precise about what it defeats: somebody carrying away the physical disk, or reading a decommissioned one. It is a compliance requirement and it is genuinely worth having. It does exactly nothing against a leaked credential, a SQL injection, an over-broad IAM role, or a curious engineer with production read access — because to all of those, the database is mounted and decrypting cheerfully. Most breaches are of that kind, which is why "the data is encrypted at rest" is a true sentence that answers the wrong question.
  • At rest, in the application. The field arrives at the database already encrypted, so the database never holds the plaintext and neither does its backup, its replica, or the analyst with read access. This is the layer that defeats the threats the previous one does not — and it is also the one with real costs, which the rest of this lesson is about.

Envelope encryption, and why keys are not stored in config

You do not want one key for everything, and you do not want a million keys in a config file. The standard construction, and the one every cloud KMS implements:

Each object or record is encrypted with its own data key (DEK), generated fresh. That data key is itself encrypted by a key-encrypting key (KEK) that never leaves the key management service — hardware-backed, access-controlled, and every use audited. The encrypted data key is stored right next to the ciphertext it belongs to. To read the record you send the wrapped key to the KMS, get the plaintext key back, decrypt locally, and discard it.

Three consequences worth internalising. The KMS never sees your data, only small keys, so it is not a throughput bottleneck. Access to data becomes an auditable API call, which is how you get "who decrypted this customer's record, and when" — a question the disk-encryption checkbox can never answer. And revoking the KEK instantly invalidates every DEK it wrapped, without touching a byte of the ciphertext. That last property is the entire foundation of crypto-shredding: give each data principal their own key, and Lesson 12.07's right to erasure against an append-only log becomes a single key deletion instead of an impossible edit.

Rotation, stated accurately

"Rotate your keys" is repeated far more often than it is understood, and the confusion is that the two levels rotate very differently:

  • Rotating the KEK is cheap. Generate a new version, encrypt new data keys with it, and unwrap old ones with the old version, which you retain. No data is rewritten. This is what the automatic annual rotation in your cloud console does, and it is nearly free.
  • Rotating a DEK is expensive, because it means decrypting and re-encrypting the data. On a large table that is a background job with all of Lesson 14.03's migration machinery, and it needs to be designed rather than assumed.

So build for it now, at negligible cost: store a key identifier alongside every ciphertext. Decrypt by whatever key the record names, encrypt new writes with the current one, and re-encryption becomes a lazy background sweep rather than a flag day. A ciphertext that does not say which key made it is a ciphertext you can never rotate, and you find that out on the day it becomes urgent.

What application-level encryption breaks

This is the tradeoff people meet late, so meet it now. An encrypted column is opaque to the database. You cannot index it usefully, range-query it, sort by it, join on it, or search it — the ciphertext for the same plaintext is not even equal to itself if you are using a random IV, which you should be. Deterministic encryption restores equality lookups and leaks which rows share a value, which for something like a diagnosis code can be most of the information you were protecting.

The practical resolutions, in the order to consider them:

  • Encrypt what needs it, not the table. A name, a phone number and an ID document need protecting; the row's created-at and status do not. Blanket encryption is how teams end up with a table they cannot query at all.
  • Store a blind index for the lookups you genuinely need — a keyed hash of the normalised value, so "find the user with this phone number" works as an exact match while range and prefix queries stay impossible. Accept that this leaks equality, deliberately, having noticed.
  • Prefer tokenisation where it applies. Lesson 12.06 made the argument: a vendor keeps the real value and you hold an opaque token that is safe to index, log, join and put in the warehouse. You have solved the query problem by no longer holding the data, which is always the cheapest answer available.

And the derived-data corollary that catches everyone. Your CDC stream, your warehouse, your search index and your event log all carry copies of these fields (Tier 7). If encryption happens at the application, the pipeline has to be told what it is carrying — decrypt at the boundary under a separate key with separate access, or better, mask the field so the analytical plane never receives it. A perfectly encrypted OLTP column that arrives in the warehouse as plaintext because the pipeline was written by another team is one of the more common ways this control quietly fails.

The trap

Encrypting the data and leaving the key reachable from the same compromised place. An application that can decrypt any record at any time, from an unrestricted role, has bought a compliance artifact rather than a defence — the attacker who has your service credentials also has your decryption. Key access is the real boundary, so it deserves the design attention: narrow the role that may call decrypt, rate limit it so a bulk export looks different from serving one screen, alert on volume, and keep the key in an account the application does not administer. And check the same question about your backups (9.10), because a copy of the ciphertext and a copy of the key in one bucket is one copy of the plaintext.