How tompave/fun_with_flags works
This project is an Elixir OTP application that implements a feature flag system. The core functionality revolves around toggling, querying, and managing feature flags in real time using different kinds of “gates” (e.g. boolean, actor, group, percentage-of-time, and percentage-of-actors). It provides a two-level storage model: a local ETS cache for quick lookups and a persistent store (either Redis or an Ecto-backed relational database) for durability and cross-node synchronization. In addition, it supports cache-busting notifications via PubSub, with adapters for Redis PubSub and Phoenix PubSub.
Main Components
• Application Core: Represent the FunWithFlags OTP application along with its supervision tree (see modules like FunWithFlags.Supervisor, FunWithFlags.Application). Indicate that this is the entry point and coordinator.
• API Layer: Show the exposed API functions (e.g. FunWithFlags.enabled?/2, enable/disable/clear) which serve as an interface for other parts of the host application.
• Persistence Adapter: Illustrate the two persistence strategies – one using Redis (via the Redix adapter) and one using Ecto (supporting PostgreSQL, MySQL, SQLite). Include components like:
• Persistent Store modules (under lib/fun_with_flags/store/persistent).
• Serializer submodules for translating internal data structures (under lib/fun_with_flags/store/serializer).
• Notification System: Include the PubSub component for distributed cache invalidation. Display the two notification modules available – one for Redis (lib/fun_with_flags/notifications/redis.ex) and another for Phoenix PubSub (lib/fun_with_flags/notifications/phoenix_pubsub.ex).
• Protocols: Represent the extensibility points via protocols (FunWithFlags.Actor and FunWithFlags.Group) which allow custom types to be used as “actors” or group identifiers. These define how entity IDs are computed and group membership is determined.
• Development & Benchmark Tools: Optionally, include components like the benchmarking scripts (in benchmarks/) that help assess performance.
Relationships and Interactions
• The API Layer calls into the core modules which check the ETS cache first. If required, they invoke persistence adapter operations (read/write flag definitions) to get the latest data.
• When a flag’s state is modified (e.g. toggled on/off, updated percentages, or cleared), the persistence adapter updates the store and then triggers a notification via the PubSub system.
• The Notification System then broadcasts a “cache bust” which causes the ETS cache in all nodes to refresh.
• The gate handlers determine the final enabled/disabled state by evaluating the rules stored for a flag, following the specific priority order (actor > group > boolean > percentage).
• The protocols ensure that custom actor or group data is converted appropriately for evaluation.
Architectural Patterns and Design Principles
• Layered Architecture: The system is clearly separated into concerns – API, caching, persistence, notifications, and gate evaluation logic.
• Adapter/Plug-in Pattern: The design supports multiple persistence and PubSub adapters. This abstraction makes the system flexible and easily extensible.
• Caching with Cache Invalidation: The use of ETS for caching, combined with PubSub notifications, is an important performance optimization concept reflected in the design.
• Use of Protocols: The protocol approach in Elixir gives you polymorphic behavior for handling various actor types.
• Supervision and Fault Tolerance: As an OTP app, starting and managing processes (using a supervision tree) is central to the design.