All systems
Data

Field Encryption at Rest System Design

Making a column unreadable without the key, while the handler and the model carry on treating it as a string.

internal/cryptointernal/models

Questions this page answers

Encryption at rest comes up in any design touching personal or regulated data. The questions that separate a real answer from a buzzword are about key management and about what you give up by encrypting a column.

What is encryption at rest?

Storing data in a form that is unreadable without a key, so that having the data is not the same as being able to read it. Database-level encryption protects the files on disk, which guards against a stolen drive and nothing else: anybody with a connection still sees plain text. Encrypting particular fields in the application protects them from backups, replicas and anybody granted read access.

Worked through in Problem statement →
Is database-level encryption not enough?

It protects the files on disk, which guards against a stolen volume and nothing else. Anybody with a connection sees plain text, and so does every backup dump, every replica and every analyst given read access for an afternoon. Encrypting in the application closes that, because the database never holds the readable value at all.

Worked through in Problem statement →
Which algorithm, and why does the choice matter?

AES-256-GCM, which is authenticated encryption. The authentication is the part worth explaining: it means a modified ciphertext fails to open rather than decrypting to garbage. Encryption without authentication protects confidentiality and not integrity, and integrity is exactly what you need when the threat is somebody who can write to your database.

Worked through in System requirements →
What is a nonce and why does it have to be fresh every time?

A number used once, mixed in so the same plaintext encrypts differently each time. Reusing a nonce with the same key is the one catastrophic mistake available in this mode, because it leaks the relationship between the two messages. So it is random per write and never derived from the value or a counter.

Worked through in High level design →
What do you give up by encrypting a column?

The ability to query it. Because the same value encrypts differently every time, there is no equality lookup, no sorting, no prefix search and no unique constraint. This is the constraint that actually bites, and it bites after the data exists. It makes this suitable for data you store and display, and unsuitable for keys and lookup columns.

Worked through in System requirements →
But you need to look people up by their encrypted email. Now what?

A blind index: a second column holding a deterministic keyed hash of the normalised value. Exact-match lookup works against that column while the encrypted one stays non-deterministic. You give up range queries and you leak which rows share a value, which is usually an acceptable trade for being able to find a record at all.

Worked through in Bottlenecks and improvements →
Where does the key live?

Anywhere except the database it protects. A key sitting in the same backup as the ciphertext provides no protection while looking exactly like protection. The better answer is a managed key service, with the field key wrapped rather than stored, which also brings rotation and audit and stops the key being an environment variable somebody can print.

Worked through in Bottlenecks and improvements →
What happens if you lose the key?

The data is gone, permanently, and that is the feature working as designed. There is no recovery, which makes key custody a bigger operational problem than the encryption itself. It is worth saying plainly in an interview, because the follow-up is usually about backup and escrow of the key rather than of the data.

Worked through in Bottlenecks and improvements →
How do you add this to a table that already has a million rows?

A versioned prefix on every stored value, and a backfill. Because a prefixed value is recognisably encrypted and an unprefixed one is recognisably plain text, both can live in the column at once, which is what makes the backfill resumable and safe to rerun. It runs in batches so it does not hold a transaction open across the whole table.

Worked through in Capacity estimation →
Why the version prefix?

So the scheme can change later without archaeology. Seven bytes now means that when you move to a different algorithm or key, you can tell how each existing row was written instead of guessing. Rotation then becomes a second backfill: decrypt under the old key, encrypt under the new, with the prefix distinguishing them.

Worked through in Low level design →
Where in the code does the encryption happen?

In the column type, at the driver boundary, rather than at the call sites. That way every writer is covered: the handler, a background job, a seed, a bulk import. Encrypting in a service method only covers the callers who went through that method, and the one that did not is the one that wrote plain text.

Worked through in Low level design →

1.Problem statement

Some columns hold things you must store and display but should never be legible straight out of the database. Personal notes on a client, a third-party token, a contact detail, an identity number. Database-level encryption protects the files on disk, which guards against a stolen volume and nothing else: anybody with a connection sees plaintext, and so does every backup dump, every replica and every analyst who was given read access for an afternoon in 2024.

Encrypting those columns in the application closes that. The cost is usually that the model gets noisy: an encrypt call before every write, a decrypt after every read, a bug the first time somebody adds a field and forgets one, and a schema that can never adopt encryption later without a migration.

A custom column type removes all of that. The field is declared as an encrypted string, the driver encrypts on the way in and decrypts on the way out, and nothing else in the application knows. What remains is one real constraint that cannot be designed away, and the honest thing is to state it rather than hide it.

The system has to be able to:

  • Encrypt a named field transparently, with no calls in the handler or the service.
  • Use authenticated encryption, so a tampered value fails rather than decrypting to nonsense.
  • Use a fresh nonce per write, so identical values do not produce identical ciphertext.
  • Version the scheme, so the algorithm can change later without ambiguity about existing rows.
  • Pass values through unencrypted when no key is configured, so the feature can be adopted without a migration.
  • Refuse to start on a key of the wrong length, rather than quietly running without encryption.
  • Encrypt the rows that are already there, in batches, when the feature is turned on.

