Log collection and aggregation system with a bounded buffer, backpressure and indexed queries
Company: Cloudflare
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a log collection and aggregation system that collects logs from multiple edge data centers and transports them to a core storage layer. The system must handle high-volume data streams and implement backpressure by rejecting new logs when the internal buffer reaches capacity.
Logs have a `timestamp`, a `source` identifier (representing the edge data center), a `severity` level (`INFO`, `WARN`, or `ERROR`), and a `message` string. Logs are first buffered, then explicitly flushed to permanent storage. Only flushed logs are queryable.
The system supports querying stored logs by source, severity, or time range, and provides aggregation statistics including total collected, total flushed, dropped counts, and per-severity breakdowns.
Implement a `LogAggregator` class with the following methods:
- `LogAggregator(buffer_capacity: int)`: create an aggregator whose buffer holds at most `buffer_capacity` logs.
- `collect(timestamp: int, source: str, severity: str, message: str) -> bool`: collect a log entry into the buffer. Returns `True` if accepted, `False` if the buffer is full (backpressure). Dropped logs increment the dropped counter.
- `flush() -> int`: flush all buffered logs to permanent storage. Returns the number of logs flushed. Clears the buffer.
- `query_by_source(source: str) -> List[str]`: return all stored log messages from a given source, in insertion order.
- `query_by_severity(severity: str) -> List[str]`: return all stored log messages with a given severity, in insertion order.
- `query_by_time_range(start: int, end: int) -> List[str]`: return all stored log messages with timestamp in `[start, end]` inclusive, in insertion order.
- `get_buffer_size() -> int`: return the current number of logs in the buffer (not yet flushed).
- `get_storage_size() -> int`: return the total number of logs in permanent storage.
- `get_stats() -> List[int]`: return `[total_collected, total_flushed, dropped_count]`. `total_collected` counts logs accepted into the buffer; a rejected log counts only toward `dropped_count`.
- `get_severity_counts() -> List[int]`: return `[INFO_count, WARN_count, ERROR_count]` from permanent storage.
Example:
```text
LogAggregator(3)
collect(100, "edge-1", "INFO", "cache warm") -> True
collect(105, "edge-2", "ERROR", "upstream timeout") -> True
collect(102, "edge-1", "WARN", "high latency") -> True
collect(110, "edge-2", "INFO", "health ok") -> False
get_buffer_size() -> 3
query_by_source("edge-1") -> []
flush() -> 3
collect(110, "edge-2", "INFO", "health ok") -> True
query_by_source("edge-1") -> ["cache warm", "high latency"]
query_by_severity("INFO") -> ["cache warm"]
query_by_time_range(101, 110) -> ["upstream timeout", "high latency"]
get_buffer_size() -> 1
get_storage_size() -> 3
get_stats() -> [4, 3, 1]
get_severity_counts() -> [1, 1, 1]
```
- The fourth `collect` is rejected because the buffer already holds 3 logs, so the dropped count becomes 1.
- Before the flush, nothing is queryable, so `query_by_source("edge-1")` returns an empty list.
- After the flush the caller retries "health ok" and it is accepted, but it stays in the buffer. It does not appear in `query_by_severity("INFO")`, in the time-range result, or in the severity counts.
- The time-range result lists "upstream timeout" (timestamp 105) before "high latency" (timestamp 102) because results follow insertion order.
- `get_stats()` is `[4, 3, 1]`: 4 accepted logs, 3 flushed, 1 dropped.
```hint Answering queries
Source and severity queries are exact-match lookups that must return messages in insertion order. Think about what you could build during `flush` so these queries never touch unrelated logs.
```
```hint Counter semantics
Write down which counters each event changes: an accepted log, a rejected log, and a flush. Buffered logs must stay invisible to queries and to the severity counts until they are flushed.
```
### Constraints & Assumptions
- Buffer capacity is from 1 to 10,000.
- Severity is one of `"INFO"`, `"WARN"`, `"ERROR"`.
- Source identifiers are non-empty strings.
- Timestamps are integers from 0 to 1,000,000,000.
- At most 10,000 calls in total across all methods.
### Clarifying Questions to Ask
- Can logs arrive out of timestamp order, for example when one edge's batch is delayed behind another's?
- Is dropping final, or is the caller expected to retry a rejected log after the next flush?
- Should query results be copies, or may callers share the aggregator's internal lists?
- Is `flush` only called by the client, or should the system also flush on its own when the buffer fills?
### What a Strong Answer Covers
- A clear split between the bounded buffer and permanent storage, with backpressure applied in `collect` and only flushed logs visible to queries and severity counts.
- Indexes by source and severity built during `flush` that preserve insertion order.
- Running counters with exact semantics for accepted, flushed and dropped logs.
- The time-range query, its real cost, and a faster structure if that query becomes hot.
- Time complexity per method and memory use, stated correctly.
### Follow-up Questions
- Many edge threads call `collect` while a background worker calls `flush`. How do you make both safe without serializing everything behind one slow flush?
- Flushed logs no longer fit in memory. How would you persist them and keep the source, severity and time lookups fast?
- How would you replace plain dropping with better backpressure (blocking with a timeout, a retry-after signal, per-source quotas) so one noisy edge cannot fill the buffer for everyone?
- How would you support queries that combine filters, such as `ERROR` logs from one source within a time window?
Overview: Implement a log aggregator that buffers logs from edge data centers, rejects new logs when the buffer is full, flushes them to storage, and answers queries by source, severity and time range. It tests low-level design, exact counter semantics, indexing at write time and per-method complexity.