Ruby
Completed
-
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
224: Gradual Rollout Strategies
Imagine you're running a busy bakery. You've come up with a daring new sourdough recipe that you're convinced is better than the original. Now, you could swap the recipe for every single loaf you sell starting tomorrow morning, but that's a huge gamble. If the new recipe doesn't set right or customers hate the taste, you've just ruined your entire morning's revenue and potentially your reputation.
Instead, you do a "soft launch." You bake five loaves of the new sourdough and give them to your five most loyal regulars. If they love it, you bake twenty the next day. Then fifty. By the time you roll it out to every customer, you've already caught the fact that the new recipe needs ten more minutes in the oven, and you've fixed it without the general public ever knowing there was a problem.
In Ruby, gradual rollouts—often implemented via feature flags or "canary releases"—work exactly the same way. Instead of a "Big Bang" release where you merge a massive feature and hope for the best, you wrap the new logic in a conditional gate. You control who sees the new code without redeploying the application.
The Risk of the Big Bang
I've seen too many engineers treat a deployment like a leap of faith. You merge the PR, run the migration, and hit "Deploy." If there's a memory leak in your new logic that only triggers under heavy load, you won't find it until your entire site is down and your Slack is exploding with alerts. Gradual rollouts turn that leap of faith into a controlled walk. By limiting the blast radius, you can monitor your error rates and performance metrics for a tiny subset of users before committing everyone to the new path.
Building a Simple Feature Gate
While there are heavy-duty gems like flipper that handle this, it's important to understand the pattern. At its core, a gradual rollout is just a conditional check that looks at the current user's identity.
class FeatureGate
# In a real app, these would likely be stored in Redis or a DB
@flags = {
new_pricing_engine: { enabled: true, percentage: 10 }
}
def self.enabled?(feature, user)
config = @flags[feature]
return false unless config&.[](:enabled)
# Use the user's ID to create a consistent experience.
# We don't want the feature flickering on and off for the same user.
(user.id % 100) < config[:percentage]
end
end
Notice the user.id % 100 trick. I use this constantly. By modulo-ing the user ID, we ensure that a user who falls into the 10% bucket stays there. If you used rand(100), the user would see the new feature on one page refresh and the old one on the next, which is a great way to confuse your customers and make your debugging a nightmare.
Integrating the Rollout into Your Logic
Once the gate is built, you use it to bridge the gap between your legacy code and your new implementation. Let's say we're upgrading a pricing calculator for an e-commerce site.
class OrderProcessor
def calculate_total(order, user)
if FeatureGate.enabled?(:new_pricing_engine, user)
# The fancy new logic we're testing
NewPricingEngine.calculate(order)
else
# The battle-tested legacy logic
LegacyPricingEngine.calculate(order)
end
end
end
Now, the rollout process looks like this:
- Internal Testing: Set the percentage to 0, but hardcode your own user ID to always return
true. - The Canary: Set the percentage to 1%. Watch your logs. Are there more 500 errors for those users? Is the database CPU spiking?
- The Ramp: Move to 10%, then 25%, then 50%.
- The Cleanup: Once you hit 100% and are confident, delete the
if/elseblock and theFeatureGateentry. This is the part everyone forgets, and it's how "temporary" flags end up staying in codebases for five years.
📋 Practical Task
Implementing a Canary Rollout for a Tax Calculation Service
You are tasked with replacing a legacy SimpleTaxCalculator with a new GlobalTaxCalculator that handles international VAT. Because this affects payments, you cannot risk a full rollout.
Your Task:
Implement a RolloutManager class and update the CheckoutService to gradually roll out the new calculator.
Requirements:
- Create a
RolloutManagerthat has a methodenabled?(feature_name, user). - The
RolloutManagershould store a hash of features and their rollout percentages (e.g.,{ global_tax: 20 }). - Ensure that the rollout is deterministic—the same user must always get the same experience based on their
user_id. - Update the
CheckoutService#apply_taxmethod to use theRolloutManagerto decide whether to useSimpleTaxCalculator.calculate(amount)orGlobalTaxCalculator.calculate(amount).
Starter Code:
class User
attr_reader :id
def initialize(id); @id = id; end
end
class SimpleTaxCalculator
def self.calculate(amount); amount * 0.05; end
end
class GlobalTaxCalculator
def self.calculate(amount); amount * 0.15; end
end
class CheckoutService
def apply_tax(amount, user)
# YOUR CODE HERE
end
endThere are no comments for now.