Ever launched a new feature only to discover it breaks production after hours of traffic?
Why Feature Flags Matter in Laravel
Feature flags let you decouple deployment from release. A 2022 survey of 1,200 developers reported that 68% of teams use flags to reduce hot‑fix incidents, and Laravel’s ecosystem now includes a first‑class solution: Laravel Pennant. By toggling code paths at runtime, you gain safety, can test with real users, and avoid costly rollbacks.
Getting Started with Laravel Pennant
First, ensure your Laravel version is 10 or higher. Then install the package via Composer:
composer require laravel/pennant Publish the configuration and migration files:
php artisan vendor:publish --tag=pennant-config && php artisan migrate After migration, a features table will store flag states. Define a flag in app/Providers/AppServiceProvider.php:
use Laravel\Pennant\Feature; Feature::define('new-dashboard', false); Now you can check the flag anywhere in your application:
if (Feature::active('new-dashboard')) { return view('dashboard.new'); } else { return view('dashboard.old'); } Persisting Flags with Cloudflare KV
For high‑traffic SaaS products, a relational database can become a bottleneck. Cloudflare KV offers low‑latency key‑value storage at the edge. Install the Cloudflare SDK:
composer require cloudflare/kv-php-sdk Configure a custom driver in config/pennant.php:
'stores' => [ 'cloudflare' => [ 'driver' => 'cloudflare', 'account_id' => env('CF_ACCOUNT_ID'), 'api_token' => env('CF_API_TOKEN'), 'namespace_id' => env('CF_KV_NAMESPACE'), ], ], Set the default store to cloudflare and clear the cache when a flag changes:
Feature::whenChanged(function ($name) { Cache::store('cloudflare')->forget('feature-'.$name); }); Now every request reads the flag from the nearest edge node, reducing latency to under 30 ms for global users.
Implementing A/B Tests with Pennant
A/B testing becomes a natural extension of flags. Define a test flag that randomly assigns 20% of traffic to variant B:
Feature::define('homepage-variant', function () { return rand(1,100) <= 20; }); In your controller, render different views based on the flag:
return view(Feature::active('homepage-variant') ? 'home.variant' : 'home.control'); Track conversions with an event listener:
Event::listen('conversion', function ($payload) { $variant = Feature::active('homepage-variant') ? 'B' : 'A'; DB::table('ab_results')->insert(['variant' => $variant, 'user_id' => $payload['user_id'], 'created_at' => now()]); }); After a week, query the ab_results table to calculate lift. The same flag can be toggled off instantly if the variant underperforms.
Gradual Rollout Strategies
Gradual rollout minimizes risk by exposing a feature to a controlled slice of users. Combine Pennant with Cloudflare KV to store a rollout percentage:
Feature::define('beta-search', function () { $pct = Cache::store('cloudflare')->get('beta-search-pct', 5); return rand(1,100) <= $pct; }); Adjust the percentage without redeploying:
php artisan tinker --execute="Cache::store('cloudflare')->put('beta-search-pct', 25);" Monitor error rates via Laravel Telescope or your preferred observability tool. If the error rate spikes above 2 %, roll back by setting the percentage back to 0.
Monitoring, Rollback, and Best Practices
Keep a dashboard that lists active flags, their stores, and last updated timestamps. Use Laravel Nova or a simple Blade view that reads Feature::all(). Always version‑control flag definitions; a Git diff shows who introduced a flag and why.
When retiring a flag, remove its definition, delete the corresponding KV key, and run a migration to drop the column if you stored any historic data. This prevents stale data from cluttering your database.
Conclusion
Feature flags are no longer an optional luxury for Laravel teams—they are a prerequisite for safe continuous delivery. Laravel Pennant provides a clean API, Cloudflare KV delivers edge‑level performance, and a disciplined A/B testing workflow turns every flag into a data‑driven experiment. By adopting the patterns above, you can ship, test, and roll back features in minutes instead of days.
Sources
Laravel Official Documentation – Pennant
Cloudflare Developers – KV API
Laravel News – Feature Flag Best Practices
Author: Mahmut Sarıkaya — sarikayadev.com