KadraGuides

Booking bugs C# developers fix in ASP.NET Core

Booking systems look like simple CRUD until two people click Book at the same moment. Most of their bugs are races between requests or confusion about time zones, and neither shows up when you test alone on your own laptop. This guide covers the ones that come up most, with examples from Reserva, Kadra's ASP.NET Core booking practice codebase.

Updated

Check and reserve in one step

The natural first version asks whether the slot is free, then inserts the booking. Between those two calls another request can do the same thing, so both see a free slot and both succeed. That's Reserva's BOOK-801 ticket.

The fix is to make the decision and the insert a single atomic operation, so there's no gap for a second request to fit into:

// Wrong: another request can book between these two lines
if (await store.IsSlotFree(slot.Id, ct))
    await store.Insert(booking, ct);

// Right: one call decides and reserves
return await store.TryReserve(booking, slot.Capacity, ct)
    ? new BookingResult(true, 201, booking.BookingId)
    : new BookingResult(false, 409, null);

In a database, TryReserve is usually backed by a unique index on the slot for active bookings, so the second insert fails and you return 409 Conflict. A filtered index in SQL Server or a partial index in Postgres covers "active only", which lets a cancelled booking free the slot.

To test it, start many booking tasks for the same slot at once with Task.WhenAll and assert that exactly one gets a 201.

Store times in UTC and convert only for display

If slot times are saved in the server's local zone, a user in another country sees the wrong time, and the times move again when daylight saving starts or ends. BOOK-808 in Reserva is this bug.

In .NET the trap is DateTimeOffset.DateTime. It keeps the clock time and throws the offset away, so 09:00 in Toronto becomes a plain 09:00 with no zone. Use UtcDateTime or ToUniversalTime() when you store, and convert to the user's zone when you show it:

// Store: an exact instant in UTC
var startUtc = requestedStart.ToUniversalTime();

// Show: convert for this user
var userZone = TimeZoneInfo.FindSystemTimeZoneById(user.TimeZoneId);
var local = TimeZoneInfo.ConvertTime(slot.StartUtc, userZone);

Test with two users in different zones and with a slot that falls on a daylight saving change. An existing slot should keep the same instant on both sides of the change.

Enforce capacity under concurrent requests

A class with ten seats counts the current bookings and inserts if there's room. Under a burst of requests, several read the same count before any of them inserts, and the class ends up with twelve. That's BOOK-815, the senior ticket in Reserva.

Two things have to be right. The count and the insert have to happen together, under a lock or in one database transaction. And the comparison has to stop at capacity, not one past it:

// Full when the count reaches capacity
if (activeCount >= capacity)
    return false;

Test it with more requests than seats, all at once. Exactly capacity of them should succeed, and cancelling one should let exactly one more in.

Practice on a booking service

Reserva is an ASP.NET Core booking service with the BOOK tickets above planted in it. You get your own GitHub repo, fix each ticket on a branch and open a pull request. An AI teammate reviews the diff, and a hidden xUnit suite run by dotnet test decides whether the fix passes.

Every Reserva repo opens in GitHub Codespaces with the .NET 8 SDK installed, so there's nothing to set up on your own machine.

Questions people ask

Should I use DateTime or DateTimeOffset in C#?

Use DateTimeOffset for instants, such as when a booking starts, because it carries the offset with it. Plain DateTime makes it easy to lose track of whether a value is UTC or local.

How do I test a race condition in C#?

Start many tasks that make the same request and await them together with Task.WhenAll, then assert on the outcome, such as exactly one success. Run it several times, because a race doesn't fail on every run.

What status code should a double booking return?

409 Conflict. The request was valid, but it conflicts with the current state of the slot, and the client can pick another time.

Is Kadra free?

Yes, Kadra is free during beta with no credit card. After beta, Pro is planned at $12 a month or $79 a year, and the demo and your first full sprint stay free.

Try it on a real ticket

Pick a stack, fix a bug in a working codebase and get your pull request reviewed. Free during beta.

More guides