Data Key Caching Pattern
What you'll learn: How to cache data encryption keys locally to dramatically reduce KMS API calls and boost throughput in high-volume encryption scenarios.
The Performance Challenge
When you use envelope encryption, every encrypt or decrypt operation typically requires fetching a data encryption key (DEK) from your KMS. At scale—think encrypting thousands of records per second—this creates a bottleneck. Each KMS call adds latency (typically 10-50ms) and costs money. Your application becomes KMS-bound rather than CPU-bound.
The Caching Solution
The Data Key Caching Pattern stores DEKs in memory for a limited time or number of operations. Instead of calling KMS for every single encryption operation, you:
- Request a DEK from KMS once
- Use it to encrypt multiple data items
- Cache it locally with usage limits (time-to-live or max operations)
- Discard and refresh when limits expire
Think of it like a coffee shop: Rather than running to the bank (KMS) for change every time a customer pays, you keep a cash drawer (cache) stocked with bills. You only visit the bank periodically to restock.
Trade-offs to Consider
Caching improves performance but introduces security considerations:
- Blast radius: A compromised cache exposes multiple data items encrypted with the same key
- Rotation lag: Cached keys continue encrypting new data even after rotation begins
- Memory security: Keys in RAM are vulnerable to memory dumps
Set aggressive limits—cache keys for seconds or hundreds of operations, not hours. Libraries like AWS Encryption SDK include built-in caching with configurable thresholds.
Key Takeaway: Data key caching reduces KMS API calls by reusing DEKs across multiple encryption operations within strict time and usage limits, dramatically improving throughput while requiring careful security boundary management.