Backend Architecture
Overview
The Rhesis backend is built with FastAPI, a modern Python web framework designed for building APIs with automatic OpenAPI documentation. The application follows a modular architecture with clear separation of concerns.
Tech Stack
- Framework: FastAPI
- Database: PostgreSQL with SQLAlchemy ORM
- Authentication: Auth0
- Task Queue: Celery with Redis broker
- API Documentation: Swagger UI (via FastAPI)
- Package Management: UV (Python package installer)
Directory Structure
Architectural Patterns
1. Model-View-Controller (MVC)
The backend follows an MVC-like pattern:
- Models: SQLAlchemy models in
app/models/
- Views: FastAPI route handlers in
app/routers/
- Controllers: Business logic in
app/services/
and CRUD operations inapp/crud.py
2. Repository Pattern
The CRUD operations are abstracted into a repository layer, allowing for clean separation between database access and business logic.
3. Dependency Injection
FastAPI’s dependency injection system is used extensively for:
- Database session management
- Authentication and authorization
- Feature toggles and configuration
Key Components
FastAPI Application
The main application is defined in app/main.py
, which:
- Creates the FastAPI application
- Configures middleware (CORS, authentication)
- Registers all routers
- Sets up exception handlers
Database Layer
The database layer consists of:
- Connection management in
app/database.py
- SQLAlchemy models in
app/models/
- Pydantic schemas for request/response validation in
app/schemas/
API Routers
API endpoints are organized into routers by resource type, with each router handling a specific domain entity (tests, prompts, users, etc.).
Background Tasks
Long-running operations are handled by Celery tasks defined in the tasks/
directory, which are executed asynchronously.
Authentication
Authentication is handled through Auth0 integration, with custom middleware to enforce different authentication requirements based on route configuration.
Communication Flow
- Client sends request to API endpoint
- FastAPI routes the request to the appropriate handler
- Authentication middleware validates the request
- Dependencies are resolved (database session, current user)
- Request is validated using Pydantic schemas
- Business logic is executed (via services or direct CRUD operations)
- Response is generated and returned to the client
Multi-tenancy
The application implements multi-tenancy through PostgreSQL row-level security, ensuring data isolation between different organizations.
Scalability Considerations
- Database connection pooling
- Stateless API design for horizontal scaling
- Background task processing with Celery
- Caching strategies for frequently accessed data