# swift-server/swift-kafka-client architecture

> Architecture diagram of the GitHub repository swift-server/swift-kafka-client, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/swift-server/swift-kafka-client
- Repository: https://github.com/swift-server/swift-kafka-client

## Overview

##### Project Type & Purpose
- This is an open-source Swift library that wraps the native librdkafka (a C/C++ library) to provide a clean, concurrent Swift API for interacting with Apache Kafka.
- Its primary purpose is to enable Swift applications to produce and consume messages from a Kafka cluster using modern Swift concurrency (async/await) and a service lifecycle pattern.

##### Key Components
a. Swift API Core:
- KafkaProducer and KafkaConsumer: Represent the primary entry points for sending and receiving messages.
- Configuration Modules: Components under Sources/Kafka/Configuration that deal with broker settings, consumer groups, security settings (plaintext, TLS, SASL, SASL+TLS).
- Core Service Management: The use of the Swift Service Lifecycle (Service and ServiceGroup) ensuring proper startup and shutdown.
- Utility Modules: Under Sources/Kafka/Utilities, which provide helper functions (e.g., async back-pressure strategies, task executors) to support the main components.

b. Low-Level Integration Components:
- RDKafka Layer: Modules in Sources/Kafka/RDKafka that encapsulate the interaction with librdkafka (RDKafkaClient, RDKafkaConfig, etc.).
- Crdkafka: Wrappers and custom configurations for the native librdkafka.
- COpenSSL: The module providing the interface with OpenSSL necessary for TLS/Security features.

c. External Dependencies & Environment:
- External Apache Kafka Broker: Representing the Kafka server(s) with which the client interacts.
- Third-Party Libraries: librdkafka (included via submodule) and OpenSSL, as they underpin the low-level C integrations.

##### Relationships & Interactions
- The configuration modules feed parameters (broker addresses, security protocols, topic configurations) into both KafkaProducer and KafkaConsumer, so represent these dependencies (e.g., configuration → KafkaProducer).

##### Architectural Patterns & Design Principles
- Layered Architecture: Show a clear separation between the high-level Swift API and the low-level native (C and C++) libraries.
- Concurrency Model: Emphasize usage of Swift’s new concurrency primitives (async/await, AsyncSequence) in the consumer and producer APIs.
- Service Pattern: Highlight the use of the Service protocol and ServiceGroup for managing lifecycle and graceful shutdown.

1. The Application Layer (Swift API):
2. The Integration Layer:
- To the Kafka native libraries (librdkafka and by extension Crdkafka).
- Next, represent external communication:

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Swift API Layer"
        direction TB
        A["KafkaConfiguration"]:::config
        B["Utility Modules"]:::utility
        C["Service Lifecycle Manager"]:::service
        D["KafkaProducer"]:::swift
        E["KafkaConsumer"]:::swift
    end

    subgraph "Integration Layer"
        direction TB
        F["RDKafka Client"]:::integration
        G["Crdkafka"]:::integrationDotted
        H["COpenSSL"]:::integrationDotted
    end

    I["Apache Kafka Broker"]:::external
    J["Docker Compose Environment"]:::docker

    A --> D
    A --> E
    B --> D
    B --> E
    C --> D
    C --> E
    D --> F
    E --> F
    F -->|"sends/receives"| I
    F --> G
    F --> H
    J --> I
    J --> C

    click D "https://github.com/swift-server/swift-kafka-client/blob/main/Sources/Kafka/KafkaProducer.swift"
    click E "https://github.com/swift-server/swift-kafka-client/blob/main/Sources/Kafka/KafkaConsumer.swift"
    click A "https://github.com/swift-server/swift-kafka-client/tree/main/Sources/Kafka/Configuration"
    click B "https://github.com/swift-server/swift-kafka-client/tree/main/Sources/Kafka/Utilities"
    click F "https://github.com/swift-server/swift-kafka-client/tree/main/Sources/Kafka/RDKafka"
    click G "https://github.com/swift-server/swift-kafka-client/tree/main/Sources/Crdkafka"
    click H "https://github.com/swift-server/swift-kafka-client/tree/main/Sources/COpenSSL"
    click J "https://github.com/swift-server/swift-kafka-client/tree/main/docker"

    classDef swift fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef config fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef utility fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef service fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef integration fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef integrationDotted fill:#e2e3e5,stroke:#6c757d,stroke-width:2px,stroke-dasharray: 5 5;
    classDef external fill:#fefefe,stroke:#818182,stroke-width:2px,stroke-dasharray: 3 3;
    classDef docker fill:#e0f7fa,stroke:#006064,stroke-width:2px;
```
