Lesson 3 of 9 · 22m
Bot Lifecycles, Deploys, and Operational Observability
Run gateway shards and interaction workers through restarts, identify storms, command registration, and incidents without losing state or duplicating actions.
A Discord bot is easy to run on a laptop and strangely easy to run badly in production. The failure modes are lifecycle failures: overlapping deployments double-processing events, crashed shards never reconnecting, command registrations drifted across environments, and operators unable to tell whether Discord is down or the bot is merely stuck.
Treat identify as a scarce operation
Gateway sessions can often resume. Identify is different: Discord controls how often shards may identify, and large bots receive a max_concurrency value that defines parallel identify buckets. This means restarting every worker at once can turn a healthy deploy into a self-inflicted outage.
A graceful process should:
1. stop accepting new work
2. finish or hand off in-flight interaction work
3. close gateway sockets intentionally
4. persist durable worker state
5. exit before the supervisor forces terminationDo not confuse websocket close codes with application health. Record why the process left, whether resume was possible, and whether the next start identified or resumed.
Command registration is deployment state
Application commands are not merely code. They are registered API objects with names, descriptions, options, defaults, permissions, and guild/global scope. A local development guild gives fast iteration; global registration is production state and can take time to propagate.
Keep registration deterministic:
NODE_ENV=staging node scripts/register-commands.js --guild "$DEV_GUILD_ID"
NODE_ENV=production node scripts/register-commands.js --globalThe script should diff desired command definitions against remote definitions and print what changed. Do not silently mutate global commands from every replica boot. One release pipeline owns registration; runtime processes consume the result.
Measure the bottlenecks Discord gives you
Useful production metrics include:
gateway heartbeat latency and zombie-connection detection
identify/resume counts by shard
event lag from Discord timestamp to consumer start
interaction acknowledgement latency
rate-limit waits by bucket and route
queue depth and retry count for outbound actions
guild count, cache hit ratio, and memory per shardLog guild IDs and interaction IDs as correlation keys where privacy rules permit. Avoid logging message content by default; moderation evidence belongs in controlled storage, not in every observability backend.
Design for partial Discord failure
Sometimes one gateway region, event type, or API route is degraded while the rest works. A bot that treats Discord as either up or down will make bad choices. Shed expensive work, extend retry backoff, and keep safety-critical enforcement simple. If your moderation bot cannot reach its dashboard database, it should still enforce configured slowmode or join raid rules from cached state.
Field notes
- A restart loop is often an identify-rate problem, not a code crash. Count attempts before increasing replicas.
- Blue/green deployment can double-process gateway events unless only one generation owns the session.
- Registering commands in a test guild is fast because guild commands are scoped; global commands are the production contract.
- Your health check should distinguish process liveness from gateway freshness. A live process with a dead session is not healthy.
Was this lesson helpful?