Khalil
Khalil12mo ago

Rate limiting in Convex

is there a recommended approach for rate limiting http endpoints with Convex? Or do I have to build my own solution
3 Replies
Michal Srb
Michal Srb12mo ago
There is an example in this template: https://www.convex.dev/templates/nextjs-app-router
Templates
The backend application platform with everything you need to build your product.
Khalil
KhalilOP12mo ago
this bit?
const numSentRecently = (
await ctx.db
.query("posts")
.withIndex("authorId", (q) =>
q
.eq("authorId", author._id)
.gte("_creationTime", Date.now() - 1000 * 60)
)
.take(3)
).length;

if (numSentRecently >= 3) {
throw new Error("Too fast, slow down!");
}
const numSentRecently = (
await ctx.db
.query("posts")
.withIndex("authorId", (q) =>
q
.eq("authorId", author._id)
.gte("_creationTime", Date.now() - 1000 * 60)
)
.take(3)
).length;

if (numSentRecently >= 3) {
throw new Error("Too fast, slow down!");
}
I should have phrased my question better, sorry for that. I am looking for a solution involving rate limiting by IP address (to avoid DDoS attacks for example), most likely it will be a custom solution involving Redis

Did you find this page helpful?