Urgent.News

What's breaking now, across thousands of outlets.

Tech

The double-booking bug I shipped in my Next.js booking app (and how Postgres fixed it)

I build a multi-tenant booking template with Next.js, Drizzle and Postgres. During an audit I found that my "double booking protection" didn't protect anything. Here's the bug, and the fix. The naive version This is roughly what I had: const conflict = await db . select (). from ( bookings ). where ( and ( eq ( bookings . resourceId , resourceId ), gte ( bookings . startTime , start ), lte (…

During an audit of my Next.js booking app, I discovered that the double booking protection mechanism was flawed. The bug allowed two requests for the same time slot to proceed, resulting in concurrent INSERT operations. To address this, I implemented a fix using Postgres advisory locks within a transaction-scoped scope. By locking on the resource ID, the system serializes bookings for each resource, preventing conflicts while allowing parallel bookings for different resources.

The use of advisory locks was chosen over a unique constraint or exclusion constraint because it offers simplicity and readability without the need for additional extensions. For serverless deployments like Supabase, configuring the database with prepare: false enables the transaction-scoped lock to operate effectively. Additionally, I resolved a timezone bug by converting the business's wall-clock time to UTC using the tenant's IANA timezone, ensuring accurate slot availability across different time zones.

This solution, part of the SlotKit template, was implemented to enhance the reliability and accuracy of the booking system.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Why Old Server CPUs Are Cheap: A Practical Buying Guide

Why Old Server CPUs Are Popular Among Buyers Old server CPUs, often several generations behind the latest models, are highly sought after by buyers looking to save money without significantly…

  • Older server CPUs are affordable despite being several generations old.
  • Benchmarks show minimal performance loss for general tasks.
  • They are energy-efficient, reducing power bills and server cooling strain.

A cheap-onboarding trick that works for tiny SaaS: the free-tier cap

For my $5.50 lifetime ScsDriver WebDAV mount tool, I have a 7-day free trial. The trial converts at 14% to paid. After 8 months I tried a tiny change — adding a "trial features used: 3 of 3" cap…

  • Limiting trial users to three uses of each feature increases conversion from 14% to 22%, a 57% lift.
  • Visible counter displays per-feature counts, creating urgency without feeling punitive.
  • Resetting the counter upon upgrading maintains a positive user experience.

More from Monday 19 October →