What is Polling? And what is a Webhook?
And how should you make the right choice in your project?
Let’s clarify it in today’s post.
Polling: A Resource-Intensive Approach
When ordering food at a restaurant, it would be impractical and embarrassing to constantly ask the waiter if your order is ready.
This scenario illustrates the inefficiency of polling, a method where a client repeatedly requests data from a server at predefined intervals.
Polling involves one service making repeated requests to another service to check for new data.
Here are the key characteristics of polling:
Repeated Requests: In polling, the client sends requests to the server at regular intervals, asking if there is any new data available. This approach is akin to constantly asking, "Do you have something new for me?" even when there might be no updates.
Resource-Intensive: Polling is resource-intensive and inefficient. It consumes network bandwidth and server resources, even when there are no updates available.
Missed Real-Time Updates: Polling can lead to missed real-time updates. Since the client checks for updates at predefined intervals, it may not receive notifications immediately when data becomes available.
Webhooks: A Notification-Based Approach
Webhooks offer a more efficient and real-time alternative to polling. Here’s how webhooks work:
Callback URL: With webhooks, you set up a callback URL that one system uses to notify another system when new data is available. This approach is like having a built-in notification system where the server says, "I’ll let you know when something happens."
Push-Based Data Delivery: Webhooks push data to the recipient service as soon as it becomes available, ensuring real-time updates. This makes webhooks ideal for scenarios where timely notifications are crucial.
Efficiency and Real-Time Updates: Webhooks are more efficient than polling because they eliminate the need for repeated requests. They provide real-time updates, making them perfect for applications like payment notifications, CI/CD platforms, and other services that require immediate updates.
When to Use Polling and Webhooks
Each approach has its use cases:
Polling:
Frequent Data Updates: Polling is better when you don't need real-time data updates and the data is frequently updated. Sending webhooks for every event can be resource-intensive. For example, a social media app that wants to report on its user base might be overwhelmed with webhooks if many users sign up in quick succession.
Flexibility: You can customize the sync frequency based on how often you need updated data.
Webhooks:
Real-Time Updates: Webhooks are perfect for scenarios where real-time updates matter. Examples include payment notifications, chat applications, and any service that requires immediate notifications.
Efficient Resource Utilization: Webhooks are more efficient in terms of resource utilization compared to polling. They ensure that data is delivered in real time without the need for repeated requests.
So - have you used Polling or Webhooks in your application?
Shoutout
Here are some interesting articles I’ve read recently:
Master The 5 Types of Mocks by
System design isn't a Cut & Paste job by
Raul Junco
How to design a system for scale by
How Code Reviews Shape Productivity, Team Culture, and Mental Health by
4 tools to supercharge your Jest Testing by
That’s it for today! ☀️
Enjoyed this issue of the newsletter?
Share with your friends and colleagues.
See you later with another edition — Saurabh
Can webhooks be used on FE? And callback_url would be like a endpoint that will be called by recipient service, when it has any updates? Is my understanding right?
An excellent comparison between the two, i especially liked this compact article this time!
And huge thanks for the shoutout!