Loading...
Loading...
Configure Spice.ai in-memory caching for SQL query results, search results, and embeddings. Use when setting up caching, tuning cache TTL/size/eviction, configuring stale-while-revalidate, custom cache keys, or cache-control headers.
npx skill4agent add spiceai/skills spice-caching/v1/sql/v1/searchruntime.cachingspicepod.yamlversion: v1
kind: Spicepod
name: app
runtime:
caching:
sql_results:
enabled: true
max_size: 1GiB # Default 128MiB
item_ttl: 1m # Default 1s
eviction_policy: lru # lru | tiny_lfu
hashing_algorithm: xxh3
cache_key_type: plan # plan | sql
encoding: none # none | zstd
stale_while_revalidate_ttl: 30s # Default 0s (disabled)
search_results:
enabled: true
max_size: 1GiB
item_ttl: 1m
eviction_policy: lru
embeddings:
enabled: true
max_size: 128MiB
item_ttl: 1m| Parameter | Default | Description |
|---|---|---|
| | Enable/disable the cache |
| | Maximum cache size |
| | |
| | Cache entry TTL (Time to Live) |
| | Hash for cache keys: |
| Parameter | Default | Description |
|---|---|---|
| | |
| | |
| | Serve stale entries while refreshing in background. |
cache_key_typeplansqlNOW()eviction_policylrutiny_lfuencodingnonezstdhashing_algorithmxxh3ahashxxh64xxh128blake3siphashstale_while_revalidate_ttlitem_ttlitem_ttlitem_ttl + stale_while_revalidate_ttlSTALEitem_ttl + stale_while_revalidate_ttlruntime:
caching:
sql_results:
enabled: true
item_ttl: 10s
stale_while_revalidate_ttl: 10s
# Fresh for 10s → Stale (served while refreshing) for 10s → EvictedConflict warning: When usingon a dataset, do not configure bothrefresh_mode: cachingandruntime.caching.sql_results.stale_while_revalidate_ttlfor the same dataset. Choose one approach.acceleration.params.caching_stale_while_revalidate_ttl
Cache-Control/v1/sql/v1/search| Directive | Description |
|---|---|
| Skip cache for this request; cache the result for future requests |
| Require cached entry to remain fresh for at least N seconds |
| Accept stale responses up to N seconds old |
| Return only cached responses; error on cache miss |
| Serve stale cache (up to N seconds) if fetching fresh data fails |
# Skip cache for this query
curl -H "cache-control: no-cache" -XPOST http://localhost:8090/v1/sql -d 'SELECT 1'
# Only accept fresh results (at least 30s remaining)
curl -H "cache-control: min-fresh=30" -XPOST http://localhost:8090/v1/sql -d 'SELECT 1'
# Accept stale up to 60s
curl -H "cache-control: max-stale=60" -XPOST http://localhost:8090/v1/sql -d 'SELECT 1'
# Only return if cached
curl -H "cache-control: only-if-cached" -XPOST http://localhost:8090/v1/sql -d 'SELECT 1'spice sql --cache-control no-cache
spice sql --cache-control min-fresh=30
spice sql --cache-control max-stale=60
spice sql --cache-control only-if-cached
spice search --cache-control no-cachecache-controllet mut request = FlightDescriptor::new_cmd(sql_command_bytes).into_request();
request.metadata_mut().insert("cache-control", "no-cache");Properties props = new Properties();
props.setProperty("cache-control", "no-cache");
Connection conn = DriverManager.getConnection("jdbc:arrow-flight-sql://localhost:50051", props);Spice-Cache-Key-_cache_key_type# First query — cache MISS
curl -XPOST http://localhost:8090/v1/sql \
-H "spice-cache-key: users_spiceai" \
-d "select * from users where org_id = 1;"
# Different query, same cache key — cache HIT
curl -XPOST http://localhost:8090/v1/sql \
-H "spice-cache-key: users_spiceai" \
-d "select * from users where split_part(email, '@', 2) = 'spice.ai';"Warning: Ensure queries sharing a cache key are truly semantically equivalent. The runtime will return the cached result regardless of the actual query.
| Cache Type | Response Header |
|---|---|
| |
| |
| Status | Meaning |
|---|---|
| Served from cache |
| Cache checked, result not found |
| Cache bypassed (e.g., |
| Stale entry served while revalidating |
| (absent) | Cache did not apply (disabled or system table query) |
results_*search_results_*embeddings_*| Metric | Type | Description |
|---|---|---|
| Gauge | Configured max cache size |
| Counter | Total cache lookups |
| Counter | Total cache hits |
| Gauge | Current items in cache |
| Gauge | Current cache size |
| Counter | Total evictions |
| Gauge | Hit ratio (hits / total) |
runtime:
caching:
sql_results:
item_ttl: 30s
max_size: 2GiB
eviction_policy: tiny_lfu
encoding: zstd
stale_while_revalidate_ttl: 30sruntime:
caching:
sql_results:
item_ttl: 5s
cache_key_type: sql
hashing_algorithm: xxh3runtime:
caching:
sql_results:
enabled: false
search_results:
enabled: false
embeddings:
enabled: false| Issue | Solution |
|---|---|
Always getting | Check |
| Cache filling up quickly | Increase |
| Stale data being served | Reduce |
Dynamic functions ( | Switch to |
| SWR conflict error | Don't set both |