A concurrency-safe reservation platform that completely eliminates double bookings during high-traffic registration windows using distributed mutex locking.
import Redlock from "redlock";
import { redisClient } from "./redis";
const redlock = new Redlock([redisClient], {
retryCount: 5,
retryDelay: 150,
});
export async function reserveTimeSlot(slotId: string, customerId: string) {
const resourceKey = `locks:slot:${slotId}`;
const lock = await redlock.acquire([resourceKey], 4000);
try {
return await executeSlotBookingTransaction(slotId, customerId);
} finally {
await lock.release();
}
}