Skip to main content
Version: Next

Flow Example

This is an example of a fully configured Caching Flow that caches API responses based on URL patterns and headers, ensuring faster response times and reduced load on API providers.

/etc/lunar-proxy/flows/caching-flow-example.yaml
name: CachingFlowExample

filter:
url: api.example.com/v1/resource/* # Caches responses for API endpoints matching this pattern.

processors:
ReadCache:
processor: ReadCache # Reads cached responses if available.
parameters:
- key: normalized_path_key
value: "api.example.com/v1/resource"
- key: header_key
value: "Authorization" # Example: Authorization header used for cache keys.
- key: path_parameter_key
value: "resourceID" # Example: Path parameter used for cache keys.

WriteCache:
processor: WriteCache # Writes responses to the cache.
parameters:
- key: ttl_seconds
value: 300 # Cache time-to-live set to 5 minutes.
- key: record_max_size_bytes
value: 4096 # Maximum size of cached responses is 4 KB.
- key: max_cache_size_megabytes
value: 100 # Cache size limited to 100 MB.
- key: normalized_path_key
value: "api.example.com/v1/resource"
- key: header_key
value: "Authorization"
- key: path_parameter_key
value: "resourceID"

flow:
request:
- from:
stream:
name: globalStream
at: start
to:
processor:
name: ReadCache

- from:
processor:
name: ReadCache
condition: cache_miss
to:
stream:
name: globalStream
at: end

response:
- from:
stream:
name: globalStream
at: start
to:
processor:
name: WriteCache

- from:
processor:
name: WriteCache
to:
stream:
name: globalStream
at: end

- from:
processor:
name: ReadCache
condition: cache_hit
to:
stream:
name: globalStream
at: end

Troubleshooting

  1. Cache Misses:

    • Verify that the normalized_path_key and header_key parameters match the actual request values.
    • Check that the path_parameter_key is properly configured to align with API request parameters.
  2. Excessive Cache Size:

    • Confirm that max_cache_size_megabytes and ttl_seconds are appropriately set.
    • Reduce record_max_size_bytes if individual cache entries are too large.
  3. Incorrect Cached Responses:

    • Check the WriteCache processor configuration to ensure responses are being cached correctly.
    • Validate that key selectors (headers, path parameters) used for cache keys are accurate and unique.