Stop Double Bookings in an Appointment App¶
Two clients open a salon's booking page at the same time. Both see 10:00 free with Anna. Both click it. Both get a confirmation. On Saturday morning, one of them is turned away.
This is the most common bug in booking apps, and it's not a front-end bug: the page showed the truth when it loaded. The fix has to be where the booking is written.
Why checking first isn't enough¶
The obvious code reads Anna's appointments, checks that 10:00 is free, and writes the booking. Between the read and the write there's a gap, and two requests can pass the check in that gap before either writes. A lock in your app server doesn't help once you run two servers. The check and the write have to be one step.
Appointments are Reservations¶
In ObjectsGrid an appointment is a Reservation of a Service,
with the staff member as provider and the slot as startTime and endTime:
curl -X POST https://apis.objectsgrid.com/reservations \
-H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"reservationFor": {"at_id": "https://apis.objectsgrid.com/services/<haircut>"},
"provider": {"at_id": "https://apis.objectsgrid.com/persons/<anna>"},
"underName": {"at_id": "https://apis.objectsgrid.com/persons/<client>"},
"reservationStatus": "https://schema.org/ReservationConfirmed",
"startTime": "2027-01-16T10:00:00+01:00", "endTime": "2027-01-16T10:45:00+01:00"}'
If another appointment of Anna's, not cancelled, overlaps 10:00 to 10:45, the write is refused with 409 and names
the reservation in the way. The check and the write happen together, so of two bookings arriving at the same moment,
exactly one succeeds. Cancelling, deleting or moving an appointment to another provider frees its time.
Tables, rooms and tickets, Reservations with a kind such as FoodEstablishmentReservation, aren't checked this way:
a restaurant may well seat two parties at 19:00.
Free slots from working hours¶
To show a booking page, you need the gaps, not the bookings. Staff working hours live on their EmployeeRole, as Schedules:
{
"employee": {"at_id": "https://apis.objectsgrid.com/persons/<anna>"},
"employer": {"at_id": "https://apis.objectsgrid.com/places/<salon>"},
"providesService": [{"at_id": "https://apis.objectsgrid.com/services/<haircut>"}],
"workSchedule": [
{"byDay": ["https://schema.org/Tuesday", "https://schema.org/Thursday"],
"startTime": "09:00:00", "endTime": "17:00:00", "scheduleTimezone": "Europe/Berlin"},
{"byDay": ["https://schema.org/Saturday"], "startTime": "09:00:00", "endTime": "13:00:00",
"scheduleTimezone": "Europe/Berlin", "repeatFrequency": "P2W", "startDate": "2027-01-02",
"exceptDate": ["2027-12-25"]}
]
}
scheduleTimezonekeeps 09:00 at 09:00 local time across daylight saving changes.repeatFrequencyP2Wwith astartDateis every other week: Anna works alternate Saturdays.exceptDateleaves out holidays.
GraphQL's availableSlots turns that into slots:
query {
availableSlots(provider: "https://apis.objectsgrid.com/persons/<anna>",
from: "2027-01-11T00:00:00+01:00", to: "2027-01-18T00:00:00+01:00",
duration: "PT45M") { startTime endTime }
}
It reads Anna's working hours, takes away her appointments, and returns 45-minute slots, in her schedule's time zone, for a window of up to 31 days.
Book, then confirm¶
availableSlots reads appointments from search, seconds behind a write, so a slot it lists may have been taken a
moment ago. That's fine, because the write is the authority: book the slot, and show the client the answer. A 409
means "someone was faster": reload the slots and let them pick another.
Who may book¶
Clients booking for themselves, a front desk booking for walk-ins, practitioners seeing their day: your
access policy decides each, and availableSlots runs with the caller's
permissions. A ready-made policy for a business with several locations, where clients' appointments start as pending
and the front desk confirms them, is in
how to configure your ABAC policy at registration.
The GraphQL guide has the details of availableSlots.