Encryption at rest concept in MongoDB - Time & Space Complexity
Encryption at rest protects data stored on disk by converting it into a secure format.
We want to understand how the time to save or read data changes when encryption is used.
Analyze the time complexity of saving data with encryption at rest enabled.
// Pseudocode for inserting a document with encryption at rest
db.collection.insertOne({ sensitiveData: "secret" })
// MongoDB encrypts data before writing to disk
This code inserts data that MongoDB encrypts before storing it on disk.
Encryption processes each piece of data before saving.
- Primary operation: Encrypting each data block or document.
- How many times: Once per document or data chunk being saved.
As the amount of data grows, the encryption work grows too, roughly matching the data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 documents | Encrypt 10 times |
| 100 documents | Encrypt 100 times |
| 1000 documents | Encrypt 1000 times |
Pattern observation: The encryption work grows linearly with the number of documents.
Time Complexity: O(n)
This means the time to encrypt data grows directly with how much data you save.
[X] Wrong: "Encryption happens once and then all data is instantly secure without extra time."
[OK] Correct: Each piece of data must be encrypted separately, so more data means more encryption time.
Understanding how encryption affects data operations helps you explain performance impacts clearly and shows you grasp real-world database security.
"What if MongoDB used batch encryption for multiple documents at once? How would the time complexity change?"