Overview
Backend caching stores responses from your upstream services, reducing load on backends and improving response times. KrakenD respects standard HTTP cache headers and provides flexible caching strategies.Example: Cached Market Data
The/market/cached endpoint demonstrates caching for cryptocurrency market data from the CoinGecko API.
Try It
Configuration
Fromconfig/krakend/krakend.json:
How It Works
HTTP Cache Component
qos/http-cache component enables HTTP-based caching that respects standard cache control headers from the backend.
Shared Cache
shared is true, the cache is shared across all clients. This is efficient for public data like market prices.
Set to false for user-specific data where each user should have their own cache.
Cache Behavior
KrakenD’s HTTP cache respects these standard headers from the backend:Cache-Control: max-age=3600- Cache for 1 hourCache-Control: no-cache- Always revalidateCache-Control: no-store- Do not cacheExpires- Explicit expiration timeETag- Conditional requestsLast-Modified- Conditional requests
Cache TTL Priority
KrakenD determines cache duration in this order:- Backend Cache-Control headers (if HTTP cache is enabled)
- Endpoint-level
cache_ttl(if set) - Global
cache_ttl(from main configuration)
Global Cache TTL
The playground sets a global default:Endpoint-Specific Cache
Override cache duration per endpoint:Cache Strategies
Shared Public Cache
Best for public data that’s the same for all users:- Public API data (weather, stocks, crypto prices)
- Product catalogs
- News feeds
- Static reference data
Private Cache
Best for user-specific data:- User profiles
- Personalized recommendations
- Shopping carts
- Private messages
Disable Caching
To disable caching for an endpoint:Cache Headers in Response
KrakenD adds headers to indicate cache status:X-Cache: HIT- Response served from cacheX-Cache: MISS- Response fetched from backendAge: 45- Response has been cached for 45 seconds
Performance Benefits
Without Caching
Every request hits the backend:Total latency: 600ms
With Caching
Total latency: 204ms
Performance improvement: 66% faster
Best Practices
1. Cache Stable Data
✅ Good candidates:- Product catalogs
- Configuration data
- Historical data
- Aggregated statistics
- Real-time stock prices
- Live sports scores
- User authentication status
- Shopping cart contents
2. Set Appropriate TTL
3. Respect Backend Headers
Always enable HTTP cache for backends that provide proper cache headers:4. Monitor Cache Hit Rate
Use Grafana metrics to track:- Cache hit rate
- Cache miss rate
- Backend request reduction
Advanced Caching
Vary by Query Parameters
KrakenD automatically varies cache by endpoint path and query parameters:Vary by Headers
For user-specific caching:Authorization).