A Rails 8.1 ride-hailing app runs on several servers with Puma workers behind a load balancer; what must its rate_limit store and client identity be for the limit to hold?
answer
- memory_store counts per process
- file_store counts per machine
- Solid Cache shared through the database
- remote_ip depends on trusted_proxies
- carrier NAT shares one IP
basics
~20 sThe counter needs a store every process shares, such as the generated Solid Cache store, and by: must name the real client: request.remote_ip does so only when every proxy hop is trusted, so key signed-in endpoints on the account.
solid answer
~40 s`rate_limit` increments a key in its store, so the limit is only as global as the store. `:memory_store` is private to one Puma worker, so four workers on each of three servers allow up to twelve times `to:`; `:file_store`, the framework default, is shared per machine. A new 8.1 app's production environment uses Solid Cache, which is database-backed and shared by every server. The default identity, `request.remote_ip`, is the rightmost `X-Forwarded-For` address that `ActionDispatch::RemoteIp` does not trust, and by default it trusts only loopback, private and link-local ranges. A public hop such as a CDN that is missing from `config.action_dispatch.trusted_proxies` makes `remote_ip` the CDN edge, shared by many clients; a proxy that sets no `X-Forwarded-For` makes it the proxy. Riders behind carrier NAT also share addresses, so key authenticated endpoints on the account.
code
ruby · 10 lines# config/environments/production.rb
config.cache_store = :solid_cache_store
# A CDN in front of the balancer: trust its edge ranges and keep the private defaults
config.action_dispatch.trusted_proxies =
ActionDispatch::RemoteIp::TRUSTED_PROXIES + [IPAddr.new("198.51.100.0/24")]
# app/controllers/quotes_controller.rb
class QuotesController < ApplicationController
rate_limit to: 30, within: 1.minute, by: -> { Current.user.id }, only: :create
endgo deeper
Know that the counter lives in the cache store, so the store must be shared for one limit to apply across servers.
Compare memory, file, Solid Cache and Redis stores for rate counting, and explain how remote_ip is derived from X-Forwarded-For.
Diagnose limits that are too loose or that throttle everyone at once, fix trusted_proxies without dropping the defaults, and choose per-endpoint identities.
Decide whether abuse controls share infrastructure with caching, and own the identity model, IP, account or device, across the product's endpoints.
## The limit is only as global as its store `rate_limit` keeps no state of its own. Every request calls `increment` on the store, and two requests are counted together only if they reach the **same store instance or backend**: | Store | Shared by | Effect with 3 servers x 4 workers | |---|---|---| | `:memory_store` | one process | up to 12 separate counters per client | | `:file_store` (framework default) | processes on one machine | up to 3 counters per client | | `:solid_cache_store` | every process using that database | 1 counter | | `:redis_cache_store`, `:mem_cache_store` | every process using that server | 1 counter | A new Rails 8.1 app's development environment uses `:memory_store` and its production environment, after the Solid Cache installer runs, `:solid_cache_store`. An app that copied the development setting into production, or never configured a cache store and fell back to the file store, enforces a much larger limit than the code says. Nothing errors; the numbers are just wrong. ## Sharing the cache with everything else Using the main cache store has two risks worth discussing in an interview: 1. **Eviction**: a size-capped store evicts old entries under pressure. A busy page cache can push out counters, quietly resetting them. 2. **Flushes**: `Rails.cache.clear` during an incident or deploy wipes every limit. Passing `store:` with a dedicated store isolates rate counters from both. The atomicity of `increment` is the store's job; the generated stores implement it per backend. ## The identity: who is the client? The default `by:` is `request.remote_ip`. `ActionDispatch::RemoteIp` walks `X-Forwarded-For` from the right, skipping addresses it trusts as proxies, and returns the first untrusted one; `REMOTE_ADDR` is considered only after the header. Its default trusted list covers loopback, private and link-local ranges. With a single balancer that appends the client to `X-Forwarded-For`, that already yields the rider's address. Problems arise when: - a **public** hop sits in the chain, such as a CDN in front of the balancer: the balancer records the CDN edge's address, which is not trusted, so `remote_ip` is the edge and every rider routed through it shares one counter. Add the CDN's ranges to `config.action_dispatch.trusted_proxies`; setting the list **replaces** the defaults, so keep the private ranges you still need. - a proxy sets no `X-Forwarded-For`: Rails falls back to `REMOTE_ADDR`, the proxy itself. - the app trusts too much: a client could then prepend a fake address to `X-Forwarded-For` and pick its own identity. - many legitimate users share one address, as riders on a mobile carrier's NAT do: a per-IP quote limit throttles a whole city block. ## Choosing by: per endpoint For the ride-hailing app: - **Sign-in**: per-IP as generated, because the caller is anonymous, plus optionally a named per-account limit. - **Fare quotes**: `by: -> { Current.user.id }`, since every quote request is authenticated and carrier NAT makes IPs a poor identity. - **Driver location pings**: probably no `rate_limit` at all; they are high-volume by design and belong to a different control. ## Symptoms and causes | Symptom | Likely cause | |---|---| | attackers get far more attempts than `to:` | per-process or per-machine store | | a whole office or city block is throttled together | identity is an address many clients share | | every client throttled at once after a CDN rollout | CDN edges missing from `trusted_proxies` | | limits reset at odd times | counters evicted or the cache cleared | ## Verifying in production 1. Subscribe to `rate_limit.action_controller` and log `cache_key` and `count`. 2. Trip a limit from one machine and confirm the count rises across servers. 3. Check that the logged `by` values are client addresses, not the CDN's or the balancer's. If counts climb in steps of one per server instead of one per request, the store is not shared.
- Why does adding a CDN's ranges to trusted_proxies require care?Assigning `config.action_dispatch.trusted_proxies` replaces Rails' default list of loopback, private and link-local ranges rather than extending it. If you list only the CDN, internal hops on private addresses, such as the balancer, stop being skipped. Append to `ActionDispatch::RemoteIp::TRUSTED_PROXIES` instead of replacing it.
- Can rate counters share the main cache store safely?They work, but a size-capped store may evict counters under load, and clearing the cache resets every limit. For limits that guard sign-in or SMS costs, pass a dedicated `store:` so page caching and incident cache flushes cannot reset them.
A per-process memory store is like each cashier keeping a private tally of a customer's refunds: a customer who changes lines resets the count, and only a shared ledger at the service desk catches them.
- :memory_store in production enforces the limit across all workers
- remote_ip takes the leftmost X-Forwarded-For address, so it is always the browser
- Setting trusted_proxies adds to Rails' default trusted ranges
- Each server's tmp/cache file store is shared with the other servers
- Per-IP limits are fair for authenticated mobile users