Kafka-RS follows Domain-Driven Design (DDD) principles to create a clear separation between business logic and technical implementation. This makes the codebase educational and easier to understand.
┌─────────────────────────────────────────────────────────┐
│ Client Layer │
│ (KafkaJS, kafka-node, or any Kafka client) │
└─────────────────────────┬───────────────────────────────┘
│ TCP/Kafka Protocol
┌─────────────────────────▼───────────────────────────────┐
│ Infrastructure Layer │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ TCP Server │ │ Protocol │ │
│ │ (Tokio) │ │ Handler │ │
│ └─────────────────┘ └─────────────────┘ │
└─────────────────────────┬───────────────────────────────┘
│
┌─────────────────────────▼───────────────────────────────┐
│ Application Layer │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Producer │ │ Consumer │ │
│ │ Service │ │ Service │ │
│ └─────────────────┘ └─────────────────┘ │
└─────────────────────────┬───────────────────────────────┘
│
┌─────────────────────────▼───────────────────────────────┐
│ Domain Layer │
│ ┌──────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ Topic │ │ Message │ │ Partition │ │
│ │ Entity │ │ Entity │ │ Entity │ │
│ └──────────┘ └──────────┘ └─────────────┘ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Domain │ │ Repository │ │
│ │ Services │ │ Interfaces │ │
│ └─────────────────┘ └─────────────────┘ │
└─────────────────────────┬───────────────────────────────┘
│
┌─────────────────────────▼───────────────────────────────┐
│ Infrastructure Layer (Storage) │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ In-Memory │ │ Offset │ │
│ │ Topic Store │ │ Manager │ │
│ └─────────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────┘
src/domain/
)Purpose: Contains the core business logic and rules
Components:
Topic
, Message
, Partition
, Consumer
, Producer
TopicName
, MessageId
, Offset
Key Principles:
src/application/
)Purpose: Orchestrates use cases and coordinates between layers
Components:
SendMessage
, ConsumeMessages
, CreateTopic
Responsibilities:
src/infrastructure/
)Purpose: Handles external concerns and technical implementation
Components:
External Dependencies:
A named channel for organizing messages. In Kafka-RS, topics are:
The fundamental unit of data with:
A subset of a topic that:
An entity that:
An entity that:
DashMap
and RwLock
DashMap<TopicName, Topic>
Vec<Message>
per partitionHashMap<ConsumerId, Offset>
We implement a subset of Kafka’s binary protocol:
Every operation logs detailed information:
log::info!("Received produce request for topic: {}", topic_name);
log::debug!("Storing message with offset: {}", offset);
log::info!("Consumer {} fetched {} messages", consumer_id, count);
Abstract storage behind interfaces:
trait TopicRepository {
async fn save_topic(&self, topic: Topic) -> Result<()>;
async fn find_by_name(&self, name: &TopicName) -> Option<Topic>;
}
Encapsulate business operations:
struct MessageService {
topic_repo: Arc<dyn TopicRepository>,
}
Complex object construction:
Message::builder()
.key("user-123")
.value("user data")
.timestamp(now())
.build()
Tests serve as documentation and examples of expected behavior.
This architecture supports adding:
The DDD structure makes these extensions clear about where new functionality belongs.