A lot of systems answer questions that don’t have one single source of truth. A search result has to reconcile a live index with whatever changed since the last crawl. A read replica has to be treated as “probably current, but verify for anything that matters.” A materialized view is a cached answer to a question the underlying tables could re-answer more slowly but more correctly. In each case the system combines data that updates at different speeds and can disagree at any moment, and the result is only correct if the merge is.
The usual failure: two sources are each reasonable on their own, but nobody designed for what happens when they disagree, so the system trusts whichever ran last or whichever the code checks first. Neither source has a bug. The merge logic is missing an explicit decision about priority and staleness.
A concrete version: is this time slot open
A scheduling system answering “is this person free at this time” is a small example. No single table holds the answer. There are at least three sources, and they update at different rates:
| Source | Update pattern | What it holds |
|---|---|---|
| A stored rule | Changes rarely, edited directly by the user | Working hours, buffers, min/max lead time |
| A cached copy of external data | Synced periodically, carries a TTL | Busy blocks pulled from an external calendar |
| Live authoritative data | Always current, but requires a network call to fetch | Bookings the system itself already holds |
In SmartCal’s calculateAvailability() (src/lib/availability/calculate-availability.ts), all three are fetched in parallel:
const [eventType, connectedCalendars, cachedBlocks, hostUser] = await Promise.all([
prisma.eventType.findUnique({
where: { id: eventTypeId },
include: {
availabilityRule: true,
bookings: {
where: {
status: { in: ['CONFIRMED', 'PENDING'] },
startsAt: { lt: timeRange.end },
endsAt: { gt: timeRange.start },
},
},
},
}),
prisma.connectedCalendar.findMany({ where: { userId, provider: 'GOOGLE', syncEnabled: true } }),
prisma.calendarBusyBlock.findMany({
where: {
userId,
startsAt: { lt: timeRange.end },
endsAt: { gt: timeRange.start },
OR: [{ expiresAt: null }, { expiresAt: { gte: new Date() } }],
},
}),
prisma.user.findUnique({ where: { id: userId } }),
]);
Fetching in parallel only affects speed. Correctness depends on what happens after the fetch: which source wins when two disagree about the same window, and what happens when one of them can’t be trusted right now.
Live data adds to the cache
The live lookup, a network call to the external calendar provider, happens in a second step, one request per connected calendar, using Promise.allSettled instead of Promise.all:
const calendarResults = await Promise.allSettled(
connectedCalendars.map((calendar) =>
getFreeBusy(userId, calendar.id, timeRange.start, timeRange.end)
.then((freeBusy) => ({ calendarId: calendar.id, busy: freeBusy.busy }))
)
);
for (const result of calendarResults) {
if (result.status === 'fulfilled') {
for (const busy of result.value.busy) {
busyBlocks.push({ id: `${result.value.calendarId}-${busy.start}`, startsAt: new Date(busy.start), endsAt: new Date(busy.end), isTransparent: false });
}
}
}
allSettled fits here: a slow or failing external calendar shouldn’t take down the whole check. It just means that calendar’s live data is missing from this answer. The cached blocks are pushed onto the same busyBlocks list unconditionally, whether or not the live fetch for that calendar succeeded:
for (const block of cachedBlocks) {
busyBlocks.push({ id: block.id, startsAt: block.startsAt, endsAt: block.endsAt, isTransparent: block.isTransparent });
}
This is what makes the design safe. The cache is always in the answer, and the live call adds to it. If the network call succeeds, the check has the cache plus fresher data. If it fails, the cache still bounds how wrong the answer can be, by however stale the cache can get before its TTL expires. When a live source can fail but a cached source effectively can’t (a database read almost always succeeds), don’t make the cache’s inclusion depend on the live call. Merge them additively, so the live source can only add information.
Why buffers have to apply uniformly across sources
Once busy time from all three sources lands in one list, the interval check treats them uniformly, applying the same before/after buffer to a booking made directly in the system and to a block synced from an external calendar:
const bufferBefore = (rule?.bufferBefore ?? eventType.bufferTime ?? 0) * 60 * 1000;
const bufferAfter = (rule?.bufferAfter ?? eventType.bufferTime ?? 0) * 60 * 1000;
for (const booking of existingBookings) {
const bufferedStart = booking.startsAt.getTime() - bufferBefore;
const bufferedEnd = booking.endsAt.getTime() + bufferAfter;
if (intervalStart < bufferedEnd && intervalEnd > bufferedStart) return false;
}
for (const busyBlock of busyBlocks) {
if (busyBlock.isTransparent) continue;
const busyStart = busyBlock.startsAt.getTime() - bufferBefore;
const busyEnd = busyBlock.endsAt.getTime() + bufferAfter;
if (intervalStart < busyEnd && intervalEnd > busyStart) return false;
}
This is easy to get wrong: applying a business rule (the buffer) to one source of busy time and forgetting the other, because the two arrive through different code paths and data models. A host who sets a 15-minute buffer wants it around every meeting, whichever system it was booked through. A merge that buffers only its own bookings implements a different feature from the one requested, and the difference only shows up when a meeting from the other source sits right at the edge of a slot.
The general shape of the problem
Wherever a system caches part of its own truth (a search index behind a database, a materialized view behind tables, a read replica behind a primary), these questions decide whether the merge is safe:
- When two sources disagree about the same fact, which one wins, and is that decision made once in the merge logic or implicitly wherever the data happens to get read?
- Does a cached source get treated as a real answer, or only as a fallback when the live source fails? Getting that wrong either discards a signal for no reason or leaves the system exposed when the live source fails.
- Do rules that are supposed to apply globally (buffers, permissions, filters) get applied to every source, or only to whichever source the rule was written against first?
None of these has one correct answer. A system might prefer live data over cache or the reverse, depending on what it’s protecting against. The choice still has to be explicit and applied to every source that answers the same question. Otherwise there’s no crash or visible error, just a plausible answer that’s wrong in whatever way the unexamined priority order produces, and that kind of bug tends to survive testing.