Redis data sources
Redis integration enables FeatureQL to connect to Redis instances for real-time feature value retrieval.
Creating a Redis data source
First, establish a connection to your Redis instance by creating a source feature:
Connection configuration:
- URL: Standard Redis connection string (
redis://host:port
) - Timeout: Connection and operation timeout (e.g.,
'500ms'
) - Key patterns: Array of key patterns for optimization (e.g.,
ARRAY['customer_hash:*']
)
The SOURCE_REDIS()
function creates a persistent connection configuration that can be reused across multiple queries.
Data access patterns
Redis supports multiple data structures, and FeatureQL provides corresponding access patterns for each:
JSON object retrieval
For Redis keys containing JSON objects, retrieve and parse the entire value:
How this works:
- Key construction:
'customer:' || CUSTOMER_ID::VARCHAR
builds dynamic keys - JSON retrieval:
EXTERNAL_REDIS(KEY ...)
fetches the raw JSON string - Parsing:
JSON_PARSE_AS()
converts JSON to structured data with type safety - Field access:
CUSTOMER_DETAILS[last_order_id]
extracts specific fields
Use cases: Complex customer profiles, product catalogs, configuration objects
Hash field access
For Redis hash data structures, directly access specific fields:
Key advantages:
- Direct field access: No JSON parsing overhead
- Selective retrieval: Only fetch the fields you need
- Network efficiency: Reduces data transfer for large hashes
Use cases: User preferences, feature flags, counters, metadata lookups
Table-style joins on keys
Join based on the Redis key itself rather than field contents:
Use cases:
- Key-based relationships: When the Redis key represents an entity ID
- Hierarchical data: Parent-child relationships encoded in key structure
- Partitioned data: Different key patterns for different data types
Table-style joins on fields
Treat Redis hashes as table rows and join based on hash field values:
Advanced pattern: This approach allows you to:
- Field-based linking: Connect data based on hash field contents
- Batch processing: Handle multiple keys efficiently
- Performance: Needs to be reserved for very small datasets as it is very slow.
Performance considerations
- Connection pooling: Source connections are reused across queries
- Key patterns: Specify patterns for Redis optimization
- Batch operations: Multiple keys in a single query are processed efficiently
- Timeout management: Set appropriate timeouts for your latency requirements
Error handling
Redis operations can fail due to:
- Network connectivity issues
- Key not found scenarios
- Timeout exceeded
- Data type mismatches inducing JSON parsing errors
In all cases, the result is NULL. Use COALESCE to set a fallback strategy.