skip to content

How are encryption keys organised and rotated for a database encrypted at rest, and what does envelope encryption — a data key wrapped by a key-encrypting key — buy you?

level: seniorimportance: must knowfreq 45%

answer

  1. DEK encrypts data, KEK wraps DEK
  2. KEK rotation = re-wrap, cheap; DEK rotation = re-encrypt everything
  3. per-tenant DEK → crypto-shredding
  4. key retention >= backup retention
  5. key service on the critical path for startup and restore

basics

~20 s

Data is encrypted with a data key; the data key is encrypted by a master key held in a key manager or HSM. Rotating the master key only re-wraps the data key — seconds. Rotating the data key means re-encrypting all data. Keep old key versions or old backups become unrestorable.

solid answer

~60 s

**Envelope encryption** uses two levels. A **data-encrypting key (DEK)** encrypts pages or column values and lives with the database, normally only in memory. A **key-encrypting key (KEK / master key)** lives in a key manager or HSM and encrypts the DEK; the wrapped DEK is stored next to the data, which is safe because it is ciphertext. What that buys: - **Cheap rotation.** Rotating the KEK re-wraps a small blob — no data is touched. Rotating the DEK re-encrypts everything, so you do it rarely, online, tagging each artifact with its key version. - **Custody separation.** The master key never leaves the HSM; administrators hold data, the security team holds the KEK grant, and neither alone can read the data. - **Crypto-shredding.** Per-tenant or per-tablespace DEKs mean destroying one key makes exactly that data unreadable — the practical way to honour deletion at scale. - **Audit.** Every unwrap is a logged key-service call, so unusual access is visible. The cost is a hard dependency: no key service, no startup — and a prematurely retired key version is permanent data loss.

go deeper

for a junior

Know that one key encrypts the data and another key protects that key, and that keys must not sit next to the data they protect.

for a middle

Explain envelope encryption and why rotating the master key is cheap while rotating the data key means re-encrypting everything.

for a senior

Cover separation of duties, version-tagged ciphertext for online rotation, key retention versus backup retention, crypto-shredding, and the availability dependency on the key service.

for a principal

Own the key hierarchy as an architectural decision: per-tenant scoping for deletion obligations, key-service availability in the recovery plan, custody policy, and rehearsed restore drills that exercise the whole path.

## The hierarchy Almost every real system uses at least two levels: - **DEK (data-encrypting key)** — the symmetric key that actually encrypts pages, tablespaces or column values. It must be available on every read, so it is cached in the database process's memory. - **KEK / master key** — encrypts ("wraps") the DEK. It lives in a key manager, HSM or cloud key service and ideally never leaves that boundary; the database sends the wrapped DEK and receives the unwrapped one at startup. The wrapped DEK is stored with the database (a keyfile, a control file, a catalog table). Because it is ciphertext, storing it beside the data is safe — which is the elegant part of the design. ## Why two levels **Rotation asymmetry.** Cryptographic hygiene and most compliance regimes ask for periodic key rotation. Re-encrypting terabytes annually is unattractive. With envelope encryption, "rotating the master key" means decrypting one small DEK blob and re-encrypting it under the new KEK: milliseconds, no data rewritten. That satisfies rotation for the key that is most exposed — the one referenced by many systems. Rotating the **DEK** is a different, expensive operation: every page or value must be read, decrypted under the old key and written back under the new one. Reserve it for suspected compromise or very long-lived keys, and run it online: keep both versions valid, tag each artifact with its key version, migrate in throttled batches. **Separation of duties.** The database administrator can read files and run the engine but does not hold the KEK; the key custodian can unwrap keys but has no database privileges. Compromising one role is not enough. Enforce it with key-service policy — the instance's identity may call unwrap, humans may not. **Scoped destruction.** Give each tenant, tablespace or backup set its own DEK. Deleting that DEK renders exactly that data unreadable without touching anything else — crypto-shredding, the only practical way to "delete" a tenant from years of immutable backups. **Audit.** Unwrap calls are logged centrally, so a restore or an unexpected instance startup produces a visible event. ## Operational realities **Availability dependency.** If the key service is unreachable, the instance cannot unwrap its DEK and may fail to start or fail to open a tablespace. Model this in the availability budget: regional redundancy, cached unwrapped keys where the engine allows it, and a documented break-glass path. **Backup restorability.** A backup is encrypted under the DEK version current at backup time. Destroy or lose that version and every backup wrapped by it is unrecoverable. Key retention must therefore be at least as long as backup retention — the most common way teams destroy their own recovery capability. **Key placement.** A key file on the same volume as the datafiles turns encryption into obfuscation, because a thief takes both. Likewise, an application encryption key checked into config beside the connection string protects nothing. **Compromise response.** If a DEK may have leaked, rotating it is necessary but not sufficient: the attacker still holds anything copied while it was valid. The real response is rotation plus assessing what was readable during the exposure window. **Verification.** Test the whole path, not the config: restore a backup in an isolated environment using only the key service, and confirm the audit log shows the unwrap. A key hierarchy never exercised in a restore is an assumption, not a control. ## Say this in an interview "Two-level keys let me rotate the exposed key cheaply and keep custody outside the database, while accepting that data-key rotation is a bulk re-encryption job. The two hard constraints are that the key service is now on the critical path for startup and restore, and that key retention must outlive backup retention."

  • An auditor asks you to rotate encryption keys annually. What do you actually rotate?
    The key-encrypting key: unwrap the data key under the old master and re-wrap it under the new version. No data is rewritten, so it completes in seconds and satisfies rotation for the widely-referenced key. Rotating the data key itself is a bulk re-encryption job reserved for suspected compromise, run online with version-tagged ciphertext.
  • What breaks if you delete an old key version after rotating?
    Every backup and archived log encrypted under that version becomes permanently unrecoverable. Key retention must be at least as long as backup retention, and destruction should be a deliberate, dated action. The one time you do want this is crypto-shredding, where destroying a per-tenant key is the mechanism for erasing that tenant from immutable backups.

A safe deposit box: the documents are locked in the box (data key), and the box key is kept in the bank vault (master key). Changing the vault's lock does not require reprinting the documents.

saying these in an interview costs you the question

  • Storing the key or keyfile on the same volume as the datafiles
  • Claiming key rotation always requires re-encrypting all data
  • Deleting old key versions while backups encrypted under them are still retained
  • Giving one role both database privileges and unwrap rights on the master key
  • Ignoring that a key-service outage can prevent the database from starting

context