Cache Component
Cache Component Concept
Reduces response time
Lightens backend load
Protection against backend errors
Reduces backend resource usage
Cache Component Types
Local Cache
Configuration Cache
- API Proxy configurations
- Policy definitions
- Routing information
- Metadata information
Token Cache
- OAuth2/JWT tokens
- Token validation results
- User session information
Response Cache
- API responses
- Cache key-based storage
- TTL (Time To Live) management
- Can be configured as local or distributed
When response cache is configured as local, Hazelcast is not used. When configured as distributed, Hazelcast is used and shared among all API Gateway instances.
Distributed Cache (Hazelcast)
Distributed cache infrastructure
- Multi-node support
- High availability
- Management and monitoring can be done in Apinizer
- Can run in independent namespaces from Gateway pods
Data such as throttling and quota are stored distributed
- Throttling data
- Quota data
- Shared state information
- Cross-namespace access support
Response cache can be both local and distributed
- Local response cache: Hazelcast is not used
- Distributed response cache: Hazelcast is used
Automatic and event-based invalidation
- Automatic invalidation
- Event-based invalidation
- Pattern-based invalidation
Hazelcast is managed and can be monitored by Apinizer. When distributed cache is used, data such as throttling and quota are shared among all API Gateway instances.
Namespace Independence:
Cache Server pods can now run in different Kubernetes namespaces than Gateway pods. Gateway pods can access cache servers in other namespaces using Kubernetes service discovery (e.g., http://cache-http-service.apinizer-cache.svc.cluster.local:8090). This provides more flexible infrastructure management and allows you to separate Gateway and Cache workloads. For Cache Server configuration, see the Distributed Cache page.
Cache Strategies
- Search in cache
- If not found, get from backend
- Save to cache
- Return to client
- Write to backend
- Write to cache
- Return to client
- Write to cache
- Return to client
- Write to backend asynchronously
Cache Configuration
Cache Key Strategy
URL path is used as cache key
Example: GET /api/v1/products → Cache Key: /api/v1/products
URL and query parameters are included in cache key
Example: GET /api/v1/products?category=electronics → Cache Key: /api/v1/products?category=electronics
URL and specific header values are included in cache key
Cache Key: URL + Header values
Creating custom cache key
User-defined cache key strategies can be used.
Cache TTL (Time To Live)
Fixed TTL
- Keeping in cache for a specific period
- Example: 5 minutes, 1 hour
Dynamic TTL
- TTL based on response headers
- Cache-Control header
- Expires header
Conditional TTL
- Conditional TTL
- Based on response status code
- Based on content type
Cache Invalidation
- Automatic deletion with timeout
- Manual cache clearing
- Invalidation via API
- Event-based invalidation
- Invalidation with webhook
- Pattern-based invalidation
- Wildcard invalidation
Cache Usage Scenarios
- First request: Get from backend, save to cache
- Subsequent requests: Return from cache
- After TTL: Clear cache, get from backend again
- Token validation: Check if token exists in cache
- If in cache: Return without validation
- If not in cache: Validate from Identity Manager, save to cache
- API Proxy configuration: Get from Management API
- Save to local cache
- On configuration change: Invalidate cache
- Throttling and quota data are stored in distributed cache
- Shared among all API Gateway instances (even if in different namespaces)
- Synchronized via Hazelcast
- Gateway pods access cache servers in different namespaces via Kubernetes service discovery
Cache Best Practices
- Select appropriate TTL values
- Too short TTL reduces performance
- Too long TTL risks stale data
- Meaningful cache keys
- Prevent collisions
- Pattern-based invalidation
- Cache size limits
- Eviction policies
- Memory monitoring
- Fallback on cache errors
- Routing to backend
- Error logging