upstash / redis-js

HTTP based Redis Client for Serverless and Edge Functions
https://docs.upstash.com/redis
MIT License
666 stars 51 forks source link

Is there a way to connect to a local redis server when using `@upstash/redis`? #230

Closed flexdinesh closed 7 months ago

flexdinesh commented 1 year ago

I use @upstash/redis in my vercel serverless functions. I can connect to the databases created in Upstash console.

Looks like the connection API is tailored to work wiht upstash redis server using http REST.

const redis = new Redis({ url: 'UPSTASH_REDIS_REST_URL', token: 'UPSTASH_REDIS_REST_TOKEN' });

I would also like to be able to connect to my local redis server for local development. I don't really like the idea of sending requests over the network for local development. Using a different client for local development is not great either. So is there a way to run an upstash cluster in my local or connect to my local redis server using @upstash/redis?

Eg. CockroachDB lets run their cluster locally in my machine so I don't have to build local only abstractions to work with CockroachDB.

It'd be nice to have something similar while working with Upstash.

chronark commented 1 year ago

You can use https://github.com/mna/upstashdis

flexdinesh commented 1 year ago

Looks like upstashdis is a Go client. @upstash/redis supporting this out of the box would be nice. Asking as a feature request.

chronark commented 1 year ago

upstashdis is also a REST wrapper around redis.

But you would rather use an official solution, correct?

flexdinesh commented 1 year ago

That's correct. Support by the Upstash team means that any breaking releases to @upstash/redis would support both local redis and upstash redis instead of having to wait for a release if I used a third party package.

flexdinesh commented 1 year ago

upstashdis is also a REST wrapper around redis.

upstashdis is good but I'm building with JavaScript. I don't think I can use upstashdis since it's a Go client.

un-focused commented 1 year ago

I have the same issue. I'm going to try coding something to get a patch but first class support would be best!

un-focused commented 1 year ago
export default class Redis {
    client: UpstashRedis | RedisClientType;

    static shared = new Redis();

    constructor() {
        if (process.env.NODE_ENV === 'production') {
            this.client = createClient(
                {
                    url: REDIS_SERVER_URL
                }
            );
        } else {
            this.client = new UpstashRedis(
                {
                    url: UPSTASH_REDIS_SERVER_URL,
                    token: UPSTASH_REDIS_SERVER_TOKEN
                }
            );
        }
    }

    set(key: string, value: string) {
        if (this.client instanceof UpstashRedis) {
            const client = this.client as UpstashRedis;

            return client.set(key, value);
        } else {
            const client = this.client as RedisClientType;

            return client.set(key, value);
        }
    }

    get(key: string) {
        if (this.client instanceof UpstashRedis) {
            const client = this.client as UpstashRedis;

            return client.get(key);
        } else {
            const client = this.client as RedisClientType;

            return client.get(key);
        }
    }
}
un-focused commented 1 year ago

That's the beginnings of a Redis & UpstashRedis abstraction. It suits my needs but feel free to clean it up, add what you need & use it yourself!

un-focused commented 1 year ago

I had the if condition wrong:

if (process.env.NODE_ENV === 'production') {
            this.client = new UpstashRedis(
                {
                    url: UPSTASH_REDIS_SERVER_URL,
                    token: UPSTASH_REDIS_SERVER_TOKEN
                }
            );
        } else {
            this.client = createClient(
                {
                    url: REDIS_SERVER_URL
                }
            );
        }
flexdinesh commented 1 year ago

I tried this last week @un-focused. This solution is good and works well enough as long as we don't need to use @upstash/ratelimit in our code. When we do, the types are not compatible.

It'd be nice to have this officially supported by @upstash/redis so packages like @upstash/ratelimit just work without any extra abstractions.

chronark commented 1 year ago

As I said above, it is a wrapper around a running redis instance to expose an http server compatible with our API. It's not just a client.

upstash-redis-rest-server --addr <ADDR> --redis-addr <ADDR> [--api-token <TOKEN>]

But I do see your point. We'll discuss providing a first party redis wrapper internally.

flexdinesh commented 1 year ago

Oh man I saw Go in their docs and mistook it for a go client. upstashdis is what I was looking for. Thanks for clearing it up @chronark and sorry about the confusion. 😀

donferi commented 1 year ago

Can this be reopened? Having an official wrapper would be ideal. It's hard to rely on a 3rd party tool for a service your org is paying for.

chronark commented 1 year ago

I'm afraid we currently don't have the capacity to build and support this. But I do see the value in an official wrapper

un-focused commented 1 year ago

I could take a look at making one. What are the requirements?

un-focused commented 1 year ago

I made a small one for my personal project a while ago and would be happy to make a new one and submit a Pull Request :)

chronark commented 1 year ago

we have't scoped it out yet

I think we'd like to offer a docker image that has an upstash compatible rest api and talks to any redis instance over tcp

CanRau commented 1 year ago

Would be really helpful for local dev, this would also enable edge-flags locally 🔥 I don't always have good internet here in Perú so I prefer to have everything locally while developing

ogzhanolguncu commented 10 months ago

You can use Serverless Redis HTTP (SRH) to run a local HTTP endpoint for development. (See: https://docs.upstash.com/redis/sdks/javascriptsdk/developing-or-testing)

stolinski commented 10 months ago

Just my two cents here as someone considering upstash for our caching. I'm sure I can just use SRH here, but it is a bummer this isn't a smoother experience. Because now you have to have to have Docker configured or go just to use @upstash/redis locally. Since we aren't using Docker or Go, our install and setup process gets that much more brittle. Not that we're unwilling to add that setup, but there not being an official solution is a turn off of using upstash in general.

ogzhanolguncu commented 10 months ago

Alright, I'll chat with the team about this. We really value our community, so we'll work on making things smoother. Keep an eye out for updates.

DarthBenro008 commented 9 months ago

I've been recently using @upstash/redis, was developing for cloudflare-workers and bumped into similar issues. I ended up creating https://github.com/DarthBenro008/upstash-redis-local

You can simply docker run -p 8000:8000 darthbenro008/upstash-redis-local or install even via homebrew ! No need to setup golang or docker separately.

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 7 months ago

This issue was closed because it has been stalled for 30 days with no activity.

olivergburke commented 6 months ago

I'm working with Upstash for clients at our agency, I agree that this would be very helpful