DEV Community

Cover image for Webhooks vs polling: how the job tells you it is done
System Design Simple
System Design Simple

Posted on Originally published at systemdesignsimple.com

Webhooks vs polling: how the job tells you it is done

Polling asks a server "done yet?" on a timer. A webhook is the server sending you an HTTP delivery when the work is done. The difference is direction, and it decides how many requests you burn.

Two GitHub workflow runs polled every second for an hour make 7,200 requests. An authenticated user usually gets 5,000 requests an hour, shared across everything that token does. So two runs at one poll a second already exceed the budget, if every poll counts.

The rate limit math

One poll a second is 3,600 requests an hour per run watched. Poll every 10 seconds and one run costs 360 an hour, so 13 runs fit under 5,000 (4,680) and 14 do not (5,040). One escape hatch exists: a correctly authorized conditional request that returns 304 Not Modified does not count against GitHub's primary rate limit. A poll that returns changed data with a 200 still counts.

Our simulation: 200 jobs, polling vs webhook

Our seeded simulation on a simulated clock: 200 jobs finishing between 2 and 30 seconds, no real GitHub calls.

  • Poll every 1 s: 16.75 status requests per job, 94% answered not done, median delay 0.568 s.
  • Poll every 10 s: 2.09 requests per job, 52% not done, median delay 4.555 s.
  • Webhook: 1.00 delivery attempt per job, median delay 0.050 s.
  • Webhook with 5% of calls dropped and no retry: 13 of 200 results still unknown after 10 simulated minutes.
  • Webhook URL down, sender retries plus a 60 s poll per job: 6.00 attempts per job, median delay 43.745 s, 0 unknown.

How to receive a GitHub webhook

GitHub wants a 2XX within 10 seconds or the delivery counts as failed. Verify the HMAC signature, commit the delivery to storage that survives a crash, then answer 2XX, and let a separate worker do the work later.

Two details matter. The signature covers the raw body only, so the X-GitHub-Delivery and X-GitHub-Event headers are unsigned: decide from the signed body, not the headers. And dedupe on a key from that body, workflow_run.id plus run_attempt, because a re-run gets a new attempt number.

What happens when a delivery fails

GitHub does not automatically redeliver failed deliveries. Redeliver by hand, or run a scheduled script that lists recent deliveries, picks the ones whose status is not OK, and redelivers them through the REST API. For long outages, add a reconciliation job that re-fetches open runs until they report completed.

Our take

Polling is fine for a few rare checks, a script waiting on one build or a dashboard refreshing every minute. With many runs in flight, a tight delay target, or a shared token, webhooks remove most of those checks. The cost moves to the receiver: a signature check, an insert keyed by the run and committed before the 2XX, an idempotent worker, a redelivery script, and a reconciliation job.

Read the full article with the complete simulation table and receiver code on System Design Simple: https://systemdesignsimple.com/webhooks-vs-polling-how-the-job-tells-you-its-done/

Top comments (1)

Collapse
 
krusenas profile image
Karolis •

GitHub doesn't auto-redeliver failed deliveries, so recovery is entirely on you. One check that's saved us, alert when a run you know is in flight goes quiet for a few minutes instead of waiting for the reconciliation sweep to notice. fwiw I built Webhook Relay around this exact gap, failed deliveries get stored and retried with backoff.