Avoiding CookieOverflow in Ruby on Rails with Redis Session Store (Single & Cluster Mode Setup)
In modern web applications, managing user sessions efficiently is essential for performance and stability. Ruby on Rails applications, by default, use CookieStore to store session data. However, this approach has limitations, especially when session data grows too large — leading to the infamous ActionDispatch::Cookies::CookieOverflow error.
In this article, we’ll explore:
- Why
CookieOverflowhappens - How
redis_storeor database-backed sessions help - Setting up
redis_storefor single Redis instances - Handling Redis Cluster mode for session storage
1.❗ Why Does CookieOverflow Happen?
Rails uses ActionDispatch::Cookies::CookieJar to store session data when :cookie_store is the default session store.
The browser imposes a size limit (usually around 4KB per cookie) — including name, value, and metadata. When your session data exceeds that limit, Rails throws:
ActionDispatch::Cookies::CookieOverflow (ActionDispatch::Cookies::CookieOverflow)
- Common Causes
- Storing too much data in the session (e.g., long lists of selected items, objects serialized into the session)
- Using Devise or OmniAuth with large tokens or excessive flash messages
- Multilingual applications with large locale data
2. ✅ Solution: Use redis_store or ActiveRecord::SessionStore
Instead of storing session data in the cookie, offload it to:
- In-Memory Stores (like Redis) for speed and scalability
- Database-backed Sessions using
activerecord-session_storefor persistence and backup
- Why Redis?
Redis is a fast, in-memory key-value store — ideal for session data. It offers:
- Speed (microsecond latency)
- Expiration and eviction policies
- Scalability via clustering
- Optional persistence
3. 🛠️ How to Set Up redis_store
You’ll need the following gems in your Rails project:
# Gemfile gem 'redis' gem 'redis-rails' gem 'redis-clustering' # For Redis cluster mode setup
Note: In Rails 7+,
redis-railsmay be deprecated in favor ofredis-storeorredis-activesupport.
- Common Config
Define these settings in config/environments/production.rb (or development.rb if testing locally):
session_key = '_your_app_session' session_timeout = 1.hour
4. 🔧 Single Redis Node Setup
Rails.application.config.session_store :redis_store, servers: Settings.redis.url, # e.g., "redis://localhost:6379/0" expire_after: session_timeout, key: session_key, reconnect_attempts: 3
This config will:
- Store session data in Redis (e.g.,
redis://localhost:6379) - Set session expiration to 1 hour
- Retry connecting up to 3 times on failures
5. 🗂️ Redis Cluster Mode Setup
When using Redis Cluster Mode (multiple nodes for HA and scalability), use RedisClient.cluster to handle connections properly.
redis_client = RedisClient.cluster( nodes: [Settings.redis.url], # e.g., array of URLs like ["redis://10.0.0.1:6379", "redis://10.0.0.2:6379"] timeout: 2.0, reconnect_attempts: 3 ) Rails.application.config.cache_store = :redis_cache_store, { redis: redis_client, namespace: 'sessions' } Rails.application.config.session_store :cache_store, key: session_key, expire_after: session_timeout, secure: Rails.env.production?
- Notes:
- Use
:cache_storeas a wrapper when using Redis Cluster namespace: 'sessions'keeps keys organized and avoids collisionsecure: trueensures session cookies are HTTPS-only in production
6. 📦 Bonus: Using ActiveRecord Session Store (Alternative)
# Gemfile gem 'activerecord-session_store'
rails generate active_record:session_migration rails db:migrate
Rails.application.config.session_store :active_record_store, key: session_key, expire_after: session_timeout
7. ✅ Summary
| Store Type | Pros | Cons |
|---|---|---|
| Cookie Store | Simple, stateless | 4KB limit, not scalable |
| Redis Store | Fast, scalable, clustered | Needs Redis infrastructure |
| ActiveRecord Store | Persistent, good for audit | Slower, DB size increases |
To prevent CookieOverflow, switching to redis_store — especially in high-scale environments — is a production-proven approach to reliable session handling in Rails.

