HeliosDB Caching Quick Start Guide
HeliosDB Caching Quick Start Guide
Version: 7.0
Prerequisites
- HeliosDB 5.0 or later installed
- Database created and accessible
- Appropriate permissions (
ALTER DATABASE,ALTER TABLE)
5-Minute Setup
Step 1: Enable Query Caching
-- Enable query result caching for the databaseALTER DATABASE ENABLE QUERY_CACHE;
-- Verify caching is enabledSELECT setting_value FROM database_settings WHERE setting_name = 'query_cache_enabled';Step 2: Configure Basic Parameters
-- Set cache size and TTLALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 1024, -- 1 GB cache eviction_policy = 'W_TINYLFU', -- Best general-purpose policy ttl_seconds = 3600 -- 1 hour default TTL);Step 3: Enable Table-Level Caching
-- Enable intelligent caching for specific tablesALTER TABLE products ENABLE INTELLIGENT_CACHING;ALTER TABLE customers ENABLE INTELLIGENT_CACHING;ALTER TABLE orders ENABLE INTELLIGENT_CACHING;Step 4: Verify Cache is Working
-- Run a querySELECT * FROM products WHERE category = 'electronics';
-- Run it again and check statsSELECT * FROM products WHERE category = 'electronics';
-- View cache statisticsSELECT cache_hits, cache_misses, hit_rateFROM cache_statistics;Expected output:
cache_hits | cache_misses | hit_rate-----------|--------------|---------- 1 | 1 | 0.50Enabling Caching
Database-Level Caching
-- Enable all caching featuresALTER DATABASE ENABLE QUERY_CACHE;ALTER DATABASE ENABLE STATEMENT_CACHE;ALTER DATABASE ENABLE BUFFER_POOL_CACHE;
-- Or enable everything at onceALTER DATABASE ENABLE ALL_CACHES;Table-Level Caching
-- Standard cachingALTER TABLE table_name ENABLE CACHING;
-- Intelligent ML-based cachingALTER TABLE table_name ENABLE INTELLIGENT_CACHING;
-- Distributed/global caching (for multi-node deployments)ALTER TABLE table_name ENABLE GLOBAL_CACHE;Query-Level Caching
-- Cache hintSELECT /*+ CACHE */ * FROM products WHERE id = $1;
-- Cache with custom TTLSELECT /*+ CACHE TTL=300 */ * FROM products WHERE category = $1;
-- Explicit cache commandCACHE SELECT * FROM frequently_accessed_data;Basic Configuration
Memory Allocation
-- Set total cache memoryALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 2048 -- 2 GB total cache);
-- Set per-query limit (prevents single query consuming all cache)ALTER DATABASE SET CACHE_PARAMS ( per_query_max_mb = 256 -- Max 256 MB per cached query);TTL Settings
-- Global default TTLALTER DATABASE SET CACHE_PARAMS ( ttl_seconds = 3600 -- 1 hour);
-- Table-specific TTLALTER TABLE products SET CACHE_PARAMS ( ttl_seconds = 300 -- 5 minutes (for frequently changing data));
ALTER TABLE static_config SET CACHE_PARAMS ( ttl_seconds = 86400 -- 24 hours (for rarely changing data));Eviction Policy
-- W-TinyLFU (recommended for most workloads)ALTER DATABASE SET CACHE_PARAMS ( eviction_policy = 'W_TINYLFU');
-- Other options:-- 'LRU' - Least Recently Used (simple, predictable)-- 'LFU' - Least Frequently Used (good for hot/cold data)-- 'ML' - Machine learning based (adaptive)Monitoring Cache Performance
Quick Health Check
-- Overall cache healthSELECT cache_hits, cache_misses, ROUND(hit_rate * 100, 2) as hit_rate_pct, eviction_count, ROUND(memory_used_mb, 2) as memory_mbFROM cache_statistics;Expected healthy output:
cache_hits | cache_misses | hit_rate_pct | eviction_count | memory_mb-----------|--------------|--------------|----------------|---------- 50000 | 2500 | 95.24 | 1500 | 512.75Per-Table Statistics
SELECT table_name, cache_entries, ROUND(avg_entry_size_kb, 2) as avg_kb, ROUND(hit_rate * 100, 2) as hit_rate_pct, invalidation_countFROM table_cache_statsORDER BY hit_rate DESCLIMIT 10;Real-Time Monitoring
-- Watch cache hit rate over timeSELECT time_bucket('1 minute', recorded_at) as minute, SUM(hits) as total_hits, SUM(misses) as total_misses, ROUND(SUM(hits)::numeric / NULLIF(SUM(hits) + SUM(misses), 0) * 100, 2) as hit_rate_pctFROM cache_metricsWHERE recorded_at > NOW() - INTERVAL '1 hour'GROUP BY minuteORDER BY minute DESC;Cache Invalidation
Automatic Invalidation
HeliosDB automatically invalidates cache entries when data changes:
-- This UPDATE automatically invalidates related cache entriesUPDATE products SET price = 99.99 WHERE id = 123;
-- Affected cached queries are immediately invalidated:-- - SELECT * FROM products WHERE id = 123-- - SELECT price FROM products WHERE id = 123-- - Any query that reads the modified rowManual Invalidation
-- Invalidate by tableINVALIDATE CACHE WHERE table_name = 'products';
-- Invalidate all product-related cachesINVALIDATE CACHE WHERE table_name LIKE 'product%';
-- Clear entire cacheFLUSH ALL CACHES;
-- Uncache specific queryUNCACHE SELECT * FROM products WHERE featured = true;Common Configurations
High-Traffic Web Application
-- Large cache, aggressive cachingALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 8192, -- 8 GB eviction_policy = 'W_TINYLFU', ttl_seconds = 300 -- 5 minutes);
-- Cache frequently accessed pagesALTER TABLE products ENABLE INTELLIGENT_CACHING;ALTER TABLE categories ENABLE INTELLIGENT_CACHING;ALTER TABLE users ENABLE INTELLIGENT_CACHING;
-- Enable predictive warmingALTER DATABASE ENABLE PREDICTIVE_CACHE_WARMING;Analytics/Reporting
-- Larger per-query limit for big result setsALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 16384, -- 16 GB per_query_max_mb = 1024, -- 1 GB per query eviction_policy = 'LFU', -- Frequency-based for repeated reports ttl_seconds = 3600 -- 1 hour);
-- Cache expensive analytics queriesALTER TABLE sales_facts ENABLE INTELLIGENT_CACHING;ALTER TABLE dim_products ENABLE INTELLIGENT_CACHING;Real-Time Application (Low Latency)
-- Smaller cache, lower TTL for freshnessALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 2048, eviction_policy = 'W_TINYLFU', ttl_seconds = 30 -- 30 seconds max staleness);
-- Enable strong consistency for critical tablesALTER TABLE account_balances SET CACHE_PARAMS ( consistency_level = 'STRONG');
ALTER TABLE inventory SET CACHE_PARAMS ( consistency_level = 'READ_YOUR_WRITES');Development/Testing
-- Small cache, easy debuggingALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 256, eviction_policy = 'LRU', ttl_seconds = 60);
-- Enable verbose loggingALTER DATABASE SET CACHE_PARAMS ( enable_debug_logging = true);Distributed Cache Setup
For multi-node deployments, enable distributed caching:
Basic Setup
-- Enable global cache for shared tablesALTER TABLE products ENABLE GLOBAL_CACHE;
-- Configure replicationALTER TABLE products SET CACHE_PARAMS ( replication_factor = 3, consistency_level = 'EVENTUAL');Troubleshooting Quick Fixes
Low Hit Rate (<70%)
-- Increase cache sizeALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 4096 -- Double current size);
-- Check if working set exceeds cacheSELECT SUM(entry_size_bytes) / 1024 / 1024 as working_set_mb, (SELECT setting_value FROM database_settings WHERE setting_name = 'cache_max_size_mb') as cache_size_mbFROM cache_entries;High Memory Usage
-- Reduce cache sizeALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 1024, per_query_max_mb = 128);
-- Identify large cached entriesSELECT query_hash, size_bytes / 1024 / 1024 as size_mbFROM cache_entriesORDER BY size_bytes DESCLIMIT 10;Stale Data Issues
-- Reduce TTLALTER TABLE products SET CACHE_PARAMS ( ttl_seconds = 60);
-- Enable stronger consistencyALTER TABLE products SET CACHE_PARAMS ( consistency_level = 'STRONG');
-- Force immediate invalidationINVALIDATE CACHE WHERE table_name = 'products';Cache Not Being Used
-- Verify caching is enabledSELECT setting_name, setting_valueFROM database_settingsWHERE setting_name LIKE '%cache%';
-- Check table caching statusSELECT table_name, caching_enabledFROM table_caching_status;
-- Ensure queries are cacheableSELECT * FROM cache_diagnostic_report;Next Steps
- Configuration Guide - Advanced configuration options
- Troubleshooting Guide - Detailed problem resolution
- Caching Overview - Architecture and concepts
Quick Reference
Enable Caching
ALTER DATABASE ENABLE QUERY_CACHE;ALTER TABLE mytable ENABLE INTELLIGENT_CACHING;Configure Cache
ALTER DATABASE SET CACHE_PARAMS ( max_size_mb = 2048, eviction_policy = 'W_TINYLFU', ttl_seconds = 3600);Monitor Cache
SELECT * FROM cache_statistics;SELECT * FROM table_cache_stats;Invalidate Cache
INVALIDATE CACHE WHERE table_name = 'mytable';FLUSH ALL CACHES;