# airtai/faststream architecture

> Architecture diagram of the GitHub repository airtai/faststream, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/airtai/faststream
- Repository: https://github.com/airtai/faststream

## Overview

The FastStream project is a sophisticated event streaming framework built using Python, intended to simplify the integration with various messaging systems such as Kafka, RabbitMQ, NATS, and Redis. Its purpose is to enable developers to write producers and consumers with ease through a unified API, leveraging features such as automatic message parsing, validation (using Pydantic), dependency injection, and automatic documentation (via the AsyncAPI specification).

##### Main Components
- Core FastStream Engine – This is the heart of FastStream. It orchestrates the publishing and subscribing of messages. It contains the common modules such as CLI interfaces, dependency injection systems, core utilities, and general application lifecycles.
- Broker Abstraction Layer – This module abstracts differences between the various supported brokers. Separate components exist for Kafka, RabbitMQ, NATS, and Redis. Each broker’s package (e.g., faststream/kafka, faststream/rabbit, etc.) provides its own implementation of publishers, subscribers, routers, error handling, as well as middleware integration.
- Integration Modules – FastStream is designed to be integrated into HTTP frameworks like FastAPI or even work standalone as messaging middleware. Components include specific routers and adapters (e.g., fastapi integrations, asgi layers).
- Serialization & Validation – Built primarily on top of Pydantic (with support for both Pydantic V2 and V1) for message schemas, this component ensures that incoming messages are correctly serialized/deserialized and validated.
- Developer Tools & Documentation – Automatic AsyncAPI documentation generation, CLI tooling for starting the application, reloading, scaling (workers, multiprocess, etc.) and testing utilities (TestBroker / in-memory testing support).
- Dependency Injection System – This subsystem (leveraging ideas from FastDepends) provides the services’ dependencies (like custom context, logger, connectivity configuration, etc.) using annotated function parameters.

##### Relationships and Interactions
- The Broker Abstraction layer acts as an intermediary: there are distinct modules for Kafka, Rabbit, NATS, and Redis. These modules interact with external message queue systems.
- The Dependency Injection system has a flow from “dependency declarations” in handler signatures to the central DI container that injects concrete dependencies at runtime.
- The Testing utilities provide an in-memory or simulated broker context. Represent these by a separate testing container that “overrides” the production broker components.

##### Architectural Patterns and Design Principles
- Modular and pluggable design – The clear separation into distinct broker modules and integration layers shows adherence to the ‘plug-in’ pattern, which minimizes coupling.
- Abstraction and encapsulation – The project abstracts connection details to various brokers through unified interfaces.
- Declarative and dependency-driven design – The use of type annotations, Pydantic models, and dependency injection is modeled similar to frameworks like FastAPI.
- Scalability and lifecycle management – The CLI interface supports features like hot reload and worker scaling, which is essential for cloud-native microservices.
- Testing and in-memory simulation – An integrated approach for testing ensures the system can swap out live broker interactions for in-memory test environments.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User and Pre-processing Layer
    userHandler["User-defined Handler"]:::user
    di["Dependency Injection"]:::di
    asyncapi["Serialization & Validation"]:::validation

    userHandler -->|"decorated with DI"| di
    userHandler -->|"validated input"| asyncapi

    %% Core FastStream Engine Subgraph
    subgraph "Core FastStream Engine"
        coreApp["Application Entry"]:::core
        coreInternal["Internal Application"]:::core
        coreApp -->|"initializes"| coreInternal
    end
    di -->|"injects dependencies"| coreApp
    asyncapi -->|"passes validated message"| coreApp

    %% Broker Abstraction Layer Subgraph
    subgraph "Broker Abstraction Layer"
        kafka["Kafka Adapter"]:::broker
        rabbit["RabbitMQ Adapter"]:::broker
        nats["NATS Adapter"]:::broker
        redis["Redis Adapter"]:::broker
    end
    coreInternal -->|"publish/subscribe"| kafka
    coreInternal -->|"publish/subscribe"| rabbit
    coreInternal -->|"publish/subscribe"| nats
    coreInternal -->|"publish/subscribe"| redis

    %% Integration Modules Subgraph
    subgraph "Integration & Adapters"
        asgi["ASGI Integration"]:::integration
        cliIntegration["CLI Integration"]:::integration
    end
    asgi -->|"routes web requests"| coreApp
    cliIntegration -->|"triggers lifecycle commands"| coreApp

    %% Developer Tools & Documentation Subgraph
    subgraph "Developer Tools & Documentation"
        cliTools["CLI Tools"]:::tools
        docs["Documentation"]:::tools
        testing["Testing Utilities"]:::tools
    end
    cliTools -->|"manages app lifecycle"| coreApp
    docs -->|"generates AsyncAPI docs"| coreApp
    testing ---|"overrides broker in testing mode"| kafka
    testing ---|"overrides broker in testing mode"| rabbit
    testing ---|"overrides broker in testing mode"| nats
    testing ---|"overrides broker in testing mode"| redis

    %% Click Events - Component Mappings
    click di "https://github.com/airtai/faststream/blob/main/faststream/annotations.py"
    click asyncapi "https://github.com/airtai/faststream/tree/main/faststream/asyncapi"
    click coreApp "https://github.com/airtai/faststream/blob/main/faststream/app.py"
    click coreInternal "https://github.com/airtai/faststream/blob/main/faststream/_internal/application.py"
    click kafka "https://github.com/airtai/faststream/tree/main/faststream/kafka"
    click rabbit "https://github.com/airtai/faststream/tree/main/faststream/rabbit"
    click nats "https://github.com/airtai/faststream/tree/main/faststream/nats"
    click redis "https://github.com/airtai/faststream/tree/main/faststream/redis"
    click asgi "https://github.com/airtai/faststream/tree/main/faststream/asgi"
    click cliIntegration "https://github.com/airtai/faststream/tree/main/faststream/cli"
    click cliTools "https://github.com/airtai/faststream/tree/main/faststream/cli"
    click docs "https://github.com/airtai/faststream/tree/main/docs/"

    %% Style Definitions
    classDef user fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef di fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef validation fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef core fill:#ffefd5,stroke:#333,stroke-width:2px;
    classDef broker fill:#d1ecf1,stroke:#333,stroke-width:2px;
    classDef integration fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef tools fill:#e2e3e5,stroke:#333,stroke-width:2px;
```
