Caching Strategies in Functions
What you'll learn: How to use in-memory caching, external caches, and CDN patterns to dramatically reduce function invocations and execution time in serverless architectures.
Why Caching Matters in Serverless
In serverless functions, you pay per invocation and execution time. Without caching, your function might repeatedly fetch the same data, call the same APIs, or perform identical computations—wasting both time and money. Caching stores frequently-used results so subsequent requests can skip expensive operations.
Three Caching Layers
In-Memory Caching stores data in global variables outside your function handler. Since container instances are reused between invocations (after the cold start), this cache persists across multiple requests. Think of it like keeping a notepad on your desk—fast access, but limited to one worker.
# Global scope - survives between invocations
cache = {}
def handler(event, context):
key = event['userId']
if key in cache:
return cache[key] # Cache hit
result = expensive_database_call(key)
cache[key] = result
return result
External Caches like Redis or DynamoDB provide shared, distributed caching across all function instances. Multiple workers can access the same cache, eliminating duplicate work across your entire serverless fleet. This builds on the connection pooling patterns you've learned—reuse connections to your cache layer.
CDN Caching places static or semi-static responses at edge locations worldwide. When functions generate API responses with proper cache headers, CDNs serve repeated requests without invoking your function at all—the ultimate optimization.
Choosing Your Strategy
Use in-memory for small, immutable data (configuration, reference tables). Use external caches for shared state and frequently-accessed dynamic data. Use CDN caching for public APIs with predictable response patterns.
Key Takeaway: Caching in serverless functions operates at three levels—in-memory for speed, external for sharing, and CDN for elimination—each reducing invocations and execution time while building on connection reuse patterns.