demo-webhooks: The Deterministic Webhook: How Vercel Labs Tames Deployment Chaos

Beyond the POST request. Building a resilient state machine for the modern cloud lifecycle.

• View on GitHub • More from vercel-labs

A mechanical sorting machine processing floating envelopes that are out of numerical order. A gauge blocks envelope 1 from passing if envelope 2 is already inside.
In distributed systems, webhooks rarely arrive in the neat order they were sent.

Webhook delivery order is **not guaranteed**. You may receive events out of order.

carbonrobot, Contributor/Maintainer · GitHub - vercel-labs/demo-webhooks

Key Takeaways

When Success Arrives Before Birth

The happy path of a deployment is a lie. When a system triggers a build, it fires off asynchronous events. 'Created' goes first. 'Succeeded' follows. But the network is a chaotic place. Sometimes, the success notification outruns the creation notice. If your dashboard blindly updates its state based on the last received payload, your UI will flicker, break, or show a completed build that suddenly reverts to 'in progress'.

This is the exact problem vercel-labs/demo-webhooks was built to solve. It is not just a demo. It is a state machine masterclass disguised as a playground.

Portrait of carbonrobot

The Hierarchy of Truth

To fix out-of-order delivery, the repository introduces a ranking system. It assigns a strict numerical value to every possible lifecycle state. 'Created' is zero. 'Succeeded' is one. 'Promoted' is two.

When a new webhook arrives, the Node.js receiver checks the incoming event's rank against the current state stored in Redis. If the new event has a lower rank than the existing state, the system discards it. This elegant logic ensures the strict forward momentum of the deployment lifecycle.

Incoming events are evaluated against the current stored state. Lower-ranked events are immediately discarded.

const ORDER = ['created', 'succeeded', 'promoted'];

function rank(state) {
  return ORDER.indexOf(state);
}

// Inside the webhook handler:
if (rank(incomingState) <= rank(currentState)) {
  console.log('Ignoring stale event');
  return res.status(200).end();
}

The Rollback Paradox

Tracking deployment state is only half the battle. The repository introduces a secondary abstraction called 'Project Mode'. This solves a deeply confusing edge case in modern cloud hosting.

Imagine a team deploys a broken update and immediately triggers an instant rollback to a previous version. Meanwhile, a developer pushes a new commit. That new commit will build. It will fire a 'Succeeded' webhook. But because the project is pinned in a rollback state, that successful build will not be promoted to production. A naive webhook receiver would see 'Succeeded' and incorrectly tell the team the new code is live.

A ship anchored to a heavy rusted weight labeled Rollback, while a shiny new ship sails past unable to dock.
A successful build means nothing if the project itself is anchored to an older version.

By tracking both the deployment lifecycle and the global project mode in Redis, the receiver can accurately reflect when a green checkmark is actually a lie. The logic forks: if the project is in rollback, ignore the success of new background builds.

Architectural Comparison: Script vs. State

Most developers treat webhooks as triggers. They write a simple serverless function that listens for a POST request and fires off a Slack message. This repository demonstrates why that approach fails at scale.

FeatureStandard ReceiverVercel Demo Architecture
Event OrderingBlindly overwrites state (Race conditions)Rank-based filtering discards stale events
Rollback AwarenessIgnorant (Shows false 'Live' status)Tracks distinct 'Project Mode' state
SecurityBasic string comparison (Vulnerable)Uses `crypto.timingSafeEqual`
Data LayerStateless fire-and-forgetRedis-backed dual-key storage

Cryptographic Paranoia

A webhook receiver is an open door to your infrastructure. The demo repository enforces strict cryptographic verification of the Vercel signature. It specifically uses Node's `timingSafeEqual` for the comparison.

Standard string comparisons return `false` the moment they hit a mismatched character. Attackers can measure the microsecond differences in response times to guess your secret key character by character. A timing-safe function takes the exact same amount of time to execute regardless of where the mismatch occurs. It is an essential detail for any production receiver.

The Blueprint

Event-driven architecture is inherently messy. You cannot control the network, and you cannot guarantee the order of operations. What you can control is how your system interprets the noise.

By combining a rank-based filter, strict state isolation, and cryptographic security, `vercel-labs/demo-webhooks` provides a blueprint for resilience. It proves that a reliable dashboard is never just a reflection of the latest event. It is a carefully curated source of truth.