AlgoViz

Design Google Calendar

hard 60 min

Events 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

0:00 / 3:56

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

  1. 1998

    RFC 2445 defines iCalendar in November, including the text form of repeating rules.

  2. 2006

    Google announces Google Calendar on its blog on 13 April.

  3. 2007

    RFC 4791 defines CalDAV in March: reading and writing calendars over HTTP.

  4. 2009

    RFC 5545 replaces the first iCalendar standard in September. Its rules are still used.

  5. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Save. Event and change number in one transaction

Understand it

The wall planner in the flat
MTWTFSSOne planner for everybody

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

ProductKnown for
Outlook CalendarWork calendars with rooms
Apple CalendarBuilt into its devices, speaks CalDAV
Calendly, Cal.comOthers book a free slot; Cal.com is open source
DoodleA group votes on a time
FantasticalEvents typed as a sentence
We design a service like Google Calendar, not Google's real system.

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

Users reach an API gateway and the calendar service. The service reads month views from Redis and events from PostgreSQL, split by calendar and copied to a second region. Changes flow from PostgreSQL to Kafka. Notifiers read Kafka and send email and push. Sync servers read Kafka and tell connected users. A search index is filled from Kafka. A reminder planner reads due reminders from PostgreSQL and puts them in Kafka.
Blue is reading and saving, pink is what follows a change, orange is reminders. Dashed means later.

Where to read more

Ideas this problem borrows

IdeaIts homeUsed here for
Safe editsConcurrency controlETag and If-Match
All or nothingACIDEvent and change number together
Split the dataShardingBy calendar
Changes as a streamChange data capture, log-based queuesMail, sync, search
Run laterTask schedulerReminders
SendingNotification serviceEmail and push
Live updatesReal-time APIsWebSocket hints
Safe repeatsIdempotencyCreate, reminders
Cache readsCache-asideMonth views
Find by wordsFull-text searchEvent search
Editing togetherGoogle DocsWhy a calendar does not need it

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