Prompt
How do I debug double-booking in my scheduling flow?
Latest observation
Double-booking usually comes from a race condition: two requests check availability at the same time, both see “free,” and both create bookings before either one commits.
Here’s a practical way to debug it.
1) Reproduce it reliably
Try to create the overlap intentionally:
- Open two browser tabs
- Send two API requests at nearly the same time
- Use a load test tool or a small script to fire concurrent requests
If it only happens under concurrency, that’s a strong sign it’s a locking/transaction issue.
2) Trace the exact booking path
Add logs with a correlation ID at each step:
- request received
- availability checked
- slot reserved
- booking written
- transaction committed
Log timestamps and the resource/slot ID. You want to see whether two flows overlap in time.
3) Inspect the database writes
Check whether you’re doing:
SELECT available?- then later
INSERT booking
If those are separate steps without a transaction or lock, they’re vulnerable.
Common failure patterns:
- no transaction around check + insert
- transaction isolation too weak
- missing unique constraint
- caching stale availability
- background jobs creating bookings after the check
4) Add a database-level guard
Even if your app logic is correct, enforce it in the DB.
Examples:
- Unique constraint on
(resource_id, start_time, end_time)if exact slots - Exclusion constraint for overlapping ranges if your DB supports it
- Separate “reservation” row with unique key on resource + time slot
This prevents double-booking even if two app servers race.
5) Use transactions with locking
Typical approaches:
- Wrap availability check and insert in one transaction
- Lock the relevant row(s) with
SELECT ... FOR UPDATE - Use serializable isolation if appropriate
- Re-check availability inside the transaction before committing
If you’re booking a specific resource, lock the resource row while deciding.
6) Look for stale reads
If you read availability from:
- a cache
- a read replica
- a denormalized search index
…it may lag behind the source of truth. For booking decisions, read from the primary database or ensure strong consistency.
7) Check idempotency
If clients retry requests, you may create duplicates unless requests are idempotent.
Use:
- an idempotency key per booking attempt
- a unique constraint on that key
- logic that returns the original booking if the same request is retried
8) Add tests for concurrency
Write tests that simulate parallel booking attempts:
- two threads/processes booking same slot
- high-concurrency integration test
- assertions that only one succeeds
This is often the fastest way to catch the bug after you fix it.
9) Watch for timezone and boundary bugs
Sometimes “double-booking” is really:
- off-by-one minute/second
- timezone conversion mismatch
- daylight saving transition
- inclusive vs exclusive end-time logic
Make sure your slot overlap logic is consistent, e.g.:
- booking A ends exactly when booking B starts should usually be allowed
- but
[start, end]vs[start, end)must be handled carefully
10) Add observability
Track:
- booking attempts
- booking conflicts
- retries
- transaction aborts
- latency between availability check and insert
A spike in conflicts or long check-to-insert times can reveal the root cause.
A robust pattern
A safe booking flow usually looks like:
- Receive request with idempotency key
- Start DB transaction
- Lock resource or relevant schedule rows
- Check for overlap inside the transaction
- Insert booking
- Commit
- Return booking result
And back it with a DB constraint so the database is the final authority.
If you want, I can help you debug your specific stack if you paste:
- your booking flow pseudocode
- database type
- whether you use caches/read replicas
- the overlap logic you’re using