Sub topic 10
Caching values in your application
This chapter assumes you can already read a parameter or secret with boto3 and know that rotation changes a secret's value over time.
Why cache
Each call to GetSecretValue or GetParameter is an API request. It adds latency, costs money for Secrets Manager, and counts against service quotas. It also depends on the network. A web service that fetches the database password on every request will pay all of these costs thousands of times per minute and may be throttled.
Configuration and secrets change rarely, so you can keep a copy in memory and refresh it now and then.
The basic pattern
Fetch the value on first use. Store it with a timestamp. Serve it from memory until a time-to-live (TTL) expires, then fetch it again.
A small TTL cache in Python
import time
import boto3
from botocore.exceptions import BotoCoreError, ClientError
secrets = boto3.client("secretsmanager")
TTL_SECONDS = 300
_cache = {}
def get_secret(secret_id):
now = time.monotonic()
entry = _cache.get(secret_id)
if entry and now - entry[1] < TTL_SECONDS:
return entry[0]
try:
value = secrets.get_secret_value(SecretId=secret_id)["SecretString"]
except (BotoCoreError, ClientError):
if entry:
return entry[0] # serve the stale copy rather than fail
raise
_cache[secret_id] = (value, now)
return value
def invalidate(secret_id):
_cache.pop(secret_id, None)The cache lives in a module-level dictionary, so it survives between calls in the same process. In AWS Lambda, anything created outside the handler is reused while the execution environment stays warm.
Choosing a TTL
The TTL is a trade-off between freshness and call volume.
| TTL | Effect |
|---|---|
| Very short (seconds) | Few stale values, but many API calls |
| Around 5 minutes | Common default, a good balance for most secrets |
| Hours | Cheap, but changes and rotations reach the application slowly |
Pick a TTL shorter than the time you can tolerate using an old value. During rotation, Secrets Manager keeps the previous version available as AWSPREVIOUS, so a short overlap is usually safe.
Handling rotation
A cached password can become wrong before the TTL ends. If the database rejects your credentials, call invalidate, fetch the secret again, and retry once. This recovers from a rotation without waiting for the TTL.
Ready-made options
You do not have to write the cache yourself:
- The AWS Secrets Manager client-side caching libraries are available for Python, Java, .NET, Go and JavaScript. They cache secrets and refresh them on an interval.
- The AWS Parameters and Secrets Lambda Extension runs next to your function and serves both parameters and secrets over a local HTTP endpoint. It caches them with a configurable TTL.
Exercise
A database password rotates every 24 hours. Your service caches it with a 12-hour TTL, and after each rotation some requests fail with authentication errors. What two changes fix this?
Show answer
Shorten the TTL, for example to 5 minutes, so the cache refreshes soon after rotation. Also invalidate the cache and retry once when the database rejects the credentials, so the service recovers immediately instead of waiting for the TTL to expire.