Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from ioredis with keyv on ApolloServer #2825

Open
mirankorkmaz opened this issue Aug 27, 2024 · 1 comment
Open

Migrating from ioredis with keyv on ApolloServer #2825

mirankorkmaz opened this issue Aug 27, 2024 · 1 comment

Comments

@mirankorkmaz
Copy link

Hi, I'm migrating my implementation from ioredis to this package instead. Current implementation looks kinda like:

import Keyv from 'keyv';
import KeyvRedis from '@keyv/redis';
import Redis from 'ioredis';
import { KeyvAdapter } from '@apollo/utils.keyvadapter';

  const instances =
    config.redis.host?.split(',').map((data) => {
      const [host, port] = data.split(':');
      return { host, port: Number.parseInt(port) };
    });

  const createTheCache = () => {
    const redis = new Redis.Cluster(instances, {
      scaleReads: 'slave',
    });
    return new Keyv({
      store: new KeyvRedis(redis),
      namespace: 'my-namespace',
    });
  };

const server = new ApolloServer({
    cache: new KeyvAdapter(createTheCache(), {
      disableBatchReads: true, 
    }),
  })
  

I've tried using createCluster.

import Keyv from 'keyv';
import KeyvRedis from '@keyv/redis';
import { createCluster from 'redis';
import { KeyvAdapter } from '@apollo/utils.keyvadapter'; 


  const instances =
    config.redis.host?.split(',').map((data) => {
      const [host, port] = data.split(':');
      return { host, port: Number.parseInt(port) };
    });

  const createTheCache = async () => {
    const client = createCluster({
      rootNodes: instances.map(({ host, port }) => ({
        url: `redis://${host}:${port}`
      })),
    });
  
    client.on('error', (err: any) => { console.log("Fail!", err )});
  
    await client.connect();
  
    const store = new KeyvRedis(client as any);
    return new Keyv({
      store,
      namespace: 'my-namespace',
    });
  };

 const server = new ApolloServer({
    cache: new KeyvAdapter(await createTheCache(), {
      disableBatchReads: true, 
    }),
  )}

However I'm getting an error

| Error: connect ECONNREFUSED 127.0.0.1:6379
|     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16)
| Emitted 'error' event on Keyv instance at:
|     at KeyvRedis.<anonymous> (/var/www/app/node_modules/keyv/src/index.js:60:46)
|     at KeyvRedis.emit (node:events:519:28)
|     at KeyvRedis.emit (node:domain:488:12)
|     at EventEmitter.<anonymous> (/var/www/app/node_modules/@keyv/redis/src/index.js:18:40)
|     at EventEmitter.emit (node:events:519:28)
|     at EventEmitter.emit (node:domain:488:12)
|     at EventEmitter.silentEmit (/var/www/app/node_modules/ioredis/built/Redis.js:482:30)
|     at Socket.<anonymous> (/var/www/app/node_modules/ioredis/built/redis/event_handler.js:196:14)
|     at Object.onceWrapper (node:events:634:26)
|     at Socket.emit (node:events:531:35)
|     at Socket.emit (node:domain:488:12)
|     at emitErrorNT (node:internal/streams/destroy:169:8)
|     at emitErrorCloseNT (node:internal/streams/destroy:128:3)
|     at processTicksAndRejections (node:internal/process/task_queues:82:21) {
|   errno: -111,
|   code: 'ECONNREFUSED',

Has anyone tried migrating from ioredis on apolloServer before, what am I missing?

@leibale
Copy link
Collaborator

leibale commented Sep 19, 2024

From the error stack trace it seems like the error is coming from ioredis (and not node-redis).. If you want we can debug this together, just ping me on the redis discord (my handle is @leibale)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants