Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spiceai/spiceai/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Spice.ai provides two types of caching:- Query Results Caching - Caches SQL query results
- Data Acceleration Caching - Caches accelerated dataset contents (new in v1.0+)
Query Results Caching
Query results caching stores the output of SQL queries to avoid re-executing expensive queries.Basic Configuration
Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
enabled | boolean | false | Enable results caching |
max_size | string | 128MB | Maximum cache size |
item_ttl | duration | 1s | Time-to-live for cached items |
eviction_policy | string | lru | Eviction policy (lru or lfu) |
engine | string | moka | Cache engine (currently moka only) |
hashing_algorithm | string | xxh3 | Hash algorithm for cache keys |
encoding | string | none | Compression encoding (none or zstd) |
Example: High-Frequency Dashboard
Stale-While-Revalidate (SWR)
Serve stale cached data while refreshing in the background:- Query executes and result cached with
cached_attimestamp - Within
item_ttl(1m): Return cached result (fresh) - After
item_ttl, withinstale_while_revalidate_ttl(5m):- Return stale cached result immediately
- Trigger background refresh
- Next query gets fresh result
- After
stale_while_revalidate_ttl(6m): Cache miss, execute query
Stale-If-Error
Serve stale data when upstream source fails:Cache Keys and Hashing
Cache keys are computed from:- SQL query text
- Query parameters
- Schema/table versions
| Algorithm | Speed | Collision Resistance | Use Case |
|---|---|---|---|
xxh3 | Fastest | Good | Default, best for most cases |
xxh64 | Fast | Good | Stable across platforms |
ahash | Fast | Good | Good general purpose |
blake3 | Medium | Excellent | High-security environments |
siphash | Slow | Excellent | DoS attack prevention |
Compression Encoding
Compress cached results to store more data:- None - No compression, fastest access, more memory
- Zstd - 2-10x compression, minimal CPU overhead
Cache Invalidation
Caches are automatically invalidated when:- Item TTL expires
- Table is modified (
INSERT,UPDATE,DELETE) - Dataset is refreshed
- Manual invalidation
Ignored Schemas
Exclude certain schemas from caching:Data Acceleration Caching Mode
New in Spice v1.0+, acceleration caching mode caches filtered dataset queries with automatic freshness management.When to Use Caching Mode
Use caching refresh mode when:- Queries filter on specific dimensions (e.g.,
user_id,region) - Source data changes infrequently
- Query patterns are repetitive
- Source queries are expensive
- Need full dataset in accelerator
- Queries are always unique
- Source data changes rapidly
Configuration
How Acceleration Caching Works
Cache Key Computation
Cache keys include:- Filter expressions (
WHEREclause) - Dataset name
- Schema version
Stale-While-Revalidate with Caching Mode
- 0:00 - Query executes, result cached
- 0:30 - Query returns cached result (fresh)
- 2:30 - Query returns stale result, triggers background refresh
- 2:31 - Background refresh completes, cache updated
- 2:32 - Query returns fresh result
- 12:30 - Query executes (past SWR window)
Batched Cache Writes
Caching mode batches writes to reduce overhead:Localpod Synchronization
Synchronize cache data across pods:Performance Tuning
Cache Size Calculation
Estimate required cache size:- Average result: 1MB
- Cache hit rate: 80%
- Query frequency: 100/sec
- TTL: 60 seconds
Eviction Policies
LRU (Least Recently Used):- Evicts least recently accessed items
- Good for temporal locality (same queries repeated)
- Default policy
- Evicts least frequently accessed items
- Good for workloads with hot queries
- Requires frequency tracking (more memory)
Cache Hit Rate Monitoring
Monitor cache effectiveness:TTL Tuning
Short TTL (1s - 1m):- Near real-time data
- High query frequency
- Small result sets
- Dashboard queries
- Periodic reports
- Moderate data freshness requirements
- Historical data
- Slow-changing dimensions
- Large result sets
Best Practices
1. Match TTL to Data Freshness Requirements
2. Use Compression for Large Results
3. Enable SWR for High-Traffic Endpoints
4. Monitor Cache Memory Usage
5. Use Caching Mode for Filtered Queries
Troubleshooting
Cache Not Working
Check configuration:- Cache disabled in configuration
- Queries against ignored schemas
- Non-deterministic queries (e.g.,
NOW(),RANDOM()) - TTL too short
High Cache Miss Rate
Causes:- TTL too short for query pattern
- Queries too diverse (low repetition)
- Cache size too small (evicting frequently)
- Frequent data modifications invalidating cache
- Increase
item_ttl - Increase
max_size - Use SWR to serve stale data
- Adjust
eviction_policy
Related Topics
- Query Optimization - Query performance tuning
- Snapshots - Fast cold starts with snapshots