Understanding Rails · Season 4: Controllers, routing and auth
Rate limiting
Episode 32 · deep dive · about 3 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
rate_limit in a controller, and what it stores.
Free to watch. If it helped, support the series.
Transcript
0:00 Rate limiting
Episode thirty two. Rate limiting. Episode thirty's sign in stopped the eleventh guess. Today: how rate limit works, where it keeps its count, and the trap that hides it in your tests.
0:16 One line
Rate limit is one line in a controller. Here, creating notes: to three, within one minute, only for create, and by the current user's id, instead of the default, which is the IP address.
0:29 The fourth request
Sign in as Alex and post four notes in a row. The first three redirect, as usual. The fourth gets four twenty nine: too many requests.
0:40 The counter
Rate limit is a before action. On each request, it adds one to a counter in the cache, under a key made of rate limit, the controller, and whatever by returned: here, rate limit, notes, two, Alex's id. The counter expires one minute after it starts. Read it back, and it says four.
0:58 The notification
When a request goes over, Rails sends a notification, rate limit dot action controller, with the count, the limit and the key. Subscribe to it, as episode twenty six did, and you can log or count refusals.
1:14 with
By default, an extra request gets a too many requests error, and the response is four twenty nine. With lets you choose instead. Episode thirty's sign in used with to redirect back, and say try again later.
1:28 Where the count lives
The count lives in the cache store. In development that's memory, inside one process. In production, it's Solid Cache, in the database, so every Puma process, and every server using that database, shares one count, and a restart doesn't reset it.
1:45 The test trap
Now the trap. Write a test that posts four notes, and expect four twenty nine. It fails: the fourth note was created. The test environment's cache store is null store, which remembers nothing, so the counter never counts.
2:00 A real store in tests
Swapping the controller's cache store inside the test doesn't help either. Rate limit picks its store when the class loads, before your test runs. So give the test environment a real store: memory store. Now the test passes. But a real store remembers between tests, too. Another test, posting notes as the same user, hit the limit and failed. So clear the cache before every test, in the test helper.
2:26 The rules
So: limit the actions people can abuse, like sign in, sign up, and anything that sends email. Choose what to count by. Use a shared cache store in production. And make your test environment's store real, so the limit is tested.
2:42 Next
Next: errors, rescue from, and error pages.
Check your understanding
-
rate_limit to: 3, within: 1.minute, with no by:. What does it count by?
- The signed-in user
- The request's IP address
- The session cookie
Show answer
The request's IP address. by: defaults to request.remote_ip. Pass by: to count something else, like the user's id. Source: rate_limit (Rails API 8.1).
-
Where does rate_limit keep its count?
- In the session
- In a database table of its own
- In the cache store, under a key made of rate-limit, the controller, and the by: value
Show answer
In the cache store, under a key made of rate-limit, the controller, and the by: value. Each request increments a cache entry, rate-limit:notes:2 for Alex, which expires when the window ends. Source: ActionController::RateLimiting (Rails source, v8.1.4.1).
-
A test posts four notes and expects 429, but gets a redirect. The test environment uses :null_store. Why?
- NullStore keeps nothing, so the count never rises
- Rate limits are turned off in tests
- The test needs to sleep for a minute
Show answer
NullStore keeps nothing, so the count never rises. NullStore doesn't persist values across requests, so every request sees a fresh count. Give the test environment a real store, and clear it before every test in the test helper. Source: Caching with Rails: NullStore (Rails Guides 8.1).
-
Why does production use Solid Cache for rate limits rather than :memory_store?
- Memory is too slow
- MemoryStore lives in one Ruby process, so each process would keep its own count
- MemoryStore can't increment
Show answer
MemoryStore lives in one Ruby process, so each process would keep its own count. MemoryStore keeps entries in the same Ruby process. A store in the database is shared by every process using it, and survives restarts. Source: Caching with Rails: MemoryStore (Rails Guides 8.1).
Sources
Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.