Clustered Redis Store

The Clustered Redis Store extends the standard Redis Store with local caching and cluster-wide session invalidation via Redis Pub/Sub.

Overview

spinner

Basic Usage

require "session"

# Define session data
struct UserSession
  include Session::SessionData
  property user_id : Int64?
  property username : String?
end

# Configure with clustering
Session.configure do |config|
  config.secret = ENV["SESSION_SECRET"]
  config.timeout = 1.hour

  # Enable clustering
  config.cluster.enabled = true
  config.cluster.node_id = ENV["NODE_ID"]? || UUID.random.to_s

  # Use clustered store
  config.provider = Session::ClusteredRedisStore(UserSession).new(
    client: Redis.new(host: "redis.example.com")
  )
end

Features

Local Caching

Sessions are cached locally to reduce Redis roundtrips:

Cluster Invalidation

When a session is deleted, all cluster nodes are notified:

spinner

Message Types

Message Type
Trigger
Action on Other Nodes

SessionDeleted

store.delete(id)

Evict from local cache

SessionInvalidated

store[id] = session

Evict from local cache

CacheClear

store.clear

Clear entire local cache

API Reference

Constructor

Store Operations

Cache Operations

Cluster Operations

Health & Lifecycle

QueryableStore Methods

Configuration Examples

Production Setup

Development Setup

High-Traffic Setup

Comparison with Redis Store

Aspect
RedisStore
ClusteredRedisStore

Local caching

No

Yes

Pub/Sub sync

No

Yes

Read latency (cached)

Redis RTT

~0 (memory)

Read latency (miss)

Redis RTT

Redis RTT

Write latency

Redis RTT

Redis RTT + cache

Delete latency

Redis RTT

Redis RTT + broadcast

Memory usage

Low

Higher (cache)

Consistency

Strong

Eventual

Best Practices

1. Use Unique Node IDs

2. Size Cache Appropriately

3. Set TTL Based on Access Patterns

4. Monitor Cache Performance

5. Handle Graceful Shutdown

Last updated

Was this helpful?