Engineering

The double-booking guard I actually shipped

RxClinic prevents overlapping appointments with a read before the write. That is a real guard with a real race window, and it is worth being honest about both.

Mahsa Hassani
The double-booking guard I actually shipped

RxClinic is a clinic appointment API: patients book slots, staff book on their behalf, and no doctor should end up with two appointments over the same minutes. The guard that enforces this is a query immediately before the insert, and it is the whole mechanism.

What the handler does

Overlap is a range comparison, not an equality check. Two appointments collide when one starts before the other ends and ends after the other starts, and only appointments still in play count — a cancelled slot is free again.

// Conflict: same doctor, active status, overlapping time range.
bool overlaps = await _db.Appointments.AnyAsync(a =>
    a.DoctorId == request.DoctorId &&
    (a.Status == AppointmentStatus.Pending || a.Status == AppointmentStatus.Confirmed) &&
    a.StartTime < request.EndTime &&
    a.EndTime > request.StartTime);

if (overlaps)
    return Conflict(new { message = "The selected time slot is already booked for this doctor." });

Three details in that predicate are doing real work. Status filtering means cancellations release the slot without deleting history. The strict inequalities let a 09:00-09:30 appointment sit flush against a 09:30-10:00 one instead of falsely colliding. And the whole thing is one AnyAsync, so it costs a single existence check rather than loading the day.

The window I am not pretending is closed

Between that read and the SaveChangesAsync a few lines later, nothing stops a second request from running the same query and getting the same answer. Two concurrent bookings for the same slot can both pass the check and both insert. This is check-then-act, and check-then-act has a race.

For where RxClinic actually runs — a single process against a SQLite file — that window is narrow and the operational risk is small. It is not zero, and the honest description is "a guard that holds under the load it was built for", not "a guarantee".

Why the storage engine has not closed it for me

The reflex answer is a unique index, and it is the right instinct: a constraint the database enforces cannot be raced past, however many processes are running. But overlap is not equality. A unique index can enforce "no two appointments for this doctor start at exactly this instant"; it cannot express "no two appointments for this doctor overlap at all". The range case needs an exclusion constraint, which SQLite does not have and PostgreSQL does, via btree_gist.

Push invariants to the lowest layer that can enforce them. The catch is that the lowest layer has to be able to express the invariant — and range overlap is where that stops being free.

Next steps — not yet implemented

Everything in this section is intention, not shipped code. RxClinic today has exactly one unique index, on User.Username, and no overlap constraint of any kind.

  • Fixed-length slots would make overlap collapse into equality, and a unique index on (DoctorId, StartTime) filtered to active statuses would then close the race outright.
  • Staying with arbitrary ranges means moving to PostgreSQL and an EXCLUDE USING gist constraint — the only option here that enforces true overlap in the engine.
  • Either route needs the unique-violation exception translated at the persistence boundary into the same 409 the read path already returns, so callers see one behaviour.

Until one of those lands, the read-before-write guard is what is running, and saying so is more useful than describing a constraint that is not there.