SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
  • SSL/TLS
  • Encryption at Rest
  • Secrets Management
  • Password Management
  • RBAC
  • SAML
  • OWASP Top 10 and Common Attacks
  • DDoS Attacks and Defenses
  • Web Application Firewall (WAF)
  • Certificates and PKI
  • Multi-Factor Authentication (MFA)
  • Zero Trust
  • Key Management (KMS)
  • Data Privacy and GDPR Basics
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Operations and Security/Advanced Security

Encryption at Rest

Scramble stored data, so a stolen disk or backup is useless without the key.

โฑ 13 min readIntermediateInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Encryption at rest means the data is scrambled while it is stored on disks, in databases and in backups. Without the key, what is stored looks like random noise.

  • It protects against stolen disks, leaked backups and careless access to storage. It does not protect data from an app that is allowed to read it.
  • Real systems use envelope encryption: data is encrypted with a data key, and that key is encrypted ("wrapped") by a master key kept in a KMS or hardware module.
  • The big question is never only "is it encrypted?" but "who can get the key?"

02Concept: What is it?

Data lives in three states. In transit it moves over the network (protected by TLS). In use it is in memory while a program works on it. At rest it sits on a disk, in a database file, in object storage or in a backup. Encryption at rest covers that last state.

The idea is simple: use an encryption key to turn readable data (plaintext) into unreadable data (ciphertext). Only someone with the key can turn it back. If a disk is stolen, a backup bucket is made public by mistake, or a decommissioned drive is sold, the attacker gets only ciphertext.

Doing this well is mostly a key management problem. A strong lock is no use if the key is taped to the door. So large systems use envelope encryption: a unique data encryption key (DEK) encrypts the data, and a key encryption key (KEK), the master key, encrypts the DEK. The master key stays inside a Key Management Service (KMS) or a hardware security module (HSM) and is never handed out. The app asks the KMS to unwrap a DEK when it needs it.

Envelope encryption
๐Ÿ“„Dataplaintext
โ†’โ†“encrypt with DEK
๐Ÿ”’Ciphertextstored on disk
โ†’โ†“DEK wrapped by
๐Ÿ”KMS / HSMholds the master key
Only the wrapped data key is stored with the data. The master key never leaves the KMS.
๐Ÿง Encrypted is not the same as safe
Encryption at rest protects the storage. A running app, or an attacker who has taken over that app, can still read the data, because the app is allowed to use the key. You still need access control, network rules and monitoring.

03Real-Life Example

๐ŸตThe Safe-Deposit Box and the Bank Vault
You keep your valuables in a locked box (the encrypted data). The box key is itself kept in a small locked pouch (the wrapped data key). The pouch can only be opened with a master key, and that master key stays in the bank vault (the KMS). The bank writes down every time someone asks to open the pouch. If a thief steals the box and the pouch, they still cannot get in. And if you want to change the lock on the pouch, you do not need to move all your valuables. You just put the small box key into a new pouch.
Safe-deposit box
Encryption at rest
The valuables
The plaintext data
The locked box
The ciphertext on disk
The box key
The data encryption key (DEK)
The pouch holding the box key
The wrapped (encrypted) DEK
The master key in the bank vault
The key encryption key in a KMS or HSM
The bank's log of who asked
The KMS audit log
Changing the pouch lock
Rotating the master key (re-wrap DEKs)
Changing the box and moving everything
Rotating a DEK (re-encrypt the data)
Reading one record
๐Ÿ–ฅ๏ธAppasks for a record
โ†’โ†“unwrap my DEK
๐Ÿ”KMSchecks who asks, logs it
โ†’โ†“DEK
๐Ÿ”“Decryptin memory

04Why Do We Need It?

๐Ÿ’ฝStolen or lost media
A stolen laptop, server drive or backup tape holds only noise without the key.
๐ŸชฃMistakes in storage
A bucket or snapshot shared by accident exposes ciphertext, not readable data.
๐Ÿ“œRules and audits
Laws and standards for health, payment and personal data usually expect encryption of stored data.
โ™ป๏ธSafe disposal
When a drive is retired, destroying the key makes the data unreadable, even if the drive is not wiped.
๐Ÿง‘โ€๐Ÿ’ปLess trust in staff
Cloud or storage admins can handle disks without being able to read what is on them.
๐Ÿ”A clear audit trail
A KMS records each key use, so strange access stands out.
If someone walks away with the disk, they should walk away with noise. Keep the key somewhere else.

05Where Do We Use It?

๐Ÿ—„๏ธDatabases
Managed databases (for example on AWS, Azure and Google Cloud) offer storage encryption, and some databases offer transparent data encryption (TDE).
๐ŸชฃObject storage
Buckets can encrypt every object, with keys you or the provider manage.
๐Ÿ’พDisks and volumes
Cloud block volumes and laptop full-disk encryption protect the whole device.
๐Ÿ“ฆBackups and snapshots
Copies must be encrypted too, often with different keys from production.
๐Ÿ“ฑPhones and laptops
Device encryption protects personal data if the device is lost.
๐ŸงพSensitive fields
Cards, health notes and national IDs can be encrypted inside the app, field by field.

