Design a Log Storage System
🗂️ What is a log storage system? It is the part of a service that keeps a record of what happened and when. Every log has an id and the time it was written. Later, someone debugging an outage asks for "everything between 22:00 and 23:00 last night", or "everything from March", and the store returns the matching logs in time order.
This problem is small, but it rewards one good idea more than most. The question sounds like it needs date arithmetic: rounding times down to the hour, knowing how many days March has, and so on. It does not. With the right view of the timestamp, a query at any granularity becomes one plain range, and a range over sorted data is something every language already does well. The interview is about finding that view, then choosing the storage that makes ranges cheap.
Requirements
"Design a log storage system. Each log has an id and a timestamp. Users ask for all logs between two times, at a granularity they choose: year, month, day, hour, minute or second."
The word "granularity" carries most of the problem, so pin down exactly what it means before you design anything.
Clarifying Questions
You: What does a timestamp look like?
Interviewer: Year:Month:Day:Hour:Minute:Second, with every field zero padded, like 2017:01:01:23:59:59. You can assume it is always valid.
You: What exactly does the granularity do? If I ask for days, and the start is 23:59 on January 1st, does January 1st count?
Interviewer: Yes. The granularity says which fields matter. Everything finer than it is ignored, on the start, on the end, and on each log. So with Day, a start of 2017:01:01:23:59:59 means "from the beginning of January 1st".
You: Are both ends included?
Interviewer: Yes. With Day, an end on January 2nd includes all of January 2nd.
You: In what order should the results come back?
Interviewer: Earliest first. If two logs have the same timestamp, the smaller id first.
You: Do logs arrive in time order?
Interviewer: Not always. A server with a slow connection can send its logs late, so a new log may be older than ones we already have.
You: Can a log be changed or deleted, or the same id stored twice?
Interviewer: No. Ids are unique and logs are never changed.
You: How many logs, and how often do people query?
Interviewer: Keep it in memory. Think thousands to millions of logs, with queries coming in all the time while new logs keep arriving.
The answer about late logs is the one candidates skip, and it changes the storage. If logs always arrived in order, we could append each one to a list and the list would stay sorted for free. Because they do not, keeping the logs ordered is a real cost on every write, and we have to choose where to pay it.
💡 Interview tip: For anything with time in it, ask "can events arrive out of order?" It is one sentence, and the answer decides whether "append to the end" is a valid shortcut.
Final Requirements
put(id, timestamp)stores a log. Ids are unique; logs may arrive in any time order.- Timestamps have the fixed format
YYYY:MM:DD:HH:MM:SS, zero padded. retrieve(start, end, granularity)returns the ids of all logs whose timestamp is betweenstartandend, both included, comparing only the fields from the year down to the granularity.- The granularity is one of
Year,Month,Day,Hour,Minute,Second. - Results are ordered by timestamp, earliest first, then by id.
- If
startis afterendat that granularity, the result is empty. - Queries are frequent and the store is large, so a query should not have to look at every log.
Out of scope
- Log messages, levels and text search (we come back to levels in Extensibility).
- Deleting old logs, and saving logs to disk (also in Extensibility).
- Time zones, and granularities that do not line up with the fields, such as weeks.
- Spreading logs across many machines. That is the system design version; see /system-design/distributed-logging.