- Purpose: Define wire protocol constants and API keys
- Features:
- Custom binary protocol format
- API keys for different request types (PRODUCE, FETCH, METADATA, etc.)
- Protocol versioning
- Maximum message size limit (10MB)
Protocol Format:
┌─────────────┬─────────────┬─────────────┬──────────────┐
│ Size (4B) │ API Key (2B)│ Version (2B)│ Payload │
└─────────────┴─────────────┴─────────────┴──────────────┘
Implemented request types:
- ProduceRequest: Send messages to a topic partition
- FetchRequest: Retrieve messages from a topic partition
- MetadataRequest: Get topic metadata (partition count)
- OffsetCommitRequest: Commit consumer group offset
- HeartbeatRequest: Consumer heartbeat
Features:
- Efficient binary serialization
- Length-prefixed strings
- Support for batch operations
Implemented response types:
- ProduceResponse: Returns offset of appended messages
- FetchResponse: Returns list of messages
- MetadataResponse: Returns topic information
- OffsetCommitResponse: Confirms offset commit
- HeartbeatResponse: Confirms heartbeat
Features:
- Error code support
- Efficient deserialization
- Consistent format
- ProtocolDecoder: Decodes incoming bytes into Request objects
- Frame-based decoding
- Validates message size
- Handles partial messages
- ProtocolEncoder: Encodes Response objects into bytes
- Efficient binary encoding
- Header generation
- Purpose: Process incoming requests and generate responses
- Features:
- Routes requests based on API key
- Integrates with TopicManager and OffsetManager
- Error handling and logging
- Connection lifecycle management
- Purpose: Track consumer group offset commits
- Features:
- In-memory offset storage with ConcurrentHashMap
- Persistent storage to disk (Java serialization)
- Per-group, per-topic, per-partition tracking
- Automatic loading on startup
- Purpose: Netty-based TCP server
- Features:
- Non-blocking I/O with Netty NIO
- Configurable thread pools (boss + worker groups)
- Channel pipeline with codec and handler
- Graceful shutdown
- SO_KEEPALIVE and TCP_NODELAY options
- Purpose: Main entry point for the broker
- Features:
- Component initialization
- Configuration parsing from command-line args
- Shutdown hook for graceful termination
- Integration of all broker components
- Purpose: Client-side network communication
- Features:
- Netty-based async I/O
- Request/response correlation
- Connection management
- Timeout support (5 seconds default)
- Automatic reconnection handling
- Purpose: Send messages to topics
- Features:
- Simple API for sending messages
- Hash-based partitioning
- Batch send support
- Async send with CompletableFuture
- RecordMetadata with topic, partition, offset
- Purpose: Read messages from topics
- Features:
- Subscribe to specific partitions
- Poll-based message consumption
- Manual offset commits
- Offset tracking (current vs committed)
- Seek to specific offset
- Consumer group support
- Purpose: Determine partition for messages
- Features:
- Consistent hash-based partitioning
- Round-robin for null keys
- Same key always routes to same partition
┌─────────────┐ ┌─────────────────────┐
│ Producer │────── TCP ─────────▶│ Broker Server │
│ Client │ │ (Port 9092) │
└─────────────┘ │ │
│ ┌────────────────┐ │
┌─────────────┐ │ │ RequestHandler │ │
│ Consumer │◀───── TCP ─────────▶│ └────────────────┘ │
│ Client │ │ │ │
└─────────────┘ │ ▼ │
│ ┌────────────────┐ │
│ │ TopicManager │ │
│ │ OffsetManager │ │
│ └────────────────┘ │
└─────────────────────┘
Use the QuickStart example:
# Terminal 1: Start broker
cd broker
java -cp target/classes com.streamflow.broker.BrokerApplication
# Terminal 2: Run example
cd client
java -cp target/classes com.streamflow.client.example.QuickStart- ✅ Producer connects and sends messages
- ✅ Consumer connects and fetches messages
- ✅ Offset commit and retrieval
- ✅ Multiple partitions handling
- ✅ Error handling (invalid partition, topic not found)
- Custom Binary Protocol: Efficient wire format with minimal overhead
- Netty-based NIO: High-performance async networking
- Producer-Consumer Pattern: Clean separation of concerns
- Offset Management: Persistent offset tracking for consumers
- Partitioning: Hash-based consistent partitioning
- Error Handling: Comprehensive error codes and messages
- Connection Management: Automatic handling of connection lifecycle
0- PRODUCE: Send messages1- FETCH: Retrieve messages3- METADATA: Get topic info8- OFFSET_COMMIT: Commit consumer offset12- HEARTBEAT: Consumer heartbeat
0- No error1- Topic not found2- Invalid partition3- Offset out of range4- Invalid message
Phase 3 will implement Consumer Groups & Coordination:
- Consumer group coordinator
- Partition assignment strategies (Round-robin, Range)
- Rebalancing protocol
- Consumer group management
- Heartbeat processing
- Netty 4.1: NIO framework for network layer
- SLF4J + Logback: Logging
- Lombok: Reduce boilerplate
- JUnit 5: Testing
- Binary Protocol Design: Custom wire format optimization
- Netty Pipeline: Codec pattern for encoding/decoding
- Async I/O: Non-blocking network operations
- Request-Response Pattern: Client-server communication
- Connection Pooling: Reusing TCP connections
- Offset Management: Consumer progress tracking
- Partitioning Strategy: Load distribution across partitions
- Latency: Sub-10ms for produce/fetch operations
- Throughput: Thousands of messages per second
- Concurrency: Multiple producers/consumers simultaneously
- Connection Limit: Configurable thread pools
- Message Size: Up to 10MB per message
Broker startup options:
--broker-id <id> # Broker identifier
--host <hostname> # Bind address (default: localhost)
--port <port> # Listen port (default: 9092)
--data-dir <path> # Data storage directory
--partitions <n> # Default partitions per topic