Redis

Redis

Overview

Learn how to use Redis for high-performance caching, real-time data streaming, and inter-service communication in the Sravz platform. This section covers Redis Timeseries for storing and querying time-series financial data.

What You’ll Learn

  • Redis Timeseries: Purpose-built module for time-series data
  • Real-time Data Storage: Store stock quotes with timestamps
  • Efficient Queries: Range queries, aggregations, and downsampling
  • High Performance: Sub-millisecond latency for reads/writes
  • Compaction: Automatic data retention and downsampling
  • Integration: Use Redis with C++, Go, Node.js, and Python backends

Key Technologies

  • Redis 7.x: In-memory data store
  • RedisTimeSeries: Time-series database module
  • Redis Streams: Message broker for event streaming
  • Redis Pub/Sub: Real-time messaging
  • RedisJSON: Store and query JSON documents
  • Redis Search: Full-text search and indexing

Documentation Index

  1. Redis Timeseries Introduction - Introduction to Redis Timeseries module for financial time-series data

Use Cases

Time-Series Data

  • Stock Quotes: Store real-time tick data
  • OHLCV Bars: 1-minute, 5-minute, hourly, daily bars
  • Trading Signals: Strategy signals with timestamps
  • Performance Metrics: System performance monitoring

Caching

  • S3 Presigned URLs: Cache expensive URL generation
  • API Responses: Cache external API calls
  • Database Queries: Cache frequent MongoDB queries
  • Session Data: User session storage

Streaming

  • Redis Streams: Event sourcing and message queues
  • Pub/Sub: Real-time notifications
  • Inter-service Communication: Connect C++, Go, Node.js services
  • WebSocket Broadcasting: Push updates to clients

Redis Timeseries Features

Data Storage

TS.ADD ticker:AAPL 1638360000000 150.25 LABELS symbol AAPL type quote

Range Queries

TS.RANGE ticker:AAPL 1638360000000 1638446400000 AGGREGATION avg 3600000

Compaction Rules

  • Automatically downsample high-resolution data
  • Multiple retention policies (1min → 1hour → 1day)
  • Save storage while preserving historical data

Aggregations

  • avg: Average price over time window
  • min/max: Price range
  • sum: Total volume
  • count: Number of samples
  • first/last: Opening/closing prices

Architecture

Data Flow

Market Data → Backend (C++/Go) → Redis Timeseries → Query Layer → UI
                                       ↓
                               Downsampling & Retention

Integration Points

  • C++ Backend: Boost Redis client
  • Go Backend: go-redis library
  • Node.js: ioredis client
  • Python: redis-py
  • Rust: redis-rs

Performance Benefits

Speed

  • In-Memory: Sub-millisecond latency
  • Optimized Storage: Compressed time-series format
  • Fast Queries: Native time-range operations
  • Batch Operations: Pipeline multiple commands

Scalability

  • Redis Cluster: Horizontal scaling
  • Replication: High availability
  • Persistence: AOF and RDB snapshots
  • Compression: Efficient storage format

Common Patterns

Real-time Ticker Plant

WebSocket → Parse → Redis Timeseries → [Streams] → Consumers

Cache-Aside Pattern

Request → Check Redis → Hit? Return : Query DB → Cache → Return

Event Sourcing

Events → Redis Streams → Consumers → Process → Update State

Best Practices

Time-Series

  • Use Labels: Tag series with metadata (symbol, type)
  • Retention Policies: Define appropriate TTLs
  • Compaction Rules: Downsample old data
  • Duplicate Policy: Handle duplicate timestamps

Caching

  • TTL: Set expiration for cache entries
  • Eviction Policy: Use LRU for memory limits
  • Cache Warming: Pre-populate frequent queries
  • Invalidation: Clear cache on data updates

Streaming

  • Consumer Groups: Distribute work across instances
  • ACKs: Acknowledge processed messages
  • Dead Letter Queue: Handle failed messages
  • Monitoring: Track lag and throughput

Getting Started

  1. Read Redis Timeseries Introduction
  2. Install Redis with TimeSeries module
  3. Store sample stock quotes
  4. Query and aggregate data
  5. Set up compaction rules

Advanced Topics

  • Redis Cluster: Sharding for large datasets
  • Redis Streams: Event-driven architectures
  • RedisJSON: Store complex documents
  • RedisSearch: Full-text indexing
  • RedisGraph: Graph database capabilities

Related: C++ | Golang | Rust