Local Cache

The local cache is an in-memory caching layer that reduces Redis load by storing frequently accessed sessions locally on each application node.

How It Works

spinner

Cache Entry Lifecycle

spinner

Configuration

Session.configure do |config|
  config.cluster.local_cache_enabled = true
  config.cluster.local_cache_ttl = 30.seconds
  config.cluster.local_cache_max_size = 10_000
end

TTL (Time-To-Live)

Each cache entry has a TTL after which it's considered expired:

TTL Value
Use Case

5.seconds

Near real-time consistency needed

30.seconds

Default, good balance

1.minute

High traffic, stable sessions

5.minutes

Very high traffic, infrequent changes

Max Size and LRU Eviction

When the cache reaches max_size:

  1. Expired entries are removed first

  2. LRU eviction removes least recently accessed entries

Cache Statistics

Access cache statistics programmatically:

Understanding Hit Rate

spinner

A healthy cache should have:

  • Hit rate > 80% for typical web applications

  • Hit rate > 90% for read-heavy applications

Low hit rate indicates:

  • TTL is too short

  • Cache size is too small

  • Sessions are being invalidated frequently

Cache Invalidation

Automatic Invalidation

The cache is automatically invalidated when:

  1. Entry expires (TTL exceeded)

  2. Session deleted (via store.delete)

  3. Cluster message received (from another node)

  4. Cache cleared (via store.clear)

Manual Cache Operations

Thread Safety

The LocalCache class is thread-safe:

  • All operations are protected by a Mutex

  • Safe to use from multiple fibers

  • No external synchronization required

Memory Management

Estimating Memory Usage

Monitoring Memory

Best Practices

1. Size Your Cache Appropriately

2. Set TTL Based on Session Update Frequency

3. Monitor Cache Performance

4. Consider Disabling for Specific Use Cases

Last updated

Was this helpful?