Client-side field level encryption

Also known as: CSFLE

On this page 7

Client-side field level encryption (CSFLE) is a database security technique in which the application driver encrypts selected fields before data leaves the client, so the database server stores and returns only ciphertext for those fields. In database security, it addresses a threat that server-side encryption cannot: a compromised or curious database operator who can read memory, logs, backups or query results.

The term is most closely associated with MongoDB, which introduced automatic CSFLE in version 4.2, but the same pattern appears in other systems under different names, such as Always Encrypted in SQL Server. The defining property is that encryption keys never reach the database server in usable form.

How client-side field level encryption works

CSFLE relies on envelope encryption with two levels of keys. A customer master key (CMK) lives in an external key management service or hardware security module, and it wraps one or more data encryption keys (DEKs) that actually encrypt field values.

The wrapped DEKs are stored in a dedicated key vault collection in the database. When the driver needs a DEK, it fetches the wrapped key, asks the key management service to unwrap it with the CMK, and caches the plaintext DEK only in client memory for a limited time.

  1. The application issues an ordinary insert or query through an encryption-aware driver.
  2. The driver consults an encryption schema that names the fields to protect and the algorithm for each.
  3. It encrypts those field values with the appropriate DEK, typically using AES-256 in CBC mode with HMAC-SHA-512 authentication.
  4. The server stores the resulting binary ciphertext and never receives the key.
  5. On read, the driver decrypts matching fields transparently before returning results to the application.

Drivers can work in automatic mode, where an encryption schema drives encryption of every matching operation, or in explicit mode, where application code calls encrypt and decrypt functions directly for each value.

Deterministic and random encryption algorithms

Each protected field uses one of two algorithms, and the choice decides whether the field can be queried. Deterministic encryption always produces the same ciphertext for the same plaintext and key, while random encryption produces a different ciphertext every time.

The table below shows how the two algorithms differ in capability and exposure.

AlgorithmEquality queriesLeakageSuitable fields
DeterministicSupportedReveals which records share a valueIdentifiers with high cardinality
RandomNot supportedReveals only ciphertext lengthFree text, medical notes, low-cardinality values
Encryption algorithms available for protected fields

Deterministic encryption leaks frequency: an attacker who sees ciphertext can count how often each value occurs. For a field such as blood type or gender, that pattern alone may reveal the plaintext, so low-cardinality fields should use random encryption.

Range queries, sorting, prefix matching and aggregation on encrypted values are not available under classic CSFLE. Newer schemes such as Queryable Encryption add equality and range support with structured encryption, at the cost of extra metadata and storage.

Key management and key providers

The security of CSFLE depends almost entirely on where the customer master key lives and who can use it. A local key file is convenient for development but places the master secret beside the application, which defeats much of the purpose in production.

Key providerTypical useMain trade-off
Local key fileDevelopment and testingNo separation of duties
Cloud key management serviceProduction on a public cloudDependence on provider availability
KMIP-compliant HSMRegulated on-premises deploymentsHigher cost and operational effort
Customer master key providers and their trade-offs

Because each DEK can be assigned per field, per tenant or even per user, CSFLE supports crypto-shredding: deleting a single user’s DEK renders that user’s encrypted fields permanently unreadable, including copies in backups. Teams should also plan key rotation, which for the CMK means rewrapping DEKs, while rotating a DEK itself requires re-encrypting data.

How it compares with other database encryption methods

CSFLE is one layer among several, and it differs from its neighbors mainly in where encryption happens and who holds the keys. Server-side methods protect data at rest from disk theft but still expose plaintext to the database process and its administrators.

ApproachEncryption locationProtects againstServer sees plaintext
Transparent data encryptionStorage engine on serverStolen disks and filesYes
Column-level encryptionDatabase functions on serverUnauthorized column readersYes, during processing
Client-side field level encryptionApplication driverServer, operator and backup exposureNo
TLS in transitNetwork channelNetwork eavesdroppingYes
Encryption approaches in database security

In practice these layers are combined: TLS protects the wire, transparent data encryption protects the storage media, and CSFLE protects the most sensitive fields from everyone without access to the key management service.

Limitations and implementation pitfalls

CSFLE trades functionality for confidentiality. Encrypted fields cannot be indexed for range scans, cannot participate in most aggregation stages, and inflate document size because each value carries an algorithm marker, a key identifier, an initialization vector and an authentication tag.

  • Schema drift occurs when one application writes plaintext into a field another application expects to be encrypted; server-side schema validation can reject such writes.
  • Every client must be encryption-aware; ad hoc tools without key access see only binary values.
  • Compromised application hosts still expose plaintext, because decryption happens there.
  • Key service outages block reads and writes of protected fields once the DEK cache expires.

The most serious design error is encrypting too much or too little. Protecting every field breaks queries and reporting, while protecting only names and leaving related identifiers in plaintext allows reidentification through linked values.

The simple explanation

Client-side field level encryption works like sealing the private parts of a letter before handing it to the post office. The post office stores and delivers the letter, but it cannot read the sealed lines, because only the sender and the intended reader hold the key.

In a database, the application seals fields such as tax numbers or diagnoses before saving them. Even someone who runs the database server sees only scrambled data, while the authorized application reads everything normally.

How it is used in practice

A telehealth provider stores about 4 million patient records in a managed cloud database operated by a third party. Regulations require that the hosting provider and its staff cannot read diagnoses or insurance numbers, even during support incidents.

The engineering team configures CSFLE with a cloud key management service as the CMK provider. Insurance numbers use deterministic encryption so the call center can look up a patient by number, while diagnoses, clinician notes and birth dates use random encryption. Each patient receives a dedicated DEK.

When a patient requests erasure, the team deletes that DEK, which makes the patient’s protected fields unreadable in the live database and in every retained backup. An audit later confirms that database administrators and the hosting provider never had access to plaintext medical data.