upstash/ratelimit-js

Self-hosted Redis use?

Closed this issue · 4 comments

Can this library be used with a self-hosted Redis instance? Or is it only meant to integrate with the Upstash Redis service?

It should work with other providers as well. But, being serverless means it expects an endpoint and a token. I'm not sure how you'll provide those in a self-hosting setup, but it should be doable.

You can look here for more info upstash/redis-js#802

In my case I'd host it on AWS ElastiCache, so the endpoint is fine. However, looks like it expects an HTTP proxy, as described in this repo's README.

I've decided to go for the limits python package, since it seems that this lib is a no-go for my use-case.

Thanks all! :)

For anyone else looking for help:

I got this working with node-redis and wrapping with an adapter for a couple of functions.
Initially I tried ioredis but I couldn't see the needed .scriptLoad function call by ratelimit and moved on.

import {createClient}  from '@redis/client' 
import { Ratelimit } from "@upstash/ratelimit"

const redisClient = createClient({url: 'redis://127.0.0.1:6379'})
redisClient.evalsha = (sha1,keys,args) => redisClient.evalSha(sha1,{keys,arguments: args.map(arg => arg.toString())})
redisClient.hset = redisClient.hSet.bind(redisClient)
await redisClient.connect()
const ratelimit = new Ratelimit({
            redis: redisClient,
            limiter: Ratelimit.slidingWindow(20,'100s'),
            prefix: 'api-ratelimit',
          })
redisClient.disconnect()