graphql-hive / graphql-hive/envelop
Support Upstash Redis REST SDK in useResponseCache with Redis cache as store
- Dominant language
- No language data
- Stars
- 827
- Forks
- 132
- PR merge metrics
- No merged PRs in 30d
Description
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:
```ts
// find the responseIds for the entity
const responseIds = await store.smembers(entity);
```
**Describe the solution you'd like**
[Upstash](https://upstash.com) 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](https://github.com/upstash/upstash-redis):
For example (their sample code):
```ts
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**
* [Upstash SDK for Javascript](https://docs.upstash.com/redis/features/javascriptsdk)
* [Discussion on RedwoodJS Discoure topic](https://community.redwoodjs.com/t/guide-power-of-graphql-caching/2624/12?u=dthyresson)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with packages/plugins/response-cache-redis/src/redis-cache.ts and compare its client calls with the Upstash JavaScript SDK examples and REST API documentation linked in the issue. Determine whether Upstash support belongs in this plugin or a separate one, including pipeline support; done means the integration approach and its required tests are defined.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- redis, typescript
- Domain
- api, backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100