Bot integration architecture: Webhook vs Long Polling in Production
When launching a bot (in Telegram, VK or MAX platform) on a production server (Production Server), architects have to make the first critical choice: use Long Polling or configure a Webhook.
In articles for beginners they often write: “Long Polling for tests, Webhook for production”. In the reality of a large enterprise, this thesis is far from the truth. Let's look at the mechanics of both approaches and the hidden pitfalls of operation.
Long Polling: False Simplicity
In this model, the initiator of communication is always your server. The bot makes an HTTP request to the platform API (for example, getUpdates) and literally “freezes” waiting for a response (from 30 to 60 seconds). As soon as the user writes a message, the platform immediately returns it in the body of the response, and the cycle repeats.
The main advantages of Long Polling:
- Penetrates any firewall: Your server does not need a white static IP address and open incoming ports. It even works from an old laptop in the basement via home NAT.
- No need for SSL/TLS on your side: Since the bot acts as a client, it verifies the platform's certificate, and you don't need to bother with encryption (Let's Encrypt).
- Perimeter Security: You do not allow incoming traffic from the Internet into your local network.
Cons in Production:
- Horizontal scaling issues: If there are more than 1000 messages per second, raising a second instance (Docker container) with Long Polling simply won’t work. Both instances will start polling the servers in parallel (Race Condition), taking the same updates, or getting conflicts
HTTP 409 Conflict. Requires complex distributed locks. - Memory leaks: HTTP connections that are always open load the network.
Webhooks: Event-Driven Approach
In the event model, the initiator (client) is the MAX platform itself (or Telegram). As soon as the user submits a message, the platform server makes an incoming POST request to your pre-specified URL (Endpoint).
Pros of Webhook for Highload:
- Real scaling: You can put Nginx or HAProxy in front of your application, raise 50 microservices, and the balancer will evenly distribute incoming POST requests across containers (Round Robin/Least Conn).
- Saving CPU/RAM resources: The server is sleeping while there are no messages. There are no "empty" platform database polling cycles.
- Serverless & Lambda: Ideally fits with cloud serverless architecture (AWS Lambda, Yandex Cloud Functions). You only pay for real calculations.
Webhook Price in Production (Cons):
The architecture requires serious DevOps competence:
- Strict infrastructure requirements: Public "white" IPv4, domain, and strict configuration
https://(Valid SSL). - Silent Failures: If your server is down or your DNS has failed, the platform will not be able to deliver the hook. It will try to send several Retry requests (usually with an exponential delay) and then delete the event (Drop). The lead will be irretrievably lost. In Long Polling, events wait for you in the platform queue for up to 24 hours in case you fall.
- DDoS vulnerability: If you do not configure header validation (
X-Hub-Signatureor IP Whitelisting), an attacker can send fake POST requests to your public URL, imitating the platform and clogging your CRM with spam.
We are building a hybrid and reliable Production
Based on experience NBM-IT, the best architectural pattern for webhooks is built like this:
- The platform sends POST $\rightarrow$
- On our side they are met by a gateway (API Gateway). He checks
Secret Tokenand the sender's IP address (Security). - The gateway instantly puts the message in RabbitMQ / Kafka / Redis List and responds to the platform
HTTP 200 OK(so that the platform does not duplicate sending). - Internal “deaf” workers (without access to the Internet at all) pick up messages from the queue and process complex logic, accessing the CRM.
Recommendation: Always start the MVP of the project on Long Polling. This will save days of work at the start. And only after proving the business model and the appearance of a traffic flow, rewrite the transport on Webhook with queues.
Need a fault-tolerant smart bot?
Discuss architecture, limits and scenarios with NBM-IT technical specialists.
