Design Google Calendar
hard 60 minEvents with a start and an end look like the simplest table there is.
Video lesson · Chapter 1 of 5
A month contains more than stored rows
Theory
Read or listen first. About 1 min.
What it is
A calendar service stores events, repeats some of them by a rule, invites guests, shares whole calendars with chosen rights, reminds people on time and keeps every device the same.
It is a database of promises about the future. The past never changes, but the future does: rules get exceptions, people move, and governments change the clocks.
Why it exists
People plan together across places and devices. A paper planner on one wall cannot tell a traveller that the meeting moved.
It is a good interview problem because storing and reading rows is easy, so the discussion moves to correctness: what a time means, what one change to a series means, and what happens when two people change the same thing.
A short history
1998
RFC 2445 defines iCalendar in November, including the text form of repeating rules.
2006
Google announces Google Calendar on its blog on 13 April.
2007
RFC 4791 defines CalDAV in March: reading and writing calendars over HTTP.
2009
RFC 5545 replaces the first iCalendar standard in September. Its rules are still used.
2012
RFC 6578 adds sync tokens to WebDAV collections in March, so a client can ask only for changes.
Words to know
- Series
- An event that repeats. It is stored as one rule.
- RRULE
- The standard text form of the rule, such as every Monday.
- Occurrence
- One date of a series.
- Exception
- An occurrence that was moved, changed or cancelled.
- Wall time
- What the clock on the wall shows, together with a time zone name.
- IANA zone
- A name such as Europe/London, with the full history of its clock changes.
- Free/busy
- Only when someone is occupied, not with what.
- Sync token
- A bookmark: give me everything that changed since here.
- ETag
- A version of a record, used to refuse an edit made on an old copy.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Save. Event and change number in one transaction
Understand it
Card, magnet, exception, copy, bell.
Scope
What are we building?
A calendar for web and phones. People create single, all-day, multi-day and repeating events, invite guests who answer, share calendars, see who is free, get reminders, and find old events. It works without a network and catches up later.
Repeat
A rule for ever, with exceptions.
Agree on time
The same meeting in every zone, through clock changes.
Work together
Guests, shared calendars, edits at the same moment.
Remind
On time, not twice, and never forgotten.
Similar products
| Product | Known for |
|---|---|
| Outlook Calendar | Work calendars with rooms |
| Apple Calendar | Built into its devices, speaks CalDAV |
| Calendly, Cal.com | Others book a free slot; Cal.com is open source |
| Doodle | A group votes on a time |
| Fantastical | Events typed as a sentence |
The hard part
Time is not a number
A meeting at 9:00 every Monday in New York is at 14:00 UTC in February and at 13:00 UTC in April. If we store UTC only, it moves by an hour in March. If we store every Monday as a row, the series never ends. If we store only the rule, a moved Monday has nowhere to live. The model has to hold all three.
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database.
The whole system
Where to read more
Ideas this problem borrows
| Idea | Its home | Used here for |
|---|---|---|
| Safe edits | Concurrency control | ETag and If-Match |
| All or nothing | ACID | Event and change number together |
| Split the data | Sharding | By calendar |
| Changes as a stream | Change data capture, log-based queues | Mail, sync, search |
| Run later | Task scheduler | Reminders |
| Sending | Notification service | Email and push |
| Live updates | Real-time APIs | WebSocket hints |
| Safe repeats | Idempotency | Create, reminders |
| Cache reads | Cache-aside | Month views |
| Find by words | Full-text search | Event search |
| Editing together | Google Docs | Why a calendar does not need it |
Interview tabs: approach, scope, numbers, API and data, design, deep dives and mock.
Summary
The whole topic in one minute.
Store what people meant: wall time with a zone name, and a rule with its exceptions. Work out instants and occurrences from that. UTC columns are an index that can be rebuilt when a government changes the clocks.
Everything else hangs on one ordered list of changes for each calendar. Devices ask for what is new since their token, notifications are sent from the same list, and reminders keep only the next date of each event, claimed with a lease so that none is lost.
- Store wall time and an IANA zone name. UTC is an index.
- A series is one rule and its exceptions. Expand on read, for the visible window only.
- An exception is found by the occurrence's original start, which never changes.
- Each calendar has a change number. Sync, mail and search all follow it.
- A live message is only a hint. The sync token is the truth.
- Keep only the next reminder of each event, claim it with a lease, send at least once.
- Reminders break the 3x rule: clocks synchronise people.
Sources
- RFC 5545: iCalendar
- RFC 4791: CalDAV
- RFC 6578: Collection synchronization
- Google Calendar API: synchronize resources
- Google Calendar API: push notifications
- Google Calendar API: recurring events
- IANA time zone database
- PostgreSQL: range types and exclusion constraints
- Redis: Pub/Sub delivery
- Google blog: It's about time (2006)