Rate Limit with DB
this is more of a general knowledge question, but is it a good idea to store user rate limiting in my database? I guess it is, cause realistically there ain't other options ig. I also don't know how much faster a KV database is for this (in comparison to a doc with a single indexed row), but I don't wanna use multiple database providers (since Convex doesn't do KV)
4 Replies
Also, this is my first time extensively working with a real database, and a document database too, I feel like I'm gonna blow up my usage lol (guess that's normal though, to an extent)
Yes I'd use Convex for any k/v uses as well.
Simple k/v schema:
Performance should be similar (order of a few ms)- esp. since you're already colocated with the DB.
The only nuance I'd think about is to avoid database contention by only reading & writing the document related to the limit in question, so you don't have conflicts when many things are reading & writing at the same time. e.g. a per-user document rather than a singleton document
rate limiting on queries isn't really an option, but the nature of the queries is that if the parameters and underlying data aren't changing, it is just hitting the cache
@shadow_aya just fyi, @CharstarAdmin is something of an expert on using convex to help with rate limiting and traffic mangaement now, and he's using it for these kinds of use cases with many many many thousands of MUAs
oh nice
thanks for the tips, sounds like a table will do
my plan is to implement an
express-rate-limit
Store for this
(seems pretty straightforward https://github.com/express-rate-limit/express-rate-limit/wiki/Creating-Your-Own-Store)