Architecture Diagram
info
Understanding the overall architecture helps you build better applications with Fluvius Framework.
High-Level Architecture
┌─────────────────────────────────────────────────────────────┐
│ Client Layer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Web App │ │ Mobile App │ │ API Client │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ API Layer (FastAPI) │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ REST Endpoints│ │ WebSockets │ │ Middleware │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Domain Layer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Domains │ │ Aggregates │ │ Commands │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Events │ │ State Manager │ │ Processors │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
┌───────────┴───────────┐
▼ ▼
┌──────────────────────────┐ ┌──────────────────────────┐
│ Event Store │ │ State Store │
│ ┌────────────────────┐ │ │ ┌────────────────────┐ │
│ │ Event Log │ │ │ │ Current State │ │
│ │ (Immutable) │ │ │ │ (Read-Optimized) │ │
│ └────────────────────┘ │ │ └────────────────────┘ │
└──────────────────────────┘ └──────────────────────────┘
│ │
└───────────┬───────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ Data Layer │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ PostgreSQL │ │ MongoDB │ │ SQLite │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
Component Flow
Command Flow
Client Request
│
▼
FastAPI Endpoint
│
▼
Domain.create_command()
│
▼
Command Processor
│
▼
Aggregate Action
│
├──► Business Logic
│
├──► State Update
│
└──► Event Generation
│
├──► Event Store (Write)
│
└──► State Store (Update)
│
└──► Response to Client
Query Flow
Client Request
│
▼
FastAPI Endpoint
│
▼
State Manager
│
├──► Query Builder
│
├──► State Store (Read)
│
└──► Response to Client
Layered Architecture
1. Presentation Layer
Components:
- FastAPI application
- REST endpoints
- WebSocket handlers
- Middleware
Responsibilities:
- Request/response handling
- Authentication
- Input validation
- Error handling
2. Domain Layer
Components:
- Domains
- Aggregates
- Commands
- Events
- State Manager
Responsibilities:
- Business logic
- State management
- Event generation
- Command processing
3. Infrastructure Layer
Components:
- Event Store
- State Store
- Database drivers
- Message queues
- File storage
Responsibilities:
- Data persistence
- Event storage
- External integrations
- Infrastructure concerns
Module Architecture
fluvius/
├── domain/ # Core domain logic
│ ├── aggregate.py
│ ├── command.py
│ ├── event.py
│ └── state.py
├── fastapi/ # FastAPI integration
│ ├── app.py
│ └── endpoints.py
├── data/ # Data layer
│ ├── driver.py
│ └── models.py
├── worker/ # Background workers
│ └── tasks.py
├── query/ # Query builder
│ └── builder.py
├── media/ # Media management
│ └── manager.py
└── casbin/ # Authorization
└── enforcer.py
Data Flow
Write Path (Command)
- Client sends HTTP POST/PUT/DELETE
- FastAPI validates request
- Domain creates command
- Command Processor routes to aggregate
- Aggregate executes business logic
- Event is generated
- Event Store persists event
- State Store updates current state
- Response returned to client
Read Path (Query)
- Client sends HTTP GET
- FastAPI validates request
- State Manager queries state store
- State Store returns current state
- Response returned to client
Event-Driven Architecture
Aggregate Action
│
└──► Event Generated
│
├──► Event Store
│
├──► State Store Update
│
├──► Event Handlers
│ │
│ ├──► Notification Service
│ ├──► Analytics Service
│ └──► External Systems
│
└──► Event Stream
│
└──► Event Consumers
Scalability Architecture
Horizontal Scaling
Load Balancer
│
┌─────────────────┼─────────────────┐
│ │ │
FastAPI 1 FastAPI 2 FastAPI 3
│ │ │
└─────────────────┼─────────────────┘
│
Shared Database
(PostgreSQL/MongoDB)
CQRS Scaling
Write Instances (Commands)
│
└──► Event Store
│
├──► Event Replication
│
└──► Read Replicas (Queries)
│
└──► State Store Replicas
Security Architecture
Client Request
│
▼
Authentication Middleware
│
▼
Authorization (Casbin)
│
├──► Policy Check
│
├──► Resource Check
│
└──► Domain Access
│
└──► Aggregate Action
Deployment Architecture
┌─────────────────────────────────────┐
│ Application Container │
│ ┌───────────────────────────────┐ │
│ │ FastAPI Application │ │
│ │ ┌─────────┐ ┌───────────┐ │ │
│ │ │ Domains │ │ Workers │ │ │
│ │ └─────────┘ └───────────┘ │ │
│ └───────────────────────────────┘ │
└─────────────────────────────────────┘
│
┌───────┼───────┐
│ │ │
┌───▼───┐ ┌─▼───┐ ┌─▼───┐
│ DB │ │Redis│ │ S3 │
└───────┘ └─────┘ └─────┘
Next Steps
- Learn about Core Components
- Explore Module Structure
- Check Design Patterns