-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
116: Practice Exercise: Building a Rate Limiter with Redis
I was looking through the logs for our mock Weather API yesterday, and I noticed a few users were hitting the endpoint hundreds of times a second. It’s not a DDoS attack, just a few poorly written scripts, but it's slowing down the app for everyone else. We need a rate limiter.
The memory trap
My first instinct was to just throw a Hash into a Ruby class to keep track of request counts. It seems easy: key the hash by user ID, increment a value, and block them if the value hits a limit. Let's see how that looks.
class SimpleLimiter
def initialize
@requests = Hash.new(0)
end
def allowed?(user_id)
@requests[user_id] += 1
@requests[user_id] <= 5
end
end
limiter = SimpleLimiter.new
# This works... until it doesn't.
The problem here is obvious once you think about how we actually deploy Ruby apps. We aren't running one single process on one single machine; we have three Puma workers across two different servers. If a user hits Server A, then Server B, their count is split. They get double (or triple) the quota. Plus, if I restart the server, the counts vanish. We need a shared state that lives outside the Ruby process. That's where Redis comes in.
Moving the state to Redis
Since Redis is essentially a giant, networked key-value store, it's perfect for this. I'll swap the Ruby hash for a Redis client. I'll use the INCR command because it's atomic—I don't have to worry about two requests hitting the server at the exact same microsecond and overwriting each other's count.
require 'redis'
$redis = Redis.new
def allowed?(user_id)
key = "rate_limit:#{user_id}"
count = $redis.incr(key)
count <= 5
end
I tried this out, and it successfully blocked the user after 5 requests. But then I realized I'd created a permanent ban. Once a user hits 5 requests, they are blocked forever because that key in Redis never goes away. That's a terrible user experience. We need a "window"—usually a minute—after which the count resets.
The expiration glitch
The obvious fix is to use EXPIRE. I'll tell Redis to kill the key after 60 seconds. I'll just tack it onto the end of the method.
def allowed?(user_id)
key = "rate_limit:#{user_id}"
count = $redis.incr(key)
$redis.expire(key, 60) # Reset the window every minute
count <= 5
end
Wait, I just spotted a bug in my own logic. By calling expire on every single request, I'm not creating a fixed 60-second window; I'm creating a sliding window. If a user sends a request every 50 seconds, the key never expires, and they'll eventually hit the limit and stay blocked as long as they keep trying. That's not what I want. I want the window to start on the first request and end 60 seconds later, regardless of subsequent activity.
Locking in the window
To fix this, I only want to set the expiration when the counter is at 1. That means this is the very first request in a new window.
def allowed?(user_id)
key = "rate_limit:#{user_id}"
count = $redis.incr(key)
# Only set the timeout on the first request of the window
$redis.expire(key, 60) if count == 1
count <= 5
end
This is much better. Now, the first request triggers the 60-second countdown. The next four requests increment the counter but don't touch the expiration timer. After 60 seconds, Redis wipes the key, and the user gets a fresh start.
One last thing: in a production environment, there's a tiny "race condition" here. If the server crashes exactly between incr and expire, you'll end up with a key that has no expiration—a permanent ban again. To be truly bulletproof, I'd use a Lua script to make the increment and expiration happen as a single atomic operation inside Redis, but for most internal tools, this logic gets us 99% of the way there.
📋 Practical Task
Build a Per-IP Request Throttler for a Public API
You are tasked with protecting a public-facing API from abuse. Instead of using a User ID (which requires the user to be logged in), you need to build a rate limiter based on the user's IP address.
Create a class called IpRateLimiter that implements the following requirements:
- The class should be initialized with a
redisinstance and amax_requestslimit. - Implement a method
allowed?(ip_address, window_seconds = 60)that returnstrueif the IP is within the limit andfalseif they have exceeded it. - The expiration timer must only be set on the first request of the window to avoid the "sliding window" problem.
- Ensure the Redis keys are namespaced (e.g.,
"ip_limit:127.0.0.1") so they don't collide with other data in your database.
Testing your code: Try simulating 10 rapid requests from the same IP and verify that only the first max_requests return true.
There are no comments for now.