skip to content

For MongoDB Queryable Encryption versus application-level encryption, how do you decide when a payments service must hide card tokens and birth dates from DBAs yet search by token and birth-date range?

level: principalimportance: should knowfreq 16%

answer

  1. name the adversary first
  2. who can answer the range query
  3. price it in tags and writes
  4. one key per field, immutable schema
  5. master key loss is permanent

basics

~20 s

Start from the adversary and the queries. Queryable Encryption answers the range on the server, at a cost in metadata, limits and diagnostics; application-level encryption pushes search into your code. Master key loss is permanent either way.

solid answer

~50 s

I would first write down the adversary. Queryable Encryption stops a DBA reading fields directly, and stolen files, but the manual says it does not defend against persistent access, or snapshots combined with query logs, especially for range queries. Next, the queries: token equality is easy either way, but a birth-date range is what application-level encryption cannot serve without a plaintext bucket or a scan in the application. If that range is truly required, Queryable Encryption is the natural fit, with a tightly bounded `range` field. The price is two metadata writes per tag, up to 2-3 times storage for an all-equality collection, manual compaction, no unique or TTL indexes or secondary reads, redacted diagnostics, and a schema you cannot change. Application-level wins with per-customer keys or no server-side search. Either way, protect the master key: losing it loses the data.

go deeper

for a junior

Know that encrypting in your own code hides values from the database but also stops it searching them, and that Queryable Encryption exists to keep some of that search.

for a middle

Be able to say which queries each option can serve, what metadata Queryable Encryption adds, and why losing the master key is unrecoverable in both.

for a senior

Estimate the write and storage cost from the tag formulas, list the features the team would lose, and prototype before committing to a schema that cannot change later.

for a principal

Own the threat statement and the exit cost. Tell stakeholders plainly what a persistent insider can still learn, challenge whether the range is needed, and set the rules on who may touch the master key.

## Frame the decision **Application-level encryption** means your code encrypts values with its own library and stores opaque bytes. The server can match them only if your scheme makes equal values look equal. **Queryable Encryption (QE)** is MongoDB's scheme, where the driver and server cooperate to answer queries over randomized ciphertext. There is no universally right answer, because the two options fail in different places. CSFLE sits between them, and it remains the choice for some requirements. ## Start with the adversary "DBAs must not read them" hides several threats, and these schemes cover them unevenly. | Threat | Queryable Encryption | |---|---| | Superuser reads the field directly | Protected | | Stolen disk, snapshot or backup files | Protected, reveals database size and operation metadata | | Network capture | Protected | | Persistent access, or snapshots plus query transcripts or logs | Outside the guarantees, **especially for range queries** | | Arbitrary write access to the collection | No guarantee | A DBA with long-term access to the server is closer to the persistent case than to a stolen backup. So a design that hides data from a curious DBA is different from one that survives a determined DBA who is also capturing query logs. Say which one you are promising the auditors. Application-level encryption with no searchable structure leaks less to that insider, since the server does nothing with the values, but its limits show up immediately in the queries. ## Then the queries For this service: - **Token lookup.** A card token is high-cardinality and matched whole, so `equality` in QE works, and so would a deterministic or keyed-hash scheme in the application. - **Birth-date range.** This is the deciding requirement. QE has a `range` query type on `date`, available since MongoDB 8.0. Application-level encryption needs a plaintext bucket such as a birth year, which gives up information, or a filter in the application over a wider result set. Challenge the requirement before paying for it. The manual advises against enabling range when equality is enough. If the range is really "is this customer over 18", storing a computed flag might remove the need altogether. ## Price Queryable Encryption honestly - **Write amplification.** Each indexed value writes two metadata documents per tag, one to the ESC and one to the ECOC. - **Storage.** With equality fields, a collection encrypting every field can take up to 2-3 times the space. Range, prefix, suffix and substring costs depend on their parameters, and can be far higher, so estimate them with the manual's formulas and prototype. - **Upkeep.** Metadata compaction is manual, and the manual says to run it once metadata collections exceed 1 GB. In the worst case it reveals the number of unique field and value pairs inserted since the last run. - **Rigidity.** QE works on new collections only, and query types are immutable. Migration means inserting the documents through a configured client. - **Lost features.** No secondary reads, no unique or TTL indexes, no multi-document updates, no `$group` on encrypted fields, and no combination with Atlas Search. - **Observability.** Operations leave the slow query log and profiler, which limits what MongoDB support can analyse too. - **Edition.** Automatic mode needs Enterprise or Atlas, with explicit mode available on Community. Application-level costs are of another kind. You own the cryptographic design, key rotation, every search workaround and all of its review. There is no `rewrapManyDataKey`, and mistakes in a home-grown scheme are hard to see. ## Key loss Whichever you pick, master key handling is the catastrophic path. Deleting a customer master key makes every field under its data keys **permanently unreadable**, and restoring the vault does not help because restored data keys are still wrapped by that key. My standing rules: 1. Only the application's identity can use the KMS key, and DBAs have no access to it. 2. Key deletion needs separate approval from the people who run the database. 3. Back up the key vault, and host it apart from the data with tighter controls. 4. Rehearse a restore, including the KMS dependency. Also decide on erasure. CSFLE and application encryption allow one key per customer, so destroying a key can erase that customer from data backups too, provided no backup still holds the key. QE takes a single key per field, and it rejects duplicate key IDs, so that route is closed. Per-tenant keys are one of the manual's stated reasons to prefer CSFLE. ## A recommendation for this case If the range is a confirmed need and the team can run Enterprise or Atlas, I would choose QE: `equality` on the token, `range` on the date with tight `min` and `max`, and no `queries` on fields that are only read back. I would prototype write throughput first, and put metrics in the application from day one. I would choose application-level encryption if the range disappears under scrutiny, if per-customer shredding is mandatory, or if the workload relies on secondary reads or other features QE removes. MongoDB 8.0 and 9.0 give the same answer here, since both support range; the string query types that 9.0 adds do not change this decision.

  • A compliance officer asks you to promise DBAs can never learn anything about these fields. What do you say?
    That is stronger than the scheme offers. Direct reads return ciphertext, but the manual excludes persistent access and snapshots combined with query transcripts, particularly for range queries, and it gives no guarantee against arbitrary writes. I would promise what holds, then cover the rest with log handling, access controls and auditing.
  • When would you choose CSFLE over both Queryable Encryption and your own encryption?
    When you need different keys for the same field, such as per-tenant or per-user keys, and equality on deterministic fields is enough. It also suits an application already using it, or a schema that keeps gaining encrypted fields, because adding them under Queryable Encryption requires rebuilding metadata collections and indexes. It offers no range queries.

  • Queryable Encryption makes DBAs irrelevant, because they can never learn anything about encrypted fields.
  • Queryable Encryption has no meaningful storage cost because the fields stay about the same size.
  • Encrypted query types can be adjusted later, so there is no need to settle them up front.
  • A deleted master key is recoverable as long as there is a backup of the key vault.
  • Queryable Encryption can give each tenant its own data key on a shared encrypted field.

Assumes MongoDB 9.0; range queries on Queryable Encryption exist since 8.0, so this decision reads the same on both.