Coding Profile picture
Aug 25 8 tweets 4 min read
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. A graph showing discord-interactions compared to discord-ver
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. Screenshot from the discord documentation showing how verify
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. A screenshot of github diff showing the 5 lines changed when
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 🤯 A graph showing discord-interactions on left compared to disA graph showing discord-interactions on left compared to disA graph showing discord-interactions on left compared to dis
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.

Be sure to check out discord-verify on npm:
npmjs.com/package/discor…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Coding

Coding Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(