Log Aggregation System Design (HLD & LLD)
A Log Aggregation System collects logs from multiple services and servers, stores them centrally, and enables searching, monitoring, visualization, and alerting. It is a core component of modern distributed systems and microservices architectures.
Typical implementations include:
- ELK Stack (Elasticsearch + Logstash + Kibana)
- Splunk
- Datadog
- Grafana Loki
Functional Requirements
Section titled “Functional Requirements”- Applications send logs to the system.
- Aggregate logs from multiple sources.
- Support log search.
- Filter by:
- Service
- Timestamp
- Severity
- Dashboards and visualization.
- Alerting based on log patterns.
Non-Functional Requirements
Section titled “Non-Functional Requirements”- High throughput (millions of logs/sec)
- Fault tolerance
- Near real-time search
- Scalable storage
- Durable persistence
High-Level Design
Section titled “High-Level Design”Components
Section titled “Components”- Log Producers (Applications / Servers)
- Log Agents
- Log Ingestion Layer
- Message Queue / Buffer
- Log Processing
- Storage
- Query Service
- Visualization Dashboard
Architecture
Section titled “Architecture”flowchart TD
A[Application Servers] --> B["Log Agents<br/>Fluentd/Filebeat"]
B --> C[Log Ingestion API]
C --> D[Kafka]
D --> E[Log Processing Service]
E --> F["Storage<br/>Elasticsearch / S3"]
F --> G[Query API]
G --> H["Dashboard<br/>Kibana / Grafana"]
Component Details
Section titled “Component Details”Log Producers
Section titled “Log Producers”Applications generate logs.
INFO 2026-03-16 Payment processedERROR 2026-03-16 Payment failedLog Agents
Section titled “Log Agents”Examples:
- Fluentd
- Logstash
- Filebeat
Responsibilities:
- Read log files
- Batch logs
- Forward to ingestion service
Log Ingestion Service
Section titled “Log Ingestion Service”Receives logs via REST.
POST /logsResponsibilities:
- Validate
- Enrich
- Publish to Kafka
Decouples ingestion from processing.
Benefits:
- Handles spikes
- Prevents log loss
- Multiple consumers
Topic:
logs-topicPartition strategy:
service-nameLog Processing
Section titled “Log Processing”Responsibilities:
- Parse
- Extract fields
- Enrich metadata
- Normalize
Raw log:
ERROR Payment failed for user 123Structured log:
{ "timestamp": "...", "service": "payment-service", "level": "ERROR", "userId": 123, "message": "Payment failed"}Storage
Section titled “Storage”Hot Storage
Section titled “Hot Storage”- Elasticsearch
- OpenSearch
Recent searchable logs (7-30 days).
Cold Storage
Section titled “Cold Storage”- S3
- HDFS
Long-term archival.
Query Service
Section titled “Query Service”GET /logs?service=payment&level=ERRORGET /logs?startTime=...&endTime=...Dashboard
Section titled “Dashboard”Examples:
- Kibana
- Grafana
Capabilities:
- Search
- Dashboards
- Error monitoring
Scaling
Section titled “Scaling”Horizontal Scaling
Section titled “Horizontal Scaling”Scale independently:
- Agents
- Kafka partitions
- Processing workers
- Elasticsearch nodes
Kafka Partitioning
Section titled “Kafka Partitioning”service-nameor
timestampBatch Processing
Section titled “Batch Processing”batch size = 500 logsImproves throughput.
Data Model
Section titled “Data Model”Fields:
LogEntry---------logIdtimestampserviceNamehostlogLevelmessagetraceIdspanIdmetadataExample:
{ "timestamp": "2026-03-16T10:30:00", "service": "order-service", "level": "ERROR", "host": "server-1", "traceId": "abc123", "message": "Order failed"}Low-Level Design
Section titled “Low-Level Design”LogEntry
Section titled “LogEntry”class LogEntry {
String id; String serviceName; String host; String level; String message; long timestamp; String traceId;}Controller
Section titled “Controller”@RestControllerpublic class LogController {
@PostMapping("/logs") public void ingest(@RequestBody List<LogEntry> logs){ logService.publish(logs); }}Service
Section titled “Service”@Servicepublic class LogService {
@Autowired KafkaProducer producer;
public void publish(List<LogEntry> logs){ producer.send("logs-topic", logs); }}Kafka Consumer
Section titled “Kafka Consumer”@KafkaListener(topics="logs-topic")public void process(List<LogEntry> logs){ logProcessor.process(logs);}Processor
Section titled “Processor”public void process(LogEntry log){ logRepository.save(log);}Repository
Section titled “Repository”public interface LogRepository {
void save(LogEntry log);
List<LogEntry> search(Query query);}Elasticsearch Indexing
Section titled “Elasticsearch Indexing”Daily indices:
logs-2026-03-16logs-2026-03-17Benefits:
- Faster search
- Easier retention cleanup
Handling Massive Volume
Section titled “Handling Massive Volume”Sampling
Section titled “Sampling”1 of every 10 INFO logsPrioritize Log Levels
Section titled “Prioritize Log Levels”ERROR > WARN > INFO > DEBUGCompression
Section titled “Compression”Compress before storage.
Failure Handling
Section titled “Failure Handling”Agent Failure
Section titled “Agent Failure”Buffer locally.
Kafka Failure
Section titled “Kafka Failure”RF = 3Elasticsearch Failure
Section titled “Elasticsearch Failure”Deploy replicas.
Alerting
Section titled “Alerting”Example rule:
If ERROR logs > 1000 in 5 minutesTrigger alertCommon tools:
- Prometheus
- Alertmanager
Example Production Pipeline
Section titled “Example Production Pipeline”flowchart LR
A[Applications] --> B[Filebeat]
B --> C[Kafka]
C --> D[Logstash]
D --> E[Elasticsearch]
E --> F[Kibana]
Common Interview Questions
Section titled “Common Interview Questions”- How do you prevent log loss?
- How do you support 10 million logs/sec?
- How do you implement efficient log search?
- Difference between logs, metrics, and traces?
- How do you correlate logs across microservices?
Key Takeaways
Section titled “Key Takeaways”- Kafka provides durable buffering and decouples producers from consumers.
- Elasticsearch/OpenSearch is optimized for recent searchable logs, while S3/HDFS stores long-term archives.
- Daily index partitioning simplifies retention and improves query performance.
- Horizontal scaling across ingestion, processing, messaging, and storage enables very high throughput.
- Structured logs with trace IDs simplify distributed debugging.