Here's how a 5-line change dropped Truth or Dare Bot's CPU usage from 50-130% to 5%.
You heard it right, 5% cpu for a bot in 600,000 servers.
Truth or Dare uses HTTP Interactions, a method for running bots where Discord sends you a POST request when a command is run, and you respond how the bot should respond.
The bot is, essentially, a web server (fastify) allowing for great speed and performance.
Unfortunately, the problems begin with making sure it's really Discord sending these requests, and Discord requires that you do so.
Discord sends a signature on every request, which should be verified against the timestamp and body of it, a slow mathy process.
This is simple for smaller bots, but large scale, it gets difficult as efficiency is super important.
We receive over 1.2M requests per day, sometimes 18/s at peak hours, and have 3 seconds to respond to each interaction.
Previously, we used discord-interactions, a discord-made package using tweetnacl, which isn't native, and is pretty slow.
High CPU & event loop lag lead to many temporary outages and slow response times.
Our 5-line switch was to @IanMitchel1's discord-verify package!
It uses the node:crypto webcrypto library which runs on native code. It also uses the most efficient methods for each platform: cloudflare workers, vercel, and node.
And, the results! Huge drop in latency, cpu, and event loop lag. This simple change made the bot much faster and much more stable- in 5 lines 🤯
Thank you @IanMitchel1 for your hours of work getting this to work while I sat back and enjoyed the fruits of your labor! Definitely didn't lock you in the basement for this one.