Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Laravel Pennant handle feature flag caching


How does Laravel Pennant handle feature flag caching


Laravel Pennant handles feature flag caching primarily through an in-memory cache during a single request lifecycle, alongside optional persistent storage backed by a database.

In-Memory Cache

- When a feature flag is checked, Pennant calculates the result and caches it in memory for the duration of that request. This means if the same feature flag is checked multiple times within a single request, Pennant will return the cached result without recalculating or querying the database again.
- This in-memory caching ensures consistent feature flag results throughout the request and reduces redundant queries or computations.
- If you want to clear this in-memory cache manually during a request, you can use the `flushCache` method on the `Feature` facade:

php
  Feature::flushCache();
  

- This in-memory cache is present regardless of the storage driver used but is especially beneficial when using the `database` driver to avoid multiple database queries per request.

Persistent Storage Drivers and Caching

Pennant supports two main storage drivers for caching feature flag results beyond the request:

- Array driver: Stores feature flag results in an in-memory PHP array. This cache lasts only for the current request and is lost afterward. It is useful for simple or local development scenarios where persistence is not required.

- Database driver (default): Stores resolved feature flag values persistently in a `features` database table. This enables caching across requests and servers, making it suitable for multi-server or serverless environments.

- When using the database driver, the first check of a feature flag will calculate and store the result in the database.
- Subsequent requests or checks will read the cached result from the database rather than recalculating.
- This reduces the overhead of complex logic or external calls to determine feature flag states on every request.
- Despite persistent caching, Pennant still uses the in-memory cache during a request to minimize database queries.

Performance Optimization with Eager Loading

- Pennant provides methods like `load()`, `loadMissing()`, and `loadAll()` to eager load feature flags for collections of scopes (e.g., multiple users) to avoid the "N+1 query problem."
- For example, if you check a feature flag for many users in a loop, eager loading will fetch all needed feature flag states in fewer queries upfront, then use the in-memory cache for subsequent checks within that request.

Custom Cache Strategies

- Pennant does not provide built-in support for external cache stores (like Redis or Memcached) out of the box for feature flags.
- However, you can implement a custom cache decorator driver that wraps existing drivers to add caching layers if needed.
- You can also switch to the `array` driver to disable persistent caching and force feature flags to be computed on every request.

Summary

- Pennant caches feature flag results in-memory per request to avoid repeated evaluations.
- The database driver persists cached results across requests in a `features` table, improving performance in distributed environments.
- The array driver caches only in-memory per request, causing recalculation on every new request.
- Eager loading methods help optimize database queries when checking flags for multiple scopes.
- Manual cache flushing and custom caching implementations are supported for advanced use cases.

This caching design balances performance and consistency while allowing flexibility depending on your environment and needs[6][5][2][7].

Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://github.com/laravel/pennant/issues/68
[3] https://stackoverflow.com/questions/77078816/how-to-disable-caching-for-laravel-pennant-in-db
[4] https://laracasts.com/episodes/2685
[5] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[6] https://laravel.com/docs/12.x/pennant
[7] https://dev.to/saurabh-dhariwal/how-to-manage-feature-flags-with-laravel-pennant-in-2024-1phb
[8] https://laravel.com/docs/11.x/cache
[9] https://kinsta.com/blog/laravel-caching/