Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

🚩 Feature Flag System

An enterprise-grade, production-ready Feature Flag Management System built with Spring Boot 3, PostgreSQL, and Redis.


✨ Features

Feature Details
Boolean, String, Number, JSON flags Multi-type flag values
Percentage Rollout Deterministic user bucketing via hash
Targeting Rules Attribute, Segment, Regex, SemVer operators
Multi-Environment Development, Staging, Production (custom supported)
Audit Trail Full history of every flag change
JWT + API Key Auth Dual authentication mechanisms
RBAC ADMIN / EDITOR / VIEWER roles
Redis Caching TTL-based cache with automatic invalidation
Async Audit Logging Non-blocking audit writes
OpenAPI / Swagger UI Full interactive documentation

🏗️ Architecture

┌─────────────────────────────────────────────────────────────────┐
│                         REST Controllers                        │
│  AuthController │ FlagController │ EvaluationController │ Admin │
└────────────────────────┬────────────────────────────────────────┘
                         │
┌────────────────────────▼────────────────────────────────────────┐
│                       Service Layer                             │
│  FeatureFlagService │ EvaluationService │ AuditService          │
└────────────────────────┬────────────────────────────────────────┘
                         │
         ┌───────────────┼───────────────────┐
         │               │                   │
┌────────▼───────┐ ┌─────▼─────┐ ┌──────────▼──────────┐
│   Repository   │ │   Cache   │ │  Strategy Pattern   │
│   (JPA/JDBC)   │ │  (Redis)  │ │  Rule Evaluation    │
└────────┬───────┘ └───────────┘ └─────────────────────┘
         │
┌────────▼───────┐
│  PostgreSQL DB  │
└────────────────┘

🚀 Quick Start

Prerequisites

  • Docker & Docker Compose

Run with Docker

git clone <repo-url>
cd feature-flag-system
docker-compose up --build

App will be available at: http://localhost:8080/api

Run Locally

# Start infrastructure
docker-compose up postgres redis -d

# Run application
./mvnw spring-boot:run

🔐 Authentication

Two methods supported:

JWT (Dashboard / UI)

POST /api/auth/login
Content-Type: application/json

{
  "username": "admin",
  "password": "Admin@123"
}

Response contains accessToken — use in subsequent requests:

Authorization: Bearer <accessToken>

API Key (SDK / Server-to-Server)

X-API-Key: ff_live_<your-api-key>

📡 API Reference

Feature Flag Lifecycle

POST   /api/flags                                  Create flag
GET    /api/flags?search=&project=&archived=       List flags (paginated)
GET    /api/flags/{key}                            Get flag details
PUT    /api/flags/{key}                            Update flag metadata
POST   /api/flags/{key}/archive                    Archive flag
POST   /api/flags/{key}/unarchive                  Unarchive flag
DELETE /api/flags/{key}                            Delete (must be archived)

Environment Configuration

PATCH  /api/flags/{key}/environments/{env}         Toggle / configure per-env
GET    /api/flags/{key}/environments/{env}         Get env state

Evaluation (SDK Endpoint)

POST   /api/evaluate/{environment}/{flagKey}       Evaluate single flag
POST   /api/evaluate/{environment}                 Evaluate all flags

Audit

GET    /api/flags/{key}/audit                      Flag audit history

🎯 Evaluation Logic

Request → Check flag exists?
        → Is flag archived?   → return false (FLAG_ARCHIVED)
        → Is flag enabled?    → return false (FLAG_DISABLED)
        → Evaluate targeting rules (priority order)
              → Rule match?   → return TARGETING_RULE_MATCH
        → Evaluate rollout %
              → Deterministic hash(userId + flagKey) % 100
              → In rollout?   → return ROLLOUT_MATCH
        → return DEFAULT_VALUE

📝 Example: Create & Configure a Flag

# 1. Create flag
curl -X POST http://localhost:8080/api/flags \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "new-checkout-flow",
    "name": "New Checkout Flow",
    "description": "Enables redesigned checkout experience",
    "flagType": "BOOLEAN",
    "tags": ["checkout", "ui"],
    "project": "storefront"
  }'

# 2. Enable in production with 20% rollout
curl -X PATCH http://localhost:8080/api/flags/new-checkout-flow/environments/production \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "enabled": true,
    "rolloutPercentage": 20,
    "comment": "Starting 20% rollout for new checkout"
  }'

# 3. Add targeting rule (always enable for beta users)
curl -X PATCH http://localhost:8080/api/flags/new-checkout-flow/environments/production \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "enabled": true,
    "rolloutPercentage": 20,
    "rules": [{
      "name": "Beta Users",
      "ruleType": "SEGMENT",
      "operator": "IN",
      "values": ["user-001", "user-002", "beta@company.com"],
      "priority": 1,
      "enabled": true
    }]
  }'

# 4. Evaluate (SDK call)
curl -X POST http://localhost:8080/api/evaluate/production/new-checkout-flow \
  -H "X-API-Key: ff_live_xxx" \
  -H "Content-Type: application/json" \
  -d '{
    "context": {
      "userId": "user-001",
      "email": "beta@company.com",
      "country": "US"
    }
  }'

⚙️ Configuration

Property Default Description
app.jwt.secret (required) HS256 signing key
app.jwt.expiration-ms 86400000 Access token TTL (24h)
app.jwt.refresh-expiration-ms 604800000 Refresh token TTL (7d)
spring.data.redis.host localhost Redis host
spring.datasource.url localhost:5432 PostgreSQL URL

📖 Interactive API Docs

Swagger UI: http://localhost:8080/api/swagger-ui.html


🗄️ Database Schema

users                 → Authentication and RBAC
environments          → Logical deployment environments
feature_flags         → Flag definitions
flag_environments     → Per-environment flag state (enabled, rollout %, variant)
targeting_rules       → User targeting rules per flag+environment
flag_audit_logs       → Immutable change history
flag_evaluations      → Optional evaluation tracking

📦 Tech Stack

  • Spring Boot 3.2 — Core framework
  • Spring Security — JWT + API Key authentication, RBAC
  • Spring Data JPA — Data access
  • PostgreSQL 16 — Primary database
  • Flyway — Database migrations
  • Redis — Evaluation result caching
  • Springdoc OpenAPI — Swagger UI documentation
  • Lombok — Boilerplate reduction
  • BCrypt — Password hashing

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages