BotNetwork

Single-flight long polling for Telegram

Only one getUpdates loop per token.

Guide1 min readUpdated

Why it matters

Telegram long polling must run as a single flight per token — two pollers steal updates from each other and create races.

Practical steps

  1. Guard startup with a lock (DB advisory lock, Redis SET NX, or flock).
  2. Exit non-zero if another poller holds the lock.
  3. Prefer webhooks in multi-instance deploys.
  4. On shutdown, stop getUpdates before closing DB pools.
  5. Log offset commits so you can debug gaps.

Watch out for

  • Running polling in Kubernetes with replicas>1 and no lock.
  • Mixing webhook and polling on the same token.

Related comparisons and tools