06Types / Parts / How It Works

Levels of encryption (from outer to inner)

Who can see the plaintext at each level?
Disk or volume
๐Ÿ’ฝFull-diskthe OS and DB see plaintext
Database
๐Ÿ—„๏ธDatabase / TDEfiles encrypted, queries see plaintext
Application
๐ŸงพField-levelthe DB only stores ciphertext
The more inside you encrypt, the fewer parts of your system ever see readable data.
LevelWhat it protects againstWhat it does not protect against
Full-disk / volumeA stolen or lost disk, a discarded serverAnyone who can log in to the running machine, and a database admin
Database (TDE) or storage serviceStolen database files and backupsAnyone who can run queries as an allowed user
Application / field-levelDatabase admins, leaked backups, SQL injection that reads raw rowsA hacked app that holds the key (and you lose easy search and sorting on those fields)
Client-side (end-to-end)Even the service provider cannot read the dataA lost user key means lost data, and search and server features are limited

Who holds the key?

OptionWho manages the keyGood and bad
Provider-managed keysThe cloud provider, automaticallyEasiest and on by default. You have less control and a weaker story for strict rules.
Customer-managed keys (KMS)You, in the provider's KMSYou set who can use the key, can rotate or disable it, and get an audit log. A bit more work.
Bring or hold your own keyYou, in your own HSM or outside the cloudMaximum control. Highest cost and the most ways to lose the key by mistake.
VisualizerStolen disk, or stolen key? Compare where the key livesStore the key next to the data and steal the disk. Then switch to envelope encryption. Try giving the attacker KMS access, then compare how long key rotation takes.
Where is the key kept?
What did the attacker get?

Toy cipher for teaching only. It is not real cryptography.

On the disk: encrypted data
c8404915dc108092aff772a09a010abac26b6c6667a015ce363723bd73a3b2
On the disk: the key field
97035319fe8e083e72
The data key, wrapped (encrypted) by the master key
What the attacker reads
ยท5;ยทยทnnpยทยท*[[ยทยทGยทAยทf'A:ยทยทยทHยทยท=ยท
The thief has the data and a wrapped data key. Without the master key (which never leaves the KMS) both are just noise. This is what encryption at rest is for.
Attacker reads data
No
in this situation
Rotate the master key
5 seconds
re-wrap the data keys only
Rotate one data key
6 minutes
re-encrypt one share of the data

Speeds are made-up illustrative numbers (2 TB of re-encryption per hour, 100 key re-wraps per second). The point is the shape: re-wrapping small keys is cheap, re-encrypting data is not.

07How to Use It / Steps / Implementation

Setting it up well

  1. 1
    Turn it on everywhere data is stored
    Databases, volumes, object storage, queues, search indexes, caches that persist, logs and backups. One unencrypted copy is the weak spot.
  2. 2
    Use envelope encryption with a KMS
    Let a unique data key encrypt the data and let the KMS or HSM hold the master key. Do not invent your own key storage.
  3. 3
    Keep keys away from data
    Never keep keys in the same bucket, the same repository or on the same disk as what they protect.
  4. 4
    Give the key the least access
    Allow only the services that really need to decrypt. Use separate keys for separate data sets or customers, so one leak has a small reach.
  5. 5
    Rotate keys on a schedule
    Rotating the master key means re-wrapping the small data keys, which is cheap. Rotating a data key means re-encrypting its data, so plan it.
  6. 6
    Encrypt backups with their own keys
    A backup that can be restored by anyone with prod access is a hidden copy of everything.
  7. 7
    Log and alert on key use
    Watch the KMS audit log for unusual volume, unusual callers and key disable or delete requests.
  8. 8
    Test the recovery
    Practice restoring from an encrypted backup. A key that was lost or deleted turns the data into permanent noise.
๐Ÿ’กUse well-known building blocks
Do not design your own cipher or key scheme. Use the storage encryption and the KMS of your platform, or a well-reviewed library, with the settings it recommends.
โš ๏ธDeleting a key deletes the data
This is useful for safe disposal and for deleting a customer's data. It also means an accidental key deletion is a disaster. KMS products usually add a waiting period before a key is really removed. Keep it on.

08Key Terms to Know

TermMeaning in easy words
Plaintext / ciphertextReadable data, and the scrambled version of it
Encryption at restEncrypting data while it is stored on disk, in a database or in a backup
Encryption in transitProtecting data while it moves over a network, usually with TLS
DEK (data encryption key)The key that directly encrypts the data
KEK / master keyA key that encrypts other keys, such as DEKs
Envelope encryptionEncrypting data with a DEK and encrypting the DEK with a master key
KMSA Key Management Service that creates, stores, rotates and audits keys
HSMA hardware security module: a tamper-resistant device that holds keys and does crypto inside it
Key rotationReplacing a key with a new one on a schedule or after a leak
TDETransparent data encryption: the database encrypts its own files for you
Field-level encryptionThe application encrypts chosen fields before they reach the database

09Pros, Cons and Trade-offs

โœ… Pros
  • A stolen disk, snapshot or backup is useless without the key
  • Often one switch on a managed service
  • Helps meet rules and audits
  • Key deletion can securely retire data
  • A KMS adds access control and a log of key use
โš ๏ธ Cons
  • Does not stop a hacked app or a user with valid access
  • Adds key management work and a dependency on the KMS
  • Losing a key loses the data
  • Small speed cost, and field-level encryption makes search and sorting hard
  • A false sense of safety if the keys are poorly guarded

Trade-offs

ChoiceWe gainWe lose
Provider-managed keysNo effort, on by defaultLess control and weaker proof for strict rules
Customer-managed keysControl, rotation and auditMore set-up, and a key mistake can lock you out
Field-level encryptionThe database and its admins never see plaintextNo easy search, sort or join on those fields, and the app must manage keys
Full-disk onlySimple and covers everything on the deviceProtects only against theft of the device
One key for everythingEasy to manageOne leak exposes everything
Many keys (per customer or table)Small blast radius, easy per-customer deletionMore keys to track and rotate

10Common Problems and Mistakes

Common mistakes

  1. Key next to the data. A key in the same bucket, the same repository or a config file on the same disk gives no real protection.
  2. Forgetting the copies. Backups, replicas, logs, caches and analytics exports are left unencrypted.
  3. Thinking it stops all attacks. A stolen app credential reads the data as normal. Encryption at rest is one layer.
  4. Broad key access. If every service and every engineer can decrypt, the key is only a formality.
  5. No rotation plan. Nobody knows how long a data-key rotation takes until the day it is urgent.
  6. Losing or deleting a key. The data is gone for good. Keep the KMS deletion delay and test recovery.
  7. Making up a custom scheme. Home-made crypto fails in ways you cannot see. Use proven services and libraries.

Things to watch

๐Ÿ”‘Key sprawl
Many keys with no owner or purpose. Name, tag and review them.
โฑ๏ธKMS latency and limits
Calling the KMS for every record is slow. Cache unwrapped DEKs briefly in memory.
๐ŸŒRegion outages
If the KMS is unreachable, nothing can decrypt. Plan multi-region keys for critical data.
๐ŸงชDev and test copies
Production data copied to a laptop or test system loses its protection.

11Real-World Examples

โ˜๏ธCloud storage and databases
Major cloud providers offer encryption of stored data, with a KMS for customer-managed keys.
๐Ÿ“ฑPhones
Modern phones encrypt their storage, so a lost phone does not expose the owner's data.
๐ŸฅHealth and payment data
Systems that hold health or card data are commonly expected to encrypt it at rest, in line with their rules and standards.
๐Ÿ’ฌSecure messaging
Some apps use end-to-end encryption, so even the provider holds only ciphertext.
๐ŸงพMulti-customer products
Software sold to many companies can use a separate key per customer, so removing one customer deletes their data cleanly.
๐Ÿ—ƒ๏ธBackups
Backup tools commonly encrypt archives before they leave the production network.
๐Ÿ“Note
Product names, defaults and settings change over time. Check your platform's current documentation for what is on by default and which key options exist.
Mini case: a leaked backup
Backup not encrypted
๐Ÿ“ฆBackup fileposted public by mistake
โ†’โ†“download
๐Ÿ˜ˆAnyonereads every customer
Backup encrypted, key in KMS
๐Ÿ“ฆBackup filesame mistake
โ†’โ†“download
๐Ÿ˜ˆAnyonegets only noise
Exposed Protected
The same mistake, with and without encryption at rest.

12Related Topics to Read Next

SSL/TLSโ†’

Encryption in transit, the other half of protecting data.

Secrets Managementโ†’

Keys and credentials need safe storage and handover too.

Password Managementโ†’

Passwords use one-way hashing, not reversible encryption.

Object Storageโ†’

Buckets and objects are a common place to turn on encryption.

Block Storageโ†’

Volumes are often encrypted as a whole.

RBACโ†’

Controls who is allowed to ask for the key and the data.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: scramble stored data, so a stolen disk or backup is noise without the key.
  • โœ”Real question: who can get the key? Keep it away from the data.
  • โœ”Envelope: a data key encrypts data. A master key in a KMS or HSM wraps the data key.
  • โœ”Levels: full-disk, database (TDE), application field-level, client-side. More inside means fewer parts see plaintext.
  • โœ”Rotation: the master key is cheap (re-wrap). A data key means re-encrypting data.
  • โœ”Limit: does not stop a hacked app or an allowed user. Pair with access control and monitoring.
  • โœ”Do: encrypt backups and replicas, use separate keys, log key use, test recovery.
  • โœ”One line: โ€œLock the data, and keep the key somewhere else.โ€
โ† Previous
SSL/TLS
Next โ†’
Secrets Management