Build a Time-Zone-Safe Scheduling Backend for Recurring Events
System DesignModel answerLast updated
Design a scheduling service where users in different time zones create, edit and query one-off and repeating events, with correct behavior across daylight saving changes.
You are asked to design the backend for a scheduling product used by people all over the world. Users can have events that happen once or repeat, and they view their agenda in whatever time zone they are currently in.
The service must support:
- Creating, modifying, removing and reading events.
- Single events as well as repeating ones (daily, weekly, monthly, with an end date or count).
- Fetching one user's agenda for an arbitrary time window.
- Keeping events correct when the people involved live in different zones.
- Behaving sensibly when clocks shift for daylight saving time (DST).
In your answer, cover:
- Assumptions about scale and the main non-functional goals.
- The API surface, including how a single occurrence of a repeating event is addressed and edited.
- What you persist for each kind of event, and how repeating events are represented.
- Time zone handling, with particular attention to a repeating event whose organizer is in a zone that observes DST. What do the organizer and a remote attendee each see before and after a clock change? What happens to an occurrence that lands in a skipped or repeated local hour?
- Storage layout, partitioning, and whether occurrences are computed at read time, precomputed, or both.
- How you detect scheduling conflicts for people and for shared resources such as meeting rooms.
- Scaling, caching, and the consistency model, including concurrent edits.
Hints
Hint 1
Separate what the user meant from the instant it maps to. A repeating 'every Monday at 9:00 in the organizer's zone' is a local wall-clock rule plus a zone ID, not a fixed UTC time.
Hint 2
Think about the three kinds of time values: absolute instants (one-off events), local time plus zone (repeating events), and floating dates (all-day events). Never store a bare numeric offset in place of a zone ID.
Hint 3
Infinite series cannot be fully precomputed, but pure on-read expansion is costly at high read volume. Consider a rolling materialized window per attendee, sharded by user, with fallback expansion outside it.
Hint 4
Treat people conflicts as warnings and room conflicts as hard constraints that need a transactional or uniqueness guarantee on the room's shard.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Requirements and scale
Functional: event CRUD (title, time, attendees, location), one-off, all-day and repeating events, edits scoped to "this occurrence", "this and following" or "whole series", agenda queries rendered in the viewer's zone, and free/busy checks for people and rooms.
Non-functional: correctness across zones and DST is the central goal. Reads dominate, so agenda views should hit p99 under about 200 ms. Reads should stay highly available, but room booking must never double-book.
Rough numbers (state them aloud): 100M daily active users with about 20 agenda views each is 2B reads/day, roughly 25K QPS average and about 75K at peak. With about 2 writes per user per day we get about 2.5K writes/s. Reads outnumber writes by 10-30x, so optimize reads. Storage, including materialized occurrences, is in the tens of TB.
2. API
POST /calendars/{id}/events
GET /events/{eventId} # series definition
PATCH /events/{eventId}?scope=this|following|all&recurrenceId=...
DELETE /events/{eventId}?scope=...&recurrenceId=...
GET /users/{uid}/schedule?start=&end=&displayTz=...
POST /freebusy {userIds|roomIds, start, end}
recurrenceIdis the occurrence's original local start, so it remains a stable key even if that occurrence is later moved.- Use ETags with
If-Matchfor optimistic concurrency. - Clients send instants in UTC (or ISO-8601 with offset) and recurring rules with an IANA zone ID such as
America/Los_Angeles.
3. Time representation
Principle: store intent, derive instants.
| Kind | Stored | Reason |
|---|---|---|
| One-off timed | UTC instant + original zone ID | The instant is authoritative; the zone is for display and editing |
| Repeating timed | Local start time, duration, zone ID, RRULE | Intent is "9:00 Pacific every Monday" |
| All-day | Floating date, no zone | A birthday is the same calendar date everywhere |
Never keep a numeric offset instead of a zone ID, since an offset cannot encode DST rules.
DST example: a weekly 9:00 America/Los_Angeles meeting is 17:00 UTC in winter and 16:00 UTC after the spring change. The organizer always sees 9:00. A London attendee sees 17:00 for most of the year but 16:00 during the few weeks when the US has switched and the UK has not. This is correct: the meeting follows the organizer's zone. Calling this out is worth doing.
Edge cases (follow RFC 5545 and the platform library behavior):
- Gap (spring forward): a 2:30 occurrence on transition day does not exist. Shift forward by the gap length (3:30 daylight time).
- Overlap (fall back): 1:30 occurs twice. Choose the first (earlier offset) deterministically and document it.
- Duration: store as a span so 9:00-10:00 stays one hour on the wall clock. Decide, and document, what an event that spans the transition means in elapsed time.
- tzdata updates: governments change rules at short notice. Because you stored local time and zone ID, you can simply recompute. Record the
tzdata_versionused for each materialization and re-expand affected series when a new release arrives.
4. Data model
events(event_id, calendar_id, organizer_id, title, start_local, duration,
tzid, all_day, rrule, sequence, etag, updated_at)
event_exceptions(event_id, recurrence_id, status{CANCELLED,MODIFIED}, overrides...)
attendees(event_id, user_id, role, rsvp)
instance_index(user_id, start_utc, end_utc, event_id, recurrence_id, busy)
instance_index is derived and can always be rebuilt.
Use iCalendar RRULE plus exception rows. This gives interoperability with ICS/CalDAV and proven expansion libraries.
Edit scopes:
- This occurrence: upsert an exception row keyed by
recurrence_id. - This and following: split the series. Set UNTIL on the old master just before the occurrence and create a new master from there, carrying over or dropping later exceptions.
- All: update the master, keep exceptions that still match the new rule, and flag orphans.
5. Storage and expansion
Infinite series cannot be fully materialized, while pure read-time expansion means every range query expands every series the user belongs to, which is costly at 75K QPS.
Hybrid: master tables are the source of truth. A background expander writes instance_index rows for a rolling window (for example 3 months back to 12 months ahead) for each attendee. Queries inside the window are one range scan on (user_id, start_utc). Queries outside fall back to on-the-fly expansion. A daily job slides the window.
Partitioning: shard events by calendar ID and instance_index by user ID, so one agenda is a single-shard scan. A sharded relational store gives per-shard transactions and range scans.
Fan-out: a master write emits a change event to a log (for example Kafka). Expander workers recompute the series and upsert rows onto each attendee's shard, in batches for very large meetings.
Caching: cache hot "this week" views per user and invalidate from the same change stream.
6. Conflict detection
- People (advisory): humans double-book, so show a warning. Query
instance_indexper attendee using the overlap testa.start < b.end && b.start < a.end, always in UTC. For a new repeating event, expand its occurrences in the window and test each. - Rooms (strict): must never double-book. Put each room's bookings on one shard keyed by room ID and enforce it transactionally, via a serializable insert with an overlap check, an exclusion constraint, or a per-room lock/slot table. Reject or retry on conflict. Repeating room bookings are validated against every occurrence in the booking horizon, atomically.
7. Consistency and scaling
- Master writes are strongly consistent within a shard, with ETag checks to catch concurrent edits (return 412 and let the client re-read).
- Instance index and caches are eventually consistent, normally within seconds. For read-your-writes, the writer's client can merge its own pending change or read the master.
- Rooms are the one place where you pay for strong consistency.
- Scale horizontally by shard; reads are served from replicas and caches; fan-out is asynchronous and idempotent (keyed by event, sequence and recurrence ID).
8. Common mistakes
- Storing recurring events as UTC and drifting an hour after DST.
- Storing offsets instead of zone IDs.
- Treating all-day events as 24-hour UTC spans.
- Comparing local times in conflict checks.
- Materializing infinite series, or expanding everything at read time.
- Ignoring tzdata updates and the gap and overlap hours.
- Forgetting that "this and following" edits require splitting the series.