← All articles

5 min read

The Last-Second Bid Problem: Engineering an Auction Backend That Stays Fair

Row locks, idempotency keys, a server-owned clock and soft close: the decisions that keep an online auction correct when everyone bids at once.

Every online auction has the same moment of truth: the last sixty seconds. Traffic spikes, two buyers press “bid” within the same millisecond, someone’s phone reconnects and resends the request, and the countdown on every screen has to agree. If your backend gets this wrong, you do not just have a bug. You have a buyer who believes they won a car they did not win.

I built the bidding and auction logic for a car auction platform serving the European market. These are the engineering decisions that keep an auction fair under pressure, whether you are selling vehicles, art, domain names or anything else.

Rule 1: the database decides who is winning, not the application

The classic bug looks innocent:

$auction = Auction::find($id);
if ($amount > $auction->current_price) {
    $auction->update(['current_price' => $amount, 'leader_id' => $user->id]);
}

Two requests read the same price at the same time, both pass the check, and the slower one silently overwrites the higher bid. The fix is to make “read, check, write” one atomic step. In Laravel, a transaction with a row lock does exactly that:

public function placeBid(int $auctionId, User $user, int $amount, string $requestKey): Bid
{
    $bid = DB::transaction(function () use ($auctionId, $user, $amount, $requestKey) {
        // A retried request returns the bid it already created.
        if ($existing = Bid::firstWhere('request_key', $requestKey)) {
            return $existing;
        }

        // Only one bid per auction can hold this lock at a time.
        $auction = Auction::whereKey($auctionId)->lockForUpdate()->firstOrFail();

        if (now()->gte($auction->ends_at)) {
            throw new AuctionClosed();
        }

        $minimum = $auction->current_price + $auction->min_increment;
        if ($amount < $minimum) {
            throw new BidTooLow($minimum);
        }

        $bid = $auction->bids()->create([
            'user_id'     => $user->id,
            'amount'      => $amount,
            'request_key' => $requestKey,   // unique index in the database
        ]);

        $auction->update([
            'current_price' => $amount,
            'leader_id'     => $user->id,
            'ends_at'       => $this->softCloseEnd($auction),
        ]);

        return $bid;
    });

    BidPlaced::dispatch($bid);   // broadcast only after the commit succeeded
    return $bid;
}

Three things happen here that matter more than the syntax:

  • The lock serialises bids per auction, not across the whole platform, so 500 live auctions still run in parallel.
  • The minimum increment is checked inside the lock, against the latest price, never against what the user’s screen showed.
  • The broadcast happens after commit. If you announce a new leader and the transaction then rolls back, every screen shows a bid that does not exist.

Rule 2: every bid request must be safe to send twice

Mobile networks drop. Users double-tap. Load balancers retry. Without protection, one tap can become two bids, and the second can even outbid the user’s own first bid. Give every bid an idempotency key, generated on the client when the button is pressed, and enforce it with a unique index. A retry then returns the original result instead of creating a new bid.

Rule 3: the server owns the clock

Never let a browser decide that an auction has ended. Device clocks drift by seconds, and some users will “fix” theirs on purpose. The server stores ends_at, checks it inside the lock, and sends the remaining time to clients, which only display a countdown. When the final decision and the display disagree, the server wins and the UI corrects itself.

Rule 4: decide your anti-sniping policy before launch

“Sniping”, meaning bidding in the last second so nobody can respond, is a business decision with engineering consequences. A common answer is a soft close: any bid in the final two minutes extends the auction by two minutes. It keeps prices fair and turns the ending into a real contest. Whatever you choose, implement it inside the same locked transaction as the bid, so the extension and the bid can never disagree.

private function softCloseEnd(Auction $auction): Carbon
{
    $window = now()->addMinutes(2);
    return $auction->ends_at->lt($window) ? $window : $auction->ends_at;
}

Rule 5: real-time updates are a broadcast, not the source of truth

WebSockets make an auction feel alive, and buyers expect the price to update instantly. But messages can arrive late, out of order, or not at all. Design clients to treat each update as a hint: include the auction’s current price and a version number in every message, ignore anything older than what is already shown, and re-fetch state on reconnect. Then a dropped message is invisible to the user instead of becoming a support ticket.

Rule 6: keep an append-only audit trail

When a buyer disputes a result, “the database says so” is not an answer. Store every bid attempt, accepted or rejected, with the time received, the reason for rejection, and the request key. An append-only bid table turns disputes from arguments into lookups, and it is invaluable when you are tuning increments and soft-close rules later.

Rule 7: test the last minute, not the average minute

Average load tells you nothing about an auction platform. Write a test that fires hundreds of concurrent bids at one auction in its final seconds, then assert the invariants: exactly one leader, a strictly increasing price, no bid accepted after close, and no duplicate bids from retried keys. If those four hold under a deliberate stampede, they will hold on a busy Saturday.

Beyond auctions

The same patterns, atomic state changes, idempotency, a server-owned clock and an audit trail, apply to ticket sales, limited product drops, booking systems and marketplaces. Anywhere many people compete for one thing at one moment, correctness under contention is the product.

Building a marketplace, auction or booking platform? I have built auction and bidding logic, marketplace backends and admin panels in Laravel and Node.js. Let’s talk about your platform.

Share

Have a product in mind?

Open to senior backend roles and long-term product builds.

Get in touch