System Architecture
FlexPrice uses a modern, scalable microservices architecture designed to handle high-volume usage metering and real-time billing calculations.
Technology Stack
FlexPrice is built on proven, production-ready technologies:Go 1.23+
High-performance backend written in Go for speed and reliability
Gin Framework
Fast HTTP web framework with middleware support
PostgreSQL
Transactional database for core business data
ClickHouse
Analytics database for high-volume event storage
Apache Kafka
Message queue for reliable event processing
Temporal
Workflow engine for long-running billing processes
Ent ORM
Entity framework for type-safe database operations
Uber FX
Dependency injection framework for clean architecture
Core Services
FlexPrice runs three main service types that can be deployed independently for scalability:- API Service
- Consumer Service
- Worker Service
- Local Mode
Deployment Mode: Docker Compose:
apiThe API service handles all HTTP requests:- Usage event ingestion
- Customer and plan management
- Invoice generation requests
- Feature entitlement checks
- Real-time usage queries
Infrastructure Components
PostgreSQL
Stores transactional data with strong consistency guarantees:- Customers, plans, subscriptions
- Pricing rules and configurations
- Invoices and payment records
- Credit grants and wallet balances
- Feature definitions and entitlements
ClickHouse
Handles high-volume analytics and event storage:- Raw usage events (millions per second)
- Aggregated usage metrics
- Time-series analysis
- Cost calculations
- Audit logs
Apache Kafka
Provides reliable, ordered event streaming: Topics:events- Raw event ingestion from APIevents_lazy- Deferred event processingevents_post_processing- Post-processing pipelinesystem_events- Internal system events and webhooks
Kafka UI is available at http://localhost:8084 with the
--profile dev flag.Temporal
Orchestrates long-running business processes:- Workflows: Multi-step billing processes
- Activities: Individual tasks (invoice generation, payment processing)
- Retry Logic: Automatic retry with exponential backoff
- Observability: Full execution history and debugging
Layered Architecture
FlexPrice follows clean architecture principles with clear separation of concerns:Domain Layer
Location:
internal/domain/Core business entities and domain models:- Pure business logic
- Repository interfaces (no implementations)
- No external dependencies
- Framework-agnostic
Repository Layer
Location:
internal/repository/Data access implementations:- Implements domain repository interfaces
- Direct database access (Ent, ClickHouse)
- Data mapping and persistence
- Query optimization
Service Layer
Location:
internal/service/Business logic orchestration:- Uses repository interfaces
- Transaction management
- Complex business rules
- Temporal workflow integration
Event Processing Flow
Understand how usage events flow through the system:Consumer Processing
Consumer service:
- Reads events from Kafka
- Validates event structure
- Enriches with customer/plan data
- Writes to ClickHouse
Metering & Analytics
- Real-time aggregation of usage metrics
- Feature usage tracking and limits
- Cost calculation based on pricing rules
- Alert triggers for thresholds
Project Structure
Dependency Injection
FlexPrice uses Uber FX for dependency injection:Multi-Tenancy Model
All data is isolated by tenant and environment:- Tenant
- Environment
Top-level isolation boundary representing a company or organization.Each tenant has:
- Separate API keys
- Independent data storage
- Isolated billing cycles
- Dedicated configurations
Scalability & Performance
Horizontal Scaling
Scale each service independently:
- Multiple API instances behind load balancer
- Consumer instances with Kafka consumer groups
- Worker instances for parallel workflow execution
High Throughput
Handle millions of events per second:
- Kafka buffering and batching
- ClickHouse columnar storage
- Async event processing
Real-Time Processing
Low-latency event to insight:
- Sub-second event ingestion
- Real-time usage aggregation
- Immediate feature limit checks
Reliability
Built-in resilience:
- Temporal automatic retries
- Kafka message persistence
- Database transaction safety
Integrations
FlexPrice integrates with your existing tools: Payment Providers:- Stripe
- Chargebee
- Razorpay
- HubSpot
- Salesforce (via API)
- QuickBooks
- NetSuite (via API)
Integration implementations are in
internal/integration/ using a factory pattern for provider instantiation.Configuration Management
Configuration is managed via Viper with multiple sources:Environment Variables
Override with environment variables (prefix:
FLEXPRICE_)Examples:FLEXPRICE_POSTGRES_HOSToverridespostgres.hostFLEXPRICE_KAFKA_BROKERSoverrideskafka.brokers
Monitoring & Observability
Structured Logging
JSON logs via
internal/logger for easy parsing and analysisTemporal UI
Complete workflow execution history, debugging, and manual triggers at http://localhost:8088
Kafka UI
Monitor topics, messages, and consumer lag at http://localhost:8084
Health Checks
Service health endpoints for load balancer integration
Security
API Key Authentication
All API requests require valid API key in
X-API-Key headerTenant Isolation
Automatic data scoping prevents cross-tenant data access
Database Security
Encrypted connections, credential management, prepared statements
Audit Logging
All operations logged with user context for compliance
Next Steps
API Reference
Explore all available endpoints and data models
Product Catalog
Set up plans, pricing, and features
Event Ingestion
Learn about event schemas and best practices
Contributing
Contribute to FlexPrice development