← Back to home

Case study 01 Pre-production

Scheduling for a multi-gym platform

Recurring classes, edits that apply to one session or a whole series, and room bookings that stay correct when many people act at once.

Key points

The problem

Gyms plan classes as weekly schedules: a spinning class every Monday and Wednesday at 19:00, a personal-training slot every Friday. Staff need to change one session ("this Monday is cancelled"), everything from a date onwards ("from March, it moves to 19:30"), or the whole series. Each session has a room with limited capacity, attendees, and a waitlist. Schedules also go through a weekly approval flow before athletes see them.

What I built

  • Recurring events, generated lazily. Instead of creating every future session up front, the system generates occurrences up to a moving horizon and extends it in the background. Dates that fail to generate are retried rather than silently lost.
  • Three edit scopes. Every change applies to this event, this and following, or all events. Changing the recurrence of one occurrence splits it off into a new series, so history before the split stays untouched.
  • Client and server agree. The web portal expands recurrence rules the same way the API does, so the calendar shows the same occurrences the API generates.
  • Room conflicts and waitlists under concurrency. Booking a room or joining a full class takes database row locks in a fixed order. An earlier version with a stronger lock (FOR UPDATE) deadlocked; switching to a narrower lock (FOR NO KEY UPDATE) and a consistent ordering removed the deadlocks. Waitlists are first-in, first-out.
  • Weekly publishing that respects time zones. Approved weeks publish automatically on a schedule, calculated in Lisbon time so daylight-saving changes don't shift sessions by an hour.

Decisions and trade-offs

  • Lazy generation vs. materialising everything. Generating far ahead is simple but makes every series edit rewrite hundreds of rows. A moving horizon keeps edits cheap, at the cost of a background job and a cursor to maintain.
  • Delegated types for event kinds. Group class, personal training, evaluation and floor duty use delegated types instead of single-table inheritance, so each kind keeps its own columns without one wide, sparse table.
  • Soft deletes with cascading. Cancelling a series keeps the history that attendance and reports depend on.
  • Authorisation scoped to the caller's gym. Requests outside it return "not found" rather than "forbidden" so one gym can't discover another gym's data.

What I'd point out

The hard part wasn't any single feature; it was keeping the rules consistent across three edit scopes, a lazy generator, concurrent bookings and a calendar UI that mirrors the server.