Skip to main content

Redis usage

In the application Redis database is currently used for the following purposes:

  1. As a storage for Sidekiq
  2. As backend storage for feature flags with Flipper
  3. As a storage for session data
  4. As a token bucket rate limiter using ratelimit gem

Current database assignment

In order to avoid key clash between different usages, we use the following database assignment:

DatabaseUsage
0Sidekiq
2Flipper
3Session Storage
4Rate Limiter

You can find connection details by searching for REDIS_URL env variable in the project or looking at file config/initializers/redis.rb.

Feature flags are read lazily, not preloaded

Flipper's railtie installs Flipper::Middleware::Memoizer with preloading on by default, which runs preload_allSMEMBERS flipper_features plus a pipelined HGETALL — on every request, before any controller. config/initializers/flipper.rb turns that off with config.flipper.preload = false.

Memoization itself stays on: a request that reads a flag still pays one round trip no matter how many times it reads it. Only the unconditional up-front fetch is gone, so a request that reads no flag — the probes included — never touches Redis at all.

Two reasons:

  • The probes could not report a Redis outage. The middleware is the one unconditional Redis call in the stack, so an unreachable broker turned every request into an uncaught exception, /healthz included. Its own providers rescue and answer 503 naming the failed dependency, but the middleware raised before the request ever reached them — which is what made a broker restart show up as a burst of errors and restarted pods rather than a quiet 503.
  • It bought nothing. The app has seven feature checks, four of them on web paths, and the typical request evaluates none. In production the pair of commands averaged ~2.8ms against a ~15ms median request.

The trade is that a request reading several distinct flags now issues one HGETALL each instead of a single pipelined fetch. With at most two distinct flags on any current path that is never the worse deal, but it is worth re-checking if a code path ever starts reading many flags at once.