n1ru4l/graphql-live-query

How to use with Redis?

yayvery opened this issue · 6 comments

The readme says:

For scaling horizontally the independent InMemoryLiveQueryStore instances could be wired together via a PubSub system such as Redis.

Could you give an example for how to do this?

You can create a Redis PubSub topic to which you emit the invalidated resources. On the single application instances you subscribe to these topics and invoke the local InMemoryLiveQueryStore.invalidate.

Pseudo Code:

redis.subscribe("LIVE_QUERY_INVALIDATIONS", resourceIdentifier => {
  inMemorLiveQueryStore.invalidate(resourceIdentifier);
});

redis.publish("LIVE_QUERY_INVALIDATIONS", "Post:2");

You can create a Redis PubSub topic to which you emit the invalidated resources. On the single application instances you subscribe to these topics and invoke the local InMemoryLiveQueryStore.invalidate.

Pseudo Code:

redis.subscribe("LIVE_QUERY_INVALIDATIONS", resourceIdentifier => {
  inMemorLiveQueryStore.invalidate(resourceIdentifier);
});

redis.publish("LIVE_QUERY_INVALIDATIONS", "Post:2");

Thanks, simpler than I expected! Would you consider adding a RedisLiveQueryStore that takes care of this automatically?

I tried to avoid it for now. There are both ioredis and redis which are heavily used. Which one should the implementation support? Both? Only one?

Also, note that this approach is not a true RedisLiveQueryStore approach. The resource identifiers are still stored within memory, not Redis, we basically only have a bridge that broadcasts invalidations to all connected InMemoryLiveQueryStore instances.

If this should be a seperate package it should probably be an API like const bridgedLiveQueryStore = withRedisBridge(inMemoryLiveQueryStore).

A true RedisLiveQueryStore would probably store all the information's about the subscribed resources in Redis.

@n1ru4l sure thing, I created a PR

@avesoceans I appreciate it's been a while but I'm on a quest to teach others how to use Redis with Live Queries. I noticed in your example it's mostly using Redis for invalidation but not the actual storage of values, or is that incorrect?

Would you be interested in collaborating on this further?