From 47c60ab863308be80657774efd6a60ac83f0ff54 Mon Sep 17 00:00:00 2001 From: Brody Over <10548119+brody192@users.noreply.github.com> Date: Fri, 20 Dec 2024 17:01:41 -0500 Subject: [PATCH] Update enotfound-redis-railway-internal.md --- .../enotfound-redis-railway-internal.md | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/docs/reference/errors/enotfound-redis-railway-internal.md b/src/docs/reference/errors/enotfound-redis-railway-internal.md index ecef3d7e8..93a1fcc67 100644 --- a/src/docs/reference/errors/enotfound-redis-railway-internal.md +++ b/src/docs/reference/errors/enotfound-redis-railway-internal.md @@ -8,7 +8,7 @@ The error code `ENOTFOUND` means that your application could not resolve the `re ## Why This Error Can Occur -The main reason this error can occur is because your application uses the [`ioredis`](https://www.npmjs.com/package/ioredis) package to connect to the Redis database, or uses a package that uses ioredis as a dependency such as [`bullmq`](https://docs.bullmq.io/). +This error can occur for a few different reasons, but the main reason this error can occur is because your application uses the [`ioredis`](https://www.npmjs.com/package/ioredis) package to connect to the Redis database, or uses a package that uses ioredis as a dependency such as [`bullmq`](https://docs.bullmq.io/). By default, ioredis will only do an IPv4 (A record) lookup for the `redis.railway.internal` hostname. @@ -16,12 +16,24 @@ That presents a problem given that Railway's private network uses only IPv6 (AAA The lookup will fail because the A records for `redis.railway.internal` do not exist. +Some other reasons that this error can occur would be - + +- Your application and Redis database are in different projects. + +- You are trying to connect to a Redis database locally with the private hostname and port. + +For either of these reasons, the issue arises because the private network is scoped to a single environment within a [project](https://docs.railway.com/overview/the-basics#project--project-canvas), and would not be accessible from your local machine or other projects. + +If the Redis database is in the same project as your application, and you are not trying to connect to a Redis database locally, `ioredis` is the likely cause of the error. + ## Solutions The solution depends on the package you are using to connect to the Redis database, though the solution is the same for both. ### ioredis +#### Using ioredis directly in your application + `ioredis` has an option to do a dual stack lookup, which will try to resolve the `redis.railway.internal` hostname using both IPv4 and IPv6 addresses (A and AAAA records). To enable this, in your `REDIS_URL` environment variable, you can set the `family` to `0` to enable dual stack lookup. @@ -34,7 +46,7 @@ const redis = new Redis(process.env.REDIS_URL + '?family=0'); const ping = await redis.ping(); ``` -### bullmq +#### Using bullmq Similarly, for `bullmq` since it uses `ioredis` as a dependency, you can set the `family` option to `0` in your connection object. @@ -58,8 +70,28 @@ const jobs = await queue.getJobs(); console.log(jobs); ``` -### Other +#### Other packages Above we covered the two most common packages that can cause this error, but there are other packages that use `ioredis` as a dependency that may also cause this error. If you are using a package that uses `ioredis` as a dependency, you can try to find a way to set the `family` option to `0` either in your connection object or in your `REDIS_URL` environment variable. Similar to the examples above. + +### Redis database in a different project + +Create a [new Redis database](https://docs.railway.com/guides/redis) in the same [project](https://docs.railway.com/overview/the-basics#project--project-canvas) as your application, and connect it to the Redis database using the private network as shown in the examples above. + +Read about best pracices to get the most out of the platform [here](/overview/best-practices). + +### Connecting to a Redis database locally + +The easiest way to connect to a Redis database locally is to use the public network. + +You can do this is by using the `REDIS_PUBLIC_URL` environment variable to connect to the Redis database. + +```js +import Redis from 'ioredis'; + +const redis = new Redis(process.env.REDIS_PUBLIC_URL); + +const ping = await redis.ping(); +```