Example: Booking System

A complete scheduling app on web pages: a day view of open slots, one-click booking with no double-bookings, cancellation and automatic reminders.

Two storages: what gets booked, and each reservation

  • resources: what gets booked (rooms, tables, staff): name, capacity, opening hours, timezone.
  • bookings: one document per reservation.
Booking Document
{
  "_meta": {
    "cms": 1782166695442,
    "created": 1782166695,
    "entity": 1,
    "expiry": -1,
    "guid": "e67919283b4e37c9758326446605ab863bf452cda26f91c2aef74ba31f9a3214",
    "removed": 0,
    "ums": 1782170295118,
    "update_hash": "7b25e0af9163c4d8052ae7f31964bc0d8a5f3c62",
    "updated": 1782170295
  },
  "customer_guid": "b9ee6a631b24e78d1aa48aef0dc067fff7bfbacd24a56ef4ed1f08e537d48b6bd02",
  "customer_name": "sami b",
  "end": 1782364800,
  "reminder_sent": false,
  "resource_guid": "e01ff8643394371a8544d2c9f8a1b3e5d70892c4f6a0b8d1e3f5a7c9b2d4e6f8a0b2",
  "resource_name": "meeting room 2",
  "start": 1782361200,
  "status": "confirmed"
}

Resource and customer are denormalized onto the booking, so calendar views show names without joins (see Storage Examples).

Availability is a search for overlaps

  1. Find the overlaps

    Query the resource's bookings overlapping the requested window: start < requested_end AND end > requested_start. Two range filters on the same query via query_bool_range_field (number), plus a term on status = confirmed.

  2. Read the result

    No overlaps: the slot is free. Overlaps: return the conflicting bookings so the page can show why.

  3. Build the day

    Generate the day's slots from opening hours minus confirmed bookings, in a function with array_map (map), not in the template. Each slot gets from, to and free.

The day page lists every slot

web_page (new web page) with event book/:resource and scope *loggedin. Run the function above with function_custom_execute (execute), then wire its slots into web_foreach (web for each) with key field set to from, and print the rows through web_render (web render).

Stored template: slot-row
<li>
  {{from | date "HH:mm"}} to {{to | date "HH:mm"}}
  {{if free}}
    <button type="button" data-action="book" data-param-to="{{to}}" data-loading-text="Booking">Book</button>
  {{else}}
    <span>Taken</span>
  {{/if}}
</li>
Page template
<main>
  <h1>{{title}}</h1>
  <ul id="slots">{{{slots}}}</ul>
  <p id="booking-message" role="status"></p>
</main>

The slots use from and to because a bare {{end}} closes a block in a template. It cannot print a field named end.

A lock per resource rules out double-booking

Two reservations for one slot are two different documents, so the per-document lock of an update block does not help. The check and the create have to happen under one lock of your own.

  1. Catch the click

    web_action (on web action) book. Its key pin is the slot's start, form holds to, and params holds the resource.

  2. Claim the resource

    function_claim_lock_wait (lock or wait) with auto_clear on and a key of booking- plus the resource guid. Bookings for other rooms never wait.

  3. Re-check the overlap

    Run the overlap search again, and never trust what the page showed earlier. branch (branch) on has_hits.

  4. Create and wait

    No overlap: function_create_document_from_mutations (create document), then function_wait_for_document_search (wait for document) with the block's timestamp. A search can lag behind a write, and this wait is what guarantees the next holder of the lock sees this booking.

  5. Update the row

    web_template (web template) with {{> slot-row}} and free false, then web_replace (web replace) with the slot's key. On a conflict, send the same row and a message with web_update (web update) on #booking-message.

An API on the same flow replies 409 with the overlapping booking instead. See error conventions.

Cancellation is a status change to cancelled, never a delete. The audit trail and no-show stats survive, and the overlap search only counts confirmed bookings, so the slot is free again.

An hourly job sends the reminders

  1. Run every hour

    schedule_repeating_event (repeating event) with next_run fed by date_add_hours (plus hours): a next run at or before the current run stops the schedule.

  2. Find what is due

    Query confirmed bookings starting within the next 24 hours where reminder_sent is false.

  3. Send and mark

    Send each with the email pattern from Email System Setup, then set reminder_sent to true. That keeps the job idempotent even if a run repeats.

Customers manage their own bookings on a *loggedin page: user_current (current user) through user_get_guid (get guid) into a term query on customer_guid.

Next steps

Repeating Events The reminder engine's scheduler. Locking and Concurrency Which operations lock themselves, and when to claim a lock. Creating a REST API Expose availability to other apps. Inventory Tracker The same transaction discipline for stock.

Frequently asked

How do I check whether a time slot is available in RUAL?

Query the resource's bookings overlapping the requested window with two range filters on the same query: start < requested_end AND end > requested_start. No overlaps means the slot is free, and any overlaps are returned so the UI can show why.

How do I prevent double-bookings in RUAL?

Claim function_claim_lock_wait on the resource and re-check the overlap inside that lock, never trusting the page's earlier check. Create the booking, then use function_wait_for_document_search so the next claim's search sees it, and on conflict answer with the overlapping booking.

How do I send booking reminders in RUAL?

Run a schedule_repeating_event hourly that queries confirmed bookings starting within the next 24 hours where reminder_sent is false, send each one, then set reminder_sent = true. The marker keeps the job idempotent even if a run repeats.