Multithreaded Log Collection System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-04-02

Problem Statement

Design and implement a thread-safe log collection system that can handle logs from multiple sources concurrently, buffer them efficiently, and support real-time querying or persistence to storage. The system should guarantee ordering, fault tolerance, and scalability under high concurrency.


Functional Requirements


1.<span></span>Log Ingestion
2.<span></span>Multiple producer threads (representing applications/services) should be able to push logs into the system concurrently.
3.<span></span>Each log entry contains:
<div><div>sql</div><div>CopyEdit</div><div><timestamp, source_id, log_level, message></div></div>
1.<span></span>Buffering & Ordering
2.<span></span>Logs must be buffered in memory before being flushed to disk or external storage.
3.<span></span>Within a single source, logs must be written in order.
4.<span></span>Across multiple sources, system should preserve timestamp-based ordering (configurable).
5.<span></span>Persistence
6.<span></span>Logs should be flushed to:
7.<span></span>a file, or
8.<span></span>a database, or
9.<span></span>a queue system (like Kafka)
10.<span></span>Flushing can be periodic (e.g., every X ms) or batch-based (e.g., every N logs).
11.<span></span>Query API (Optional)
12.<span></span>getLogs(source_id, start_time, end_time) → retrieve logs for a given source in the time range.
13.<span></span>Must be thread-safe and work alongside ongoing ingestion.


Concurrency Requirements


1.<span></span>Support 100+ producer threads writing logs simultaneously.
2.<span></span>At least one consumer thread (or pool) handles batching and flushing to storage.
3.<span></span>Reads (queries) should not block writes.
4.<span></span>Avoid bottlenecks caused by global locks; use lock-free queues or fine-grained synchronization.


Constraints


1.<span></span>Log volume: up to 1 million logs/second.
2.<span></span>Each log entry ≤ 1 KB.
3.<span></span>Flush latency ≤ 1 second.
4.<span></span>System must tolerate producer spikes without dropping logs (unless backpressure is enabled).


Example Workflow


1.<span></span>Thread-1 (producer) → pushes
<div><div>bash</div><div>CopyEdit</div><div>[2025-08-18 16:00:01, source=A, INFO, "User login"]</div></div>
1.<span></span>Thread-2 (producer) → pushes
<div><div>bash</div><div>CopyEdit</div><div>[2025-08-18 16:00:02, source=B, ERROR, "Payment failed"]</div></div>
1.<span></span>Consumer thread → batches and writes to disk in timestamp order.
2.<span></span>Query thread → retrieves logs for source=A in the last 5 minutes.


Deliverables


1.<span></span>Multithreaded implementation with producer-consumer pattern.
2.<span></span>Configurable batching & flushing strategy.
3.<span></span>Thread-safe in-memory buffer (queue, ring buffer, or segment tree).
4.<span></span>Unit & stress tests with 100+ producer threads.
5.<span></span>Benchmark report on throughput (logs/sec) and latency.


Solution

Comments