Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Building Real-Time Webhook Telemetry and Rate-Limited API Gateways in Python

When scaling an API-first SaaS or reseller infrastructure platform, exposing endpoints to automated client scripts introduces two immediate operational challenges: protecting your upstream workers from burst traffic spik

When scaling an API-first SaaS or reseller infrastructure platform, exposing endpoints to automated client scripts introduces two immediate operational challenges: protecting your upstream workers from burst traffic spikes, and giving developers real-time visibility into asynchronous job states without forcing them to poll endpoints every second.

While building out the REST API v2 gateway and developer tooling for SoneBoost, we tackled both problems by combining Redis-backed token-bucket rate limiting with push-based webhook and Telegram bot telemetry. Here is how the architecture works in practice.

  1. Protecting Upstream Workers with Redis Token-Bucket Rate Limiting When enterprise clients or agency scripts integrate an automated API, a misconfigured while loop or bulk CSV import can easily fire hundreds of concurrent requests per second. Without edge rate limiting, burst traffic can exhaust database connection pools or trigger upstream throttling.

Instead of relying on simple fixed-window counters (which allow double-bursts at window boundaries), a token-bucket algorithm implemented via an atomic Redis Lua script provides smooth, accurate throttling per API key:

Python
import time
import redis
from fastapi import FastAPI, Header, HTTPException

app = FastAPI()
r = redis.Redis(host="localhost", port=6379, decode_responses=True)

Atomic Lua script for token bucket rate limiting

LUA_TOKEN_BUCKET = """
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local refill_rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

local bucket = redis.call("HMGET", key, "tokens", "last_refill")
local tokens = tonumber(bucket[1])
local last_refill = tonumber(bucket[2])

if not tokens then
tokens = capacity
last_refill = now
else
local elapsed = math.max(0, now - last_refill)
tokens = math.min(capacity, tokens + (elapsed * refill_rate))
last_refill = now
end

if tokens >= 1 then
tokens = tokens - 1
redis.call("HMSET", key, "tokens", tokens, "last_refill", last_refill)
redis.call("EXPIRE", key, 60)
return 1
else
return 0
end
"""

rate_limit_script = r.register_script(LUA_TOKEN_BUCKET)

def check_rate_limit(api_key: str, capacity: int = 30, refill_per_sec: float = 5.0):
allowed = rate_limit_script(
keys=[f"rl:{api_key}"],
args=[capacity, refill_per_sec, time.time()]
)
if not allowed:
raise HTTPException(status_code=429, detail="Rate limit exceeded. Slow down requests.")
By executing the refill and deduction inside a single Redis Lua script, we eliminate race conditions across distributed API worker nodes while adding less than 1ms of latency per request.

  1. Replacing Polling Loops with Push-Based Telemetry A common anti-pattern in API integrations is aggressive client-side pollingβ€”where a script hits /api/v2/status every 2 seconds to check whether an asynchronous task has completed. At scale, 90% of your API traffic ends up being redundant status checks.

To eliminate polling overhead, we implemented a dual push-notification pipeline:

A. HMAC-Signed Outbound Webhooks
Whenever an asynchronous task transitions to completed or partial, our background worker dispatches a JSON payload to the client's registered callback URL. To ensure the receiving server can verify authenticity and prevent spoofing, every payload is signed using HMAC-SHA256:

Python
import hmac
import hashlib
import json
import requests

def send_signed_webhook(webhook_url: str, secret: str, event_data: dict):
raw_body = json.dumps(event_data, separators=(",", ":")).encode("utf-8")
signature = hmac.new(
key=secret.encode("utf-8"),
msg=raw_body,
digestmod=hashlib.sha256
).hexdigest()

headers = {
    "Content-Type": "application/json",
    "X-SoneBoost-Signature": f"sha256={signature}"
}
requests.post(webhook_url, data=raw_body, headers=headers, timeout=5)

B. Operational Alerts via Telegram Bot API
Not every user wants to spin up a public webhook listener just to monitor balance thresholds or batch completions. For lightweight real-time observability, we wired our event bus directly into a native Telegram bot (@SoneBoost_Bot).

When a wallet balance drops below a configured threshold or an automated USDT (TRC20) deposit confirms on-chain, the event worker pushes a formatted Markdown alert directly to the user's Telegram chatβ€”turning mobile notifications into an instant operational dashboard.

Summary
Designing developer-friendly infrastructure comes down to predictable behavior under load:

Enforce atomic rate limits at the gateway edge so one noisy client never degrades latency for others.

Sign all outbound webhooks with HMAC-SHA256 so consumers can verify payload integrity cleanly.

Offer low-friction telemetry like Telegram notifications alongside webhooks so teams don't rely on wasteful polling loops.

If you're building automated workflows or exploring REST API v2 specifications, feel free to check out our open schema examples in the SoneBoost API Docs on GitHub.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.