KatInterview

Design a Concurrent Low-Latency Logging Library

System DesignModel answer
MediumModel answerSystem Designobject-oriented-designconcurrencylow-level-design

Last updated

Architect an in-process, multi-threaded logging framework with non-blocking record hand-off, dynamic log filtering, pluggable formatters, and resilient appender management.

Design an in-process logging framework built for high-concurrency environments where many application threads record log messages simultaneously. The framework must decouple application thread processing from high-latency target destinations (such as physical disks or sockets), avoiding performance bottlenecks while maintaining predictable resource usage.

System Requirements

  1. Object Architecture: Model clean separations of concern between log record instantiation, level-based filtering, message formatting, and target destination writing.
  2. Fast Filtering: Perform log level checks with minimal overhead before any string formatting or object instantiation occurs on application caller threads.
  3. Asynchronous Hand-off & Concurrency: Pass log entries from producer threads to background consumer machinery via a thread-safe, bounded structure.
  4. Ordering & Overflow Policies: Maintain chronological log record ordering per producer thread. Define how the system behaves when the internal buffer reaches full capacity (e.g., dropping new events, dropping oldest events, or blocking callers).
  5. Fault Isolation & Graceful Shutdown: Prevent single appender failures (e.g., I/O errors, full disks) from deadlocking application threads or crashing the application process. Provide a flush and shutdown mechanism that processes pending events before terminating.

Provide a low-level object-oriented design and a production-grade code implementation in your language of choice (e.g., Java, C++, Python, or Go) covering the critical execution paths.

Hints

Hint 1

Guard record creation and string formatting behind an atomic level check to ensure disabled log levels cost virtually zero CPU cycles.

Hint 2

Use a bounded concurrent queue or lock-free ring buffer for caller hand-off to prevent unconstrained heap consumption during log bursts.

Hint 3

Isolate individual appender write logic within exception handling guards inside the background worker thread so one failing destination cannot crash the logging pipeline.