Webhook or API Polling: What Is the Difference?
There are two ways for one app to learn what happened in another. It can ask, or it can be told. Asking is called polling. Being told is a webhook.
Polling: you keep asking
Polling means calling an API at intervals to see if data is available1. An API is the set of requests an app accepts from other software. A polling setup sends one question again and again. Any new orders?
Most answers are "no." A few are "yes." Between checks, a new order can sit unseen until the next question goes out.
You've probably met polling without the name. A refresh button is polling done by hand. A tool that checks on a timer is polling on a schedule.
Webhooks: the app tells you
With a webhook, the app speaks first. When the event happens, it sends a web request to the address you set up2. Stripe calls this pushing live data to the endpoint3.
You set this up once. Then you wait for messages that only arrive when there's something to report.
Side by side
| Polling | Webhook | |
|---|---|---|
| Who starts the contact | Your system | The sending app |
| When data arrives | At the next check | When the event happens |
| Traffic | Constant, even with no news | Only when there's an event |
| What you set up | A schedule for the checks | A public URL and a list of events |
GitHub's documentation lists the claimed advantages of webhooks over its API. They require less effort and fewer resources than polling an API, and they allow near real-time updates1.
It adds a point about scale. If you monitor many resources, calling the API for each one may hit your API rate limit quota quickly. With webhooks you subscribe to several events and receive information only when one happens1. A rate limit is a cap on how many requests an app lets you make in a set time.
When polling still fits
Webhooks aren't always the better choice. The same GitHub page makes this point. Say you need information only once in a while, or from a small set of resources with no plans to grow. Then you can call the API when you need it1.
Polling also suits an app that offers no webhooks. Check the maker's documentation for a list of events before you plan around one.
Using both
Some setups use both. A webhook announces that something changed, and the receiver then asks the API for the full record. Stripe describes this in its own guidance, which says you can use the API to retrieve any missing objects3.
How to tell which one an app uses
- Look in the app's settings for "webhooks," "event subscriptions" or "notifications to a URL."
- If you find one, the app can push. You'll be asked for a URL.
- If you only find "sync every so often" or "refresh," the app likely polls.
- Read the maker's documentation to confirm. Menu names vary from app to app.
A webhook still has weak points. Messages can fail on the way, so the receiver has to cope with that. Retry rules are covered under missed messages. The contents of a message are laid out in the anatomy of a request.
Sources used
- About webhooks, GitHub Docs
- Webhook, Wikipedia
- Receive Stripe events in your webhook endpoint, Stripe Documentation