2.System requirements

Functional requirements

  • An EncryptedString type usable as a model field.
  • AES-256-GCM with a random 12-byte nonce per write.
  • A version prefix on every stored value.
  • A process-wide key loaded from configuration as base64 that decodes to 32 bytes.
  • An empty key disabling encryption, with values stored as plaintext.
  • A non-empty key of the wrong length failing at startup.
  • A backfill that encrypts existing plaintext in a column, in batches.
  • JSON serialisation of the plaintext, so an API response is unaffected.

Non-functional requirements

  • Authenticated, not just encrypted. GCM means a modified ciphertext fails to open. Encryption without authentication protects confidentiality and not integrity, and integrity is what you need when the threat is somebody with write access to the database.
  • Non-deterministic. a new nonce per write means the same value stored twice looks different. That is required for confidentiality and it is exactly why such a column cannot be queried by equality.
  • Invisible to the application. the handler, the service and the Zod schema see a string. If using it required thinking about it, some field somewhere would be missed.
  • Adoptable later. no key means passthrough, so turning the feature on is a configuration change and a backfill rather than a schema migration.
  • Loud on misconfiguration. a 16-byte key where 32 is needed stops the process. Running without the encryption that was asked for is the worse outcome.

3.Capacity estimation

Numbers for a mid-sized deployment. They are here to size the thing, not to predict your traffic: change an assumption and the sums below move with it.

Assumptions

ParameterValue
Encrypted fields per project2 to 10
Reads touching an encrypted field per second~200 at peak
Average plaintext length200 bytes
AES-GCM throughput with hardware support>1 GB/s per core
Overhead per value12-byte nonce + 16-byte tag + prefix, then base64

CPU

200 reads/s x 200 bytes = 40 KB/s decrypted
against >1 GB/s available per core
under 0.01% of one core

With AES-NI the cost is not measurable against a database round trip. The reason not to encrypt a column is never performance, it is that you need to query it.

Storage

200 bytes plaintext + 28 bytes nonce and tag = 228 bytes
base64 encoded: 304 bytes
plus the 7-byte prefix: ~311 bytes, about 1.55x

Backfill

1,000,000 rows, batches of 500
2,000 batches, each a read, an encrypt and an update
at ~50 ms per batch: about 100 seconds

Batched because a single update statement over a million rows holds a transaction open for the duration, and because a backfill that can be resumed is better than one that must complete.

4.High level design

A custom column type sitting in the driver interface, so the encryption happens between the model and the database and nowhere else.

Core components

  • EncryptedString. the field type. Implements the driver value and scan interfaces, which is where the encryption and decryption live.
  • Key holder. the process-wide key, loaded once at startup behind a lock. Nil means disabled, which is a supported state rather than an error.
  • Versioned envelope. the prefix that tags a value as encrypted and says which scheme wrote it. A value without the prefix is plaintext, which is how passthrough and partial backfill coexist.
  • AEAD. AES-256-GCM. One construction, chosen once, rather than a configurable cipher suite nobody is qualified to configure per project.
  • Backfill. walks a column in batches, encrypting anything without the prefix and leaving anything with it alone, so it is safe to run twice.
  • JSON hook. serialises the plaintext, so an encrypted field is an ordinary string in an API response and in the generated TypeScript type.

Request flow

A write and a read through an encrypted column

12345678HandlerModel FieldValue()AES-256-GCMField KeyColumn: enc:v1:...Scan()JSON: plaintextBackfill
  1. 1A handler binds a request and sets the field. It is a string type, so nothing here is different from any other field.
  2. 2The row is saved. GORM asks the field for its database value, which is the hook the whole design hangs on.
  3. 3A fresh 12-byte nonce is generated for this write. Reusing a nonce with the same key is the one catastrophic mistake available in GCM, so it is never derived from the value or a counter.
  4. 4The key is read from the process-wide holder. A nil key means encryption is disabled and the value is returned as it is, which is what lets a project adopt this later.
  5. 5The nonce, the ciphertext and the authentication tag are concatenated, base64 encoded and prefixed with the scheme version. The column now holds something opaque, and the prefix is what will let a future version rotate the algorithm without guessing how an old row was written.
  6. 6On a read, the column value comes back. Without the prefix it is plaintext from before the backfill and is used as it is; with the prefix it is opened, and a failure to open is an error rather than a fallback, because a value that will not authenticate has been tampered with or is encrypted under a different key.
  7. 7The field is plaintext from here on. The API response, the generated TypeScript type and the admin form all see an ordinary string.
  8. 8Separately, turning the feature on runs a backfill over the existing rows, in batches, skipping anything already prefixed so it can be rerun after an interruption.

Data flow

  • Encryption happens in the driver interface, which means it happens for every writer: the handler, a job, a seed, a bulk import. There is no path to the column that bypasses it.
  • The prefix makes the column self-describing. Plaintext and ciphertext can coexist in it during a backfill, which is what makes the backfill resumable.
  • JSON carries plaintext deliberately. Encrypting the API response would mean the client needs the key, which would mean the key is on the client.
  • An encrypted column cannot be sorted, searched or filtered by equality, because the ciphertext for the same value differs every time. That is the price, and it is the reason this is for data you keep and show rather than for keys and lookup columns.

5.Technology stack

ComponentWhat it is
CipherAES-256-GCM
Nonce12 bytes, fresh per write, from crypto/rand
Key32 bytes, base64 in configuration, process-wide
Envelopeenc:v1: prefix, then base64
Integrationdriver.Valuer and sql.Scanner on a string type
Absent keypassthrough, so adoption needs no migration

6.Data model

There is no new table. The design is entirely in the column type, which is the point.

any model field

Text rather than bytes, so the column is readable in any client and the prefix is visible when somebody is working out what they are looking at.

ColumnHolds
notescrypto.EncryptedString, stored as text
stored formenc:v1:<base64 of nonce + ciphertext + tag>
sizeroughly 1.55x the plaintext

Where it lives

  • The key never goes in the database. It comes from the environment or a secret manager, which is what separates it from the ciphertext it protects.
  • Backups contain ciphertext. A leaked dump without the key is not a breach of those columns, which is the whole objective.
  • A replica or an analytics connection sees ciphertext too, because the encryption is above the database rather than inside it.

7.Low level design

Core types

crypto.EncryptedStringinternal/crypto/field.go

A string type with Value and Scan. Everything else about it is ordinary, which is why a model using it reads like a model that does not.

ValueScanMarshalJSONUnmarshalJSON
crypto.InitFieldKey

Loads the base64 key once at startup. Empty disables encryption; non-empty and the wrong length is a hard error.

crypto.Encrypt / Decrypt

The envelope functions. Decrypt returns a value without the prefix unchanged, which is what makes plaintext and ciphertext coexist during a backfill.

crypto.EncryptExisting

The batched backfill. Skips prefixed values, so it is idempotent and resumable.

Design principles applied

  • Put it where nothing can bypass it. in the driver interface, every writer is covered. In a service method, only the writers that call that method are.
  • One construction, not a choice. AES-256-GCM, fixed. A configurable cipher suite is an invitation to configure it wrongly in a project that has no cryptographer.
  • Version from the first write. the prefix costs seven bytes and is the only thing that makes rotating the scheme later possible rather than archaeological.
  • Fail loudly on the key, quietly on its absence. no key is a decision. A malformed key is a mistake, and the difference should be the difference between starting and not.

Patterns

PatternWhere it is used
Transparent column typeencryption in Value and Scan rather than at the call sites
Versioned envelopea scheme tag on every stored value
AEADconfidentiality and integrity from one construction
Idempotent backfillskip what is already done, so it can be rerun

8.Scalability and performance

  • AES-GCM with hardware support is far faster than the database round trip that delivered the row, so the CPU cost does not appear in a profile.
  • Storage grows by about 1.55x on the encrypted columns only, which is immaterial unless the column is large.
  • The real scaling constraint is not throughput, it is that the column is unqueryable. A field that needs sorting, searching or an equality lookup cannot be encrypted this way, and that has to be decided before the data exists.
  • The backfill is batched, so it runs against a live table without holding a long transaction, and can be interrupted and resumed.
  • A key rotation is a second backfill: decrypt under the old key, encrypt under the new, with the version prefix telling the two apart.
  • No new table and no new service means nothing extra to scale, replicate or operate.

9.Bottlenecks and improvements

What breaks first

  • The column cannot be queried. non-deterministic ciphertext rules out equality, ordering, prefix search and unique constraints. This is the constraint that actually bites, and it bites after the data exists.
  • Key loss is data loss. there is no recovery. A lost key makes every encrypted column permanently unreadable, and that is the feature working as designed.
  • The key in the wrong place. a key committed to the repository or sitting in the same backup as the database provides no protection at all while looking exactly like protection.
  • Mixed state after a partial backfill. plaintext and ciphertext in one column is correct during the backfill and misleading if it is never finished.

What to do about it

  • Keep a blind index for lookups. a deterministic HMAC of the normalised value in a second column gives exact-match lookup while the encrypted column stays non-deterministic.
  • Use a managed key service. KMS or Vault, with the field key wrapped rather than stored. Rotation and audit come with it, and the key stops being an environment variable somebody can print.
  • Separate the key from every backup. different system, different credentials, different retention. If the dump and the key travel together, nothing was gained.
  • Finish the backfill and then verify. a count of unprefixed values in the column is one query and is the only way to know the state is not mixed.

Read next