question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Upstash Redis REST SDK in useResponseCache with Redis cache as store

See original GitHub issue

Currently, useReponseCache offers a LRU and a Redis cache option.

The redis-cache relies on ioredis as seen here: https://github.com/dotansimha/envelop/blob/cdb32401bc385ca4d3503c4bc6b23ddb1a5909c6/packages/plugins/response-cache-redis/src/redis-cache.ts#L1

  /**
   * Redis instance
   * @see Redis.Redis https://github.com/luin/ioredis
   */
  redis: Redis.Redis;

And then any gets, sets, keys, or smember checks use the Redis client; for example:

    // find the responseIds for the entity
    const responseIds = await store.smembers(entity);

Describe the solution you’d like

Upstash is a serverless database service compatible with Redis® API.

They offer a REST API and a Javascript SDK: https://docs.upstash.com/redis/features/javascriptsdk

The advantage of this over a Redis client in the serverless world (where your GraphQL queries will be done in a Lambda like with RedwoodJS) is that it’s less likely to run into connection limits. Other Redis offerings have 10, 20, 40 connections and base pricing on that where with Upstash the pricing is per usage.

Use upstash-redis in serverless functions if you expect high number of concurrent connections​

Serverless functions scale up fast. This can cause some issues if you need persistent connections. REST based upstash-redis fits better in such cases as it does not require a TCP connection with its stateless design.

Also, the Rest client works well on the Edge in a Netlify or Vercel edge handler/function.

However, because the current cache implementation needs a client to invoke get etc, one cannot easily use the Upstash SDK:

For example (their sample code):

import { auth, set } from '@upstash/redis';

(async () => {
    try {
        auth('UPSTASH_REDIS_REST_URL', 'UPSTASH_REDIS_REST_TOKEN');
        const { data, error } = await set('key', 'value');
        if (error) throw error;
        console.log(data);
        // -> "OK"
    } catch (error) {
        console.error(error);
    }
})();

Notice that here set is a method, but not like store.set() on the client.

Perhaps create another redis cache that uses Upstash specifically?

Would this be part of https://github.com/dotansimha/envelop/tree/main/packages/plugins/response-cache-redis/src or an entirely new plugin just for Upstash?

Other

Or, perhaps there is a way to modify the SDK such that there is a client that can be imported (here? https://github.com/upstash/upstash-redis/blob/master/src/index.ts) and then the plugin can have as is?

  • Question: The REST API supports pipeline but not 100% certain if the SDK does.

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
chronarkcommented, May 24, 2022

Hey @dthyresson The @upstash/redis api is not compatible with ioredis, because we wanted to provide a nicer typescript experience, however it would be pretty easy to write a small adapter.

Judging from the readme example, I should probably just create another package that exports createUpstashCache(..)

So you would use it like this:

import { envelop } from '@envelop/core';
import { useResponseCache } from '@envelop/response-cache';
import { createUpstashCache } from '@envelop/response-cache-upstash';
import { Redis } from "@upstash/redis"

const cache = createUpstashCache(Redis.fromEnv());

const getEnveloped = envelop({
  plugins: [
    // ... other plugins ...
    useResponseCache({ cache }),
  ],
});

Please let me know what you think and I’ll implement it afterwards

2reactions
dthyressoncommented, Jun 13, 2022

Hey @dthyresson I’ve created a draft PR #1404 Maybe you can try and figure out why the tests are not working yet. I’ll retry as well sometime this week.

Hi. I’ll try too look too as well but Tim from the RedwoodJS community has offered to help as well; see: https://community.redwoodjs.com/t/guide-power-of-graphql-caching/2624/21?u=dthyresson

Read more comments on GitHub >

github_iconTop Results From Across the Web

REST API | Upstash: Documentation
Using Redis protocol, you can benefit from the rich Redis ecosystem. For example, you can directly use your Upstash database as session cache...
Read more >
[Guide] Power of GraphQL Caching - #21 by tctrautman - Solutions ...
I always seem to be able to store the cache in Redis but am not able to actually fetch data correctly? I never...
Read more >
How to cache REST API responses Using Redis & NodeJS
Pre-requisite​ · brew install node · git clone https://github.com/redis-developer/basic-caching-demo-nodejs · - REDIS_ENDPOINT_URI: Redis server ...
Read more >
@upstash/redis - NPM Package Overview - Socket
An HTTP/REST based Redis client built on top of Upstash REST API. Version: 1.18.4 was published by chronark. Start using Socket to analyze ......
Read more >
Caching NextJS Apps with Serverless Redis using Upstash
One of the best things about Redis is that we can persist data in a database that can continuously store them unless we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found