Scheduling Collision Detection for a Shared Calendar Service
System DesignModel answerLast updated
Design the backend that detects and handles scheduling collisions when users create or edit events, covering recurrence, attendees, privacy, time zones, and concurrent edits at massive scale.
You are designing the event and collision-handling backend of a calendar product used by hundreds of millions of people.
Users create, edit, and delete events. An event can include a list of attendees, start and end times, a time zone, a recurrence rule, reminders, a visibility level, a location, and a video-call link.
Your focus is what happens when a new or modified event collides with something already on someone's calendar. Cover the data model, the path a collision check takes, and how the design behaves under heavy load.
Assumptions to state and justify
Pick numbers of a sensible order of magnitude. A reasonable starting point:
- Around $10^8$ active users, each with many events. A single busy calendar (a meeting room, an executive) may hold thousands of events per quarter.
- Traffic is dominated by reads: availability lookups and collision checks far outnumber writes.
- A collision check during create or update should feel interactive, with a target of tens of milliseconds for the common case of an organizer plus a few attendees.
- Events must never be silently lost, and reads must be highly available.
- The collision policy is configurable:
reject,warn(user must confirm), orallow override. Consumer calendars typically allow double-booking with a warning. - Only events marked busy, tentative, or out-of-office affect availability. Events marked free never cause a collision.
Questions you should consider asking
- Is double-booking permitted with a warning, or blocked outright? Does the answer differ for organizers and invitees?
- With several attendees, does a collision for any one of them block the event, only required attendees, or only the organizer?
- How far ahead must checks on recurring events look, and can a series be unbounded?
- What do users see of calendars they do not own: full details, busy-only, or nothing?
- Should tentative, free, and out-of-office events count as hard collisions, soft collisions, or be ignored?
- Is interoperability with external or federated calendars required?
What to deliver
Walk through requirements, data model, the exact overlap rule, indexing, recurrence handling, the create/update flow, concurrency control, privacy, time-zone correctness, consistency between stores, and the user-facing experience when a collision is found.
Hints
Hint 1
Separate the canonical event record from a derived, query-optimized availability index. The index only needs calendar id, time range, busy status, and a pointer back to the event.
Hint 2
Define overlap precisely with half-open intervals: two events collide when A.start < B.end and B.start < A.end. That makes back-to-back meetings non-colliding. Then decide how free, tentative, out-of-office, and all-day events are treated.
Hint 3
Do not expand an unbounded recurrence forever. Store the rule, materialize a rolling window ahead of time, expand on demand beyond it, and handle per-occurrence exceptions. For concurrent edits, compare optimistic versioning against a short per-calendar lock.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
1. Requirements
Functional: create, update, and delete events; attendees and RSVP; recurrence with exceptions; reminders; visibility; collision detection; suggested alternative times; notifications.
Non-functional: interactive latency (tens of ms p50 for a check), high read availability, no lost events, correctness across time zones and DST, and strict privacy of other people's event details.
2. Data model
Two layers:
- Canonical store (sharded relational or Spanner-style database), partitioned by calendar id:
events(event_id, organizer_id, title, start_utc, end_utc, tz_id, rrule, visibility, busy_status, location, meeting_url, version, updated_at)attendees(event_id, user_id, role, rsvp)exceptions(series_id, original_start, override_event_id or cancelled)
- Availability index (derived), one row per occurrence per calendar:
(calendar_id, start_utc, end_utc, event_id, busy_status, visibility), clustered or indexed on(calendar_id, start_utc).
The index is small and narrow, so it can be cached and scanned quickly. The canonical store remains the source of truth.
3. Collision semantics
- Treat intervals as half-open:
[start, end). Events A and B collide iffA.start < B.end AND B.start < A.end. Back-to-back events do not collide. - Only
busy,tentative, andout-of-officeparticipate.freeis ignored. Tentative can be reported as a soft collision. - All-day events are normalized to the user's local midnight-to-midnight range, converted to UTC for that user.
- Zero-length events are rejected or given a minimum duration.
4. Indexing and query path
For one calendar and a candidate window [s, e), query rows with start < e and end > s. With a B-tree on (calendar_id, start_utc) and a bounded maximum event length (say 24 hours, longer events stored separately), scan start in [s - maxLen, e) and filter by end > s. That is O(log n + k) where k is the number of candidates, usually tiny.
For very busy calendars (rooms), use time-bucketed partitions (per week) or an interval tree held in memory per hot calendar. Insert is O(log n).
For multi-attendee events, fan out to each attendee's shard in parallel and merge results. Cap fan-out for huge groups: check the organizer and required attendees synchronously, and the rest asynchronously, since a large invite list cannot be allowed to block on thousands of lookups.
5. Recurrence
- Store the RRULE plus the time zone id and local start time, not a list of UTC instants. This keeps "9:00 every Monday" at 9:00 local across DST changes.
- Materialize occurrences into the index for a rolling horizon (e.g., 12 months). A background job extends the window. Beyond the horizon, expand on demand during checks.
- Exceptions: "this occurrence" creates an override or cancellation record; "this and future" splits the series by truncating the old rule with an UNTIL and creating a new series; "all" rewrites the rule and rebuilds the index rows.
- Report collisions per occurrence, grouped (for example, "conflicts on 4 of the next 20 occurrences") rather than flooding the user.
- For a check on an unbounded series, test only the horizon plus a user-visible warning that later occurrences are unchecked.
6. Create and update flow
- Validate input, resolve the time zone to UTC, and expand the recurrence for the check window.
- Authorize: can the caller write the organizer calendar and see the attendees' availability?
- Query the availability index for each relevant calendar, in parallel, with a tight timeout.
- Apply policy:
reject: return 409 with the colliding intervals.warn: return a response flagged as needing confirmation; the client resends with a confirm flag.allow: commit and attach collision metadata.
- Commit the event and its index rows (see consistency below).
- Publish notifications and invitations asynchronously.
On an update, exclude the event's own previous rows from the check, otherwise moving an event by 10 minutes collides with itself.
7. Concurrency control
The classic race is check-then-write: two users both see a free slot and both book it.
- Optimistic versioning on each event (
versioncolumn, compare-and-swap) protects against lost updates to the same event. It does not by itself prevent two different events from colliding. - For policies that hard-reject (rooms, resources), serialize writes per calendar: a short per-calendar lock or a single-writer partition, re-running the check inside the transaction. Holding the lock only for a few milliseconds keeps contention low, and hot calendars are rare.
- For soft policies (warn), a race is acceptable: both events are saved and the next view shows both as overlapping. Do not pay for strong serialization where the product does not need it.
- A reservation with a TTL (hold slot for 30 seconds while the user confirms) works for booking flows, at the cost of added state and cleanup.
Trade-off: strong guarantees cost latency and availability on hot keys; apply them only to resources that need them.
8. Privacy
Collision responses must respect ACLs. If the caller can see event details, return them. Otherwise return an opaque busy block (time range only). Private events always appear as "busy" to others. Never leak titles, attendees, or locations through error messages, timing, or suggested alternatives.
9. Time zones and DST
Store UTC instants for single events plus the original tz id. For recurring events store local wall time plus tz id and expand using the tz database at materialization time. Handle skipped local times (spring forward) by shifting forward, and repeated times (fall back) by choosing the first occurrence. Keep tz database updates in mind: rebuild affected index rows when rules change.
10. Consistency between store and index
Write the event and the index rows in one transaction when they share a shard. Otherwise use a transactional outbox or change-data-capture so the index is updated asynchronously and idempotently, keyed by (event_id, occurrence_start). Accept brief staleness for reads but run the authoritative check inside the write path for hard-reject cases. Provide a rebuild job that regenerates the index from the canonical store, and reconcile periodically.
11. Scale and caching
Shard by calendar id. Cache free/busy for hot calendars with short TTLs and invalidate on writes. Use read replicas for free/busy reads. At 10^8 users, assume roughly 10^3 to 10^4 index rows per user per year at the high end, so on the order of 10^11 rows overall: sharded and partitioned by time so old partitions can be moved to cheaper storage.
12. User experience
Return the colliding intervals and, where possible, suggested alternative slots, computed by merging attendees' busy intervals and finding gaps that satisfy working hours and duration. A bare rejection is a poor experience.
Common mistakes
- Using closed intervals and flagging back-to-back meetings.
- Storing recurring events as fixed UTC times and breaking at DST.
- Expanding infinite recurrences.
- Letting an event collide with its own old version on update.
- Leaking private event details in conflict responses.
- Applying heavyweight locking to every write instead of only where hard guarantees are required.