{
  "schema_version": 2,
  "id": "develop/clients/ioredis/connect",
  "title": "Connect to the server",
  "url": "https://redis.io/docs/latest/develop/clients/ioredis/connect/",
  "summary": "Connect your Python application to a Redis database",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-10-12T14:48:04+01:00",
  "page_type": "content",
  "content_hash": "ca5bded6b210e5a14c39b52949c9b5b33db10d0bfd632a2dd7cf94a6468d9fac",
  "sections": [
    {
      "id": "basic-connection",
      "title": "Basic connection",
      "role": "overview",
      "text": "Connect to localhost on port 6379:\n\n[code example]\n\nYou can also specify a full set of connection options:\n\n[code example]\n\n> [!NOTE]\n> ioredis supports [connection URLs](https://redis.io/docs/latest/develop/clients/connection-urls#ioredis-javascript) through the `Redis` constructor.\n> A URL can set the host, port, credentials, database, and whether to use TLS,\n> so you can use one instead of passing those options separately.\n\nStore and retrieve a simple string.\n\n[code example]"
    },
    {
      "id": "connect-to-a-redis-cluster",
      "title": "Connect to a Redis cluster",
      "role": "content",
      "text": "To connect to a Redis cluster, use `Redis.Cluster()`, passing an array of\nendpoints.\n\n[code example]"
    },
    {
      "id": "connect-to-redis-sentinel",
      "title": "Connect to Redis Sentinel",
      "role": "content",
      "text": "To connect through [Redis Sentinel](https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel),\npass a list of Sentinels in the `sentinels` option and the name of the primary that the\nSentinels monitor in `name`. The client asks the Sentinels for the current primary.\n\n[code example]\n\nThe Sentinels and the data nodes have separate credentials. Set `sentinelPassword` (and\n`sentinelUsername`, if needed) for the Sentinels, and `username` and `password` for the\nprimary and replicas:\n\n[code example]\n\nIf the Sentinel password is wrong or missing, the error says that all Sentinels are\nunreachable.\n\nBy default, ioredis doesn't listen for failover announcements from the Sentinels. After a\nfailover, the client keeps using the old primary until Sentinel reconfigures it as a\nreplica and closes the connection. Any writes that the old primary accepts during that time\nare lost. Set `failoverDetector: true` to switch to the new primary as soon as the Sentinels\nannounce it.\n\nTo connect to a replica instead of the primary, set `role: \"slave\"`. The client connects to\none replica, chosen at random. Replication is asynchronous, so a replica can return stale\ndata.\n\n[code example]"
    },
    {
      "id": "connect-to-your-production-redis-with-tls",
      "title": "Connect to your production Redis with TLS",
      "role": "content",
      "text": "When you deploy your application, use TLS and follow the [Redis security](https://redis.io/docs/latest/operate/oss_and_stack/management/security) guidelines.\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "basic-connection-ex0",
      "language": "js",
      "code": "const redis = new Redis();",
      "section_id": "basic-connection"
    },
    {
      "id": "basic-connection-ex1",
      "language": "js",
      "code": "const redis = new Redis({\n  port: 6379,\n  host: \"127.0.0.1\",\n  username: \"default\",\n  password: \"my-password\",\n  db: 0,\n});",
      "section_id": "basic-connection"
    },
    {
      "id": "basic-connection-ex2",
      "language": "js",
      "code": "await redis.set('foo', 'bar');\nconst value = await redis.get('foo');\nconsole.log(value); // >>> bar",
      "section_id": "basic-connection"
    },
    {
      "id": "connect-to-a-redis-cluster-ex0",
      "language": "js",
      "code": "const redis = new Redis.Cluster([\n    {\n        host: '127.0.0.1',\n        port: 6380,\n        password: 'my-password',\n        username: 'default',\n    },\n    {\n        host: '127.0.0.1',\n        port: 6381,\n        password: 'my-other-password',\n        username: 'default',\n    },\n    // ...\n]);",
      "section_id": "connect-to-a-redis-cluster"
    },
    {
      "id": "connect-to-redis-sentinel-ex0",
      "language": "js",
      "code": "const redis = new Redis({\n  sentinels: [\n    { host: \"localhost\", port: 26379 },\n    { host: \"localhost\", port: 26380 },\n    { host: \"localhost\", port: 26381 },\n  ],\n  name: \"mymaster\",\n});\n\nawait redis.set('foo', 'bar');\nconst value = await redis.get('foo');\nconsole.log(value); // >>> bar",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-redis-sentinel-ex1",
      "language": "js",
      "code": "const redis = new Redis({\n  sentinels: [\n    { host: \"localhost\", port: 26379 },\n    { host: \"localhost\", port: 26380 },\n    { host: \"localhost\", port: 26381 },\n  ],\n  name: \"mymaster\",\n  // Credentials for the Sentinel instances.\n  sentinelPassword: \"my-sentinel-password\",\n  // Credentials for the primary and replica data nodes.\n  username: \"default\",\n  password: \"my-password\",\n});",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-redis-sentinel-ex2",
      "language": "js",
      "code": "const replica = new Redis({\n  sentinels: [{ host: \"localhost\", port: 26379 }],\n  name: \"mymaster\",\n  role: \"slave\",\n});",
      "section_id": "connect-to-redis-sentinel"
    },
    {
      "id": "connect-to-your-production-redis-with-tls-ex0",
      "language": "js",
      "code": "const redis = new Redis({\n  host: \"localhost\",\n  //...\n  tls: {\n    key: readFileSync('./redis_user_private.key'),\n    cert: readFileSync('./redis_user.crt'),\n    ca: fs.readFileSync('./redis_ca.pem'),\n  },\n});",
      "section_id": "connect-to-your-production-redis-with-tls"
    }
  ]
}
