4 Hours

12. Redis Stack: JSON, Search & Time-Series

Extensions

Redis Stack: Extending Redis with Modern Capabilities

Redis Stack extends Redis with powerful new capabilities: JSON for document storage, Search for full-text search, Time-Series for time-series data, and Probabilistic data structures. These modules make Redis a complete data platform capable of handling modern application workloads.

Redis Stack is essential for modern applications. JSON enables document storage with nested data and JSONPath queries. Search enables full-text search and indexing with powerful query capabilities. Time-Series enables efficient time-series data storage with downsampling and aggregation. Probabilistic data structures enable advanced analytics with Bloom filters, Cuckoo filters, and more. Understanding Redis Stack unlocks Redis's full potential and makes it a complete data platform.

This module covers each Redis Stack module in depth with practical examples and use cases.

Redis JSON

Redis JSON provides native JSON document storage. You can store nested JSON documents with full CRUD operations. Redis JSON supports JSONPath queries. This makes Redis a document store. Use JSON for storing complex, structured data that needs nested queries.

Redis JSON stores documents in a binary JSON format for efficient storage and retrieval. It supports JSONPath for querying nested fields. Operations are atomic and efficient. Redis JSON is ideal for storing user profiles, product catalogs, and any structured data.

JavaScript
// JSON operations
JSON.SET user:1 $ '{"name":"Alice","age":30,"address":{"city":"NYC"},"tags":["redis"]}'
JSON.GET user:1
JSON.GET user:1 $.name
JSON.GET user:1 $.address.city
JSON.GET user:1 $.tags[0]

JSON.SET user:1 $.age 31
JSON.NUMINCRBY user:1 $.age 1
JSON.ARRAPPEND user:1 $.tags "database"
JSON.DEL user:1 $.address.city

// JSONPath queries
JSON.GET user:1 $..["name", "age"]

Redis Search

Redis Search provides full-text search and indexing. You can index documents and query with text search, filters, and sorting. Redis Search is fast and efficient. Use Search for any search capability — product search, document search, or any full-text search need.

Redis Search builds inverted indexes for efficient text search. It supports querying with boolean operators, field filters, sorting, and pagination. It also supports highlighting and aggregation. Redis Search is ideal for building search features in applications.

JavaScript
// Create an index
FT.CREATE idx:users ON JSON SCHEMA $.name TEXT $.age NUMERIC $.email TEXT

// Add documents (automatically indexed)
JSON.SET user:1 $ '{"name":"Alice","age":30,"email":"alice@example.com"}'
JSON.SET user:2 $ '{"name":"Bob","age":25,"email":"bob@example.com"}'
JSON.SET user:3 $ '{"name":"Charlie","age":35,"email":"charlie@example.com"}'

// Search
FT.SEARCH idx:users 'Alice'
FT.SEARCH idx:users '@age:[20 30]'
FT.SEARCH idx:users 'Alice' LIMIT 0 10 RETURN 1 $.name

// Aggregations
FT.AGGREGATE idx:users '*' GROUPBY 1 @$.age REDUCE COUNT 0 AS count

Redis Time-Series

Redis Time-Series provides efficient storage for time-series data. It supports downsampling, aggregation, and filtering. Use Time-Series for IoT, monitoring, and analytics applications where time-based data is critical.

Redis Time-Series stores data in a compressed format for efficient storage. It supports labels for filtering and aggregation for downsampling. It's ideal for storing sensor data, application metrics, and financial data. Time-Series is essential for modern analytics applications.

JavaScript
// Time-Series operations
TS.CREATE temperature RETENTION 86400000 LABELS sensor_id 1
TS.ADD temperature 1609459200000 25.5
TS.ADD temperature 1609459260000 26.1
TS.ADD temperature 1609459320000 26.3

// Query time range
TS.RANGE temperature 1609459200000 1609459320000

// Aggregations (avg over 1 minute)
TS.RANGE temperature 1609459200000 1609459320000 AGGREGATION avg 60000

// Multiple series with labels
TS.MRANGE 1609459200000 1609459320000 FILTER sensor_id=1

Knowledge Check

Ready to test your understanding of 12. Redis Stack: JSON, Search & Time-Series?

Back