Nov 20, 20268 min read

Webhooks vs Polling: How to Choose for Backend Integrations

When a backend should receive webhooks and when it should poll an API, comparing latency, reliability, rate limits and operational cost, why most production integrations end up doing both, and a Go service that takes webhooks and reconciles with a scheduled poll.

Illustration for Webhooks vs Polling: How to Choose for Backend Integrations

Most backend integrations start with a choice about a third-party API: ask it for changes on a schedule, or wait for the provider to send them. Webhooks look obvious until a two-minute deploy drops a payment update, and polling looks safe until rate limits and delays reach support tickets.

This guide compares both on latency, reliability, limits and cost, and shows the hybrid most production integrations end up with.

Webhooks vs Polling at a Glance

WebhooksPolling
LatencySeconds after the eventUp to one polling interval
Calls when nothing changesNoneOne per interval
Missed eventsPossible when your endpoint is down and retries run outNot possible with a changes-since query
Inbound trafficNeeds a public HTTPS endpointOutbound calls only
Security workVerify signatures, reject replaysStore an API key
OrderingNot guaranteed, retries arrive lateYou control the order you process
Rate limitsProvider's problemYours, every interval
Best forPayments, chat, CI events, anything user-visibleProviders without webhooks, batch syncs, reconciliation

When Are Webhooks the Better Choice?

Webhooks win when users wait on the result, such as a checkout page waiting for a payment to succeed or a CI badge waiting for a build. They also scale better against busy accounts: a poll that lists ten thousand objects every minute to find three changes wastes both sides' capacity, while a webhook sends only the three.

In exchange, your service has to be the reliable party, with a public endpoint that answers fast, typically within 10 seconds before GitHub, for one, records the delivery as failed. You also need signature verification, duplicate handling and a plan for downtime, because retry behavior varies a lot between providers: Stripe retries undelivered events for up to three days, while GitHub does not redeliver failed deliveries automatically at all.

When Is Polling the Better Choice?

Polling wins when the provider has no webhooks, when the backend runs somewhere that cannot accept inbound traffic, or when minutes of delay are fine, such as a nightly sync of product catalog data. It also has a property webhooks lack: if the API can list changes since a timestamp or cursor, a poll cannot miss anything, because the next run picks up whatever the last one did not.

Store a cursor or the last updated_at you saw, and ask only for newer records. Use conditional requests where the API supports them; GitHub, for example, does not count a 304 Not Modified against the rate limit when the request sends If-None-Match with a stored ETag. Back off when the API returns 429, and space out polls for accounts that rarely change.

Why Do Production Integrations Use Both?

Webhooks give latency and polling gives completeness, so integrations that move money or goods usually run both. The webhook handler takes events as they arrive, and a scheduled job lists recent changes every hour or so and feeds anything new through the same ingestion code. For this to work, three rules apply to both paths:

  • One idempotent ingestion function. An event seen twice, once by webhook and once by the poll, must change nothing the second time, which the event's provider ID makes easy as a unique key.
  • Newer wins. Webhooks can arrive out of order after retries, so store the provider's timestamp or version and ignore updates older than what you have.
  • Acknowledge fast, process after. The webhook handler verifies, stores and returns 200, and slower work happens afterwards, so a slow downstream call never causes the provider to retry.

The Standard Webhooks specification is a useful reference for the signing and header conventions if you are the one sending webhooks.

How Do You Build the Hybrid in an Encore App?

Encore

Encore is a backend framework with built-in infrastructure, so the webhook endpoint, the reconciliation schedule, the database and the provider secrets are all declared in the same Go service, and deploying it to AWS or GCP gives you a public HTTPS endpoint, the database and the schedule in your own account. The example tracks shipment status from a carrier that sends webhooks and also offers a list-changes API.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

~
$ encore app create carrier-sync --example=hello-world$ cd carrier-sync && mkdir -p shipments/migrations

The events table is the idempotency key, and the shipments table only moves forward in time:

-- shipments/migrations/1_shipments.up.sql CREATE TABLE carrier_events ( event_id TEXT PRIMARY KEY, received_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE TABLE shipments ( shipment_id TEXT PRIMARY KEY, status TEXT NOT NULL, status_at TIMESTAMPTZ NOT NULL ); CREATE TABLE sync_state ( name TEXT PRIMARY KEY, cursor TIMESTAMPTZ NOT NULL );

Both paths call ingest, which records the event ID and applies the status only if it is newer:

// shipments/shipments.go package shipments import ( "context" "crypto/hmac" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "time" "encore.dev/cron" "encore.dev/rlog" "encore.dev/storage/sqldb" ) var db = sqldb.NewDatabase("shipments", sqldb.DatabaseConfig{ Migrations: "./migrations", }) var secrets struct { CarrierWebhookSecret string CarrierAPIKey string } type CarrierEvent struct { ID string `json:"id"` ShipmentID string `json:"shipment_id"` Status string `json:"status"` OccurredAt time.Time `json:"occurred_at"` } // ingest is safe to call any number of times for the same event. func ingest(ctx context.Context, ev CarrierEvent) error { tx, err := db.Begin(ctx) if err != nil { return err } defer tx.Rollback() res, err := tx.Exec(ctx, ` INSERT INTO carrier_events (event_id) VALUES ($1) ON CONFLICT DO NOTHING`, ev.ID) if err != nil { return err } if res.RowsAffected() == 0 { return nil // already seen through the other path } _, err = tx.Exec(ctx, ` INSERT INTO shipments (shipment_id, status, status_at) VALUES ($1, $2, $3) ON CONFLICT (shipment_id) DO UPDATE SET status = EXCLUDED.status, status_at = EXCLUDED.status_at WHERE shipments.status_at < EXCLUDED.status_at`, ev.ShipmentID, ev.Status, ev.OccurredAt) if err != nil { return err } return tx.Commit() } //encore:api public raw method=POST path=/webhooks/carrier func CarrierWebhook(w http.ResponseWriter, req *http.Request) { body, err := io.ReadAll(http.MaxBytesReader(w, req.Body, 1<<20)) if err != nil { http.Error(w, "body too large", http.StatusRequestEntityTooLarge) return } mac := hmac.New(sha256.New, []byte(secrets.CarrierWebhookSecret)) mac.Write(body) sig, err := hex.DecodeString(req.Header.Get("X-Carrier-Signature")) if err != nil || !hmac.Equal(sig, mac.Sum(nil)) { http.Error(w, "invalid signature", http.StatusUnauthorized) return } var ev CarrierEvent if err := json.Unmarshal(body, &ev); err != nil || ev.ID == "" { http.Error(w, "invalid event", http.StatusBadRequest) return } if err := ingest(req.Context(), ev); err != nil { http.Error(w, "retry later", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) } var _ = cron.NewJob("carrier-reconcile", cron.JobConfig{ Title: "Poll the carrier for events webhooks missed", Every: 1 * cron.Hour, Endpoint: Reconcile, }) type ReconcileResult struct { Seen int `json:"seen"` } //encore:api private func Reconcile(ctx context.Context) (*ReconcileResult, error) { since := time.Now().Add(-6 * time.Hour) err := db.QueryRow(ctx, `SELECT cursor FROM sync_state WHERE name = 'carrier'`).Scan(&since) if err != nil && !errors.Is(err, sqldb.ErrNoRows) { return nil, err } endpoint := "https://api.carrier.example.com/v1/events?updated_since=" + url.QueryEscape(since.Format(time.RFC3339)) req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+secrets.CarrierAPIKey) resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("carrier returned %d", resp.StatusCode) } var page struct { Events []CarrierEvent `json:"events"` } if err := json.NewDecoder(resp.Body).Decode(&page); err != nil { return nil, err } latest := since for _, ev := range page.Events { if err := ingest(ctx, ev); err != nil { return nil, err } if ev.OccurredAt.After(latest) { latest = ev.OccurredAt } } // Overlap by ten minutes so events written late on the carrier side are not skipped. _, err = db.Exec(ctx, ` INSERT INTO sync_state (name, cursor) VALUES ('carrier', $1) ON CONFLICT (name) DO UPDATE SET cursor = EXCLUDED.cursor`, latest.Add(-10*time.Minute)) rlog.Info("carrier reconcile done", "events", len(page.Events)) return &ReconcileResult{Seen: len(page.Events)}, err }

The webhook is a raw endpoint, so the handler reads the exact body bytes the signature covers, and the two credentials are secrets whose values never enter the repo. encore run starts Postgres and the endpoint locally, so you can replay a signed sample event with curl and watch the transaction in the trace. Cron jobs do not run locally, so trigger Reconcile from the local dashboard while developing; in deployed environments the hourly cron job is Encore platform managed and calls the endpoint on schedule.

When the carrier adds a new event type, an agent can extend CarrierEvent and ingest, replay a captured payload locally and see both paths use the new code, since the webhook, the poll and the tables are one service.

Best fit: backends that integrate with payment, shipping or SaaS providers and want webhook intake and scheduled reconciliation sharing one idempotent code path in their own AWS or GCP account.

Webhooks vs Polling Checklist

  • Use webhooks when users wait on the result, polling when minutes are fine.
  • Verify webhook signatures against the raw body and reject bad ones.
  • Acknowledge webhooks fast and process afterwards.
  • Make ingestion idempotent on the provider's event ID.
  • Apply updates only when they are newer than what you store.
  • Poll with a cursor or conditional requests, and back off on 429.
  • Run a reconciliation poll for anything involving money or goods.

Frequently asked questions

What is the difference between webhooks and polling?

With polling, your backend calls the provider's API on a schedule and asks what changed. With webhooks, the provider sends an HTTP request to your endpoint when something happens. Webhooks deliver changes within seconds and only when there are changes, while polling is simpler to run and never depends on the provider reaching you.

Are webhooks more reliable than polling?

No. Webhook deliveries can fail while your endpoint is down or slow, and providers differ on retries: Stripe retries for up to three days, while GitHub does not automatically redeliver failed deliveries. Polling cannot miss a change as long as the API lets you list changes since a point in time.

When should I use polling instead of webhooks?

Poll when the provider has no webhooks, when your backend cannot accept inbound HTTP, when a delay of minutes is fine, or to reconcile and catch events a webhook missed. Use conditional requests or an updated-since filter so each poll is cheap and stays inside rate limits.

Should I use both webhooks and polling?

Yes. For anything involving money, orders or shipments, use webhooks for low latency and a periodic poll that lists recent changes to catch anything that was missed, with both paths writing through the same idempotent ingestion code.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders