Performance: Rate limiting
Rate limiting controls how often users can make requests, affecting server load and response times.
class ApplicationController < ActionController::Base before_action :check_rate_limit def check_rate_limit if Redis.current.incr(user_rate_key) > 1000 render plain: 'Rate limit exceeded', status: 429 end end end
class ApplicationController < ActionController::Base before_action :check_rate_limit def check_rate_limit if request_count_for_user > 1000 render plain: 'Rate limit exceeded', status: 429 end end end
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| In-memory request counting | 0 | 0 | 0 | [X] Bad |
| Redis atomic counters for rate limiting | 0 | 0 | 0 | [OK] Good |
throttle with arguments: name, limit, period, and a block returning the key.throttle with named parameters limit: 5 and period: 1.minute, and block returning req.ip, which is correct.Rack::Attack.throttle('logins/ip', limit: 3, period: 20.seconds) { |req| req.ip if req.path == '/login' && req.post? }Rack::Attack.throttle('api/ip', limit: 10, period: 1.minute) do |req|
req.ip if req.path.start_with?('/api')
endreq.ip only if path starts with '/api', else returns nil.req.params['email'] for email throttle and req.ip for IP throttle with proper limits.Pass the test and this pattern moves unknown