title: Redis sentinel not connect to master: Error: READONLY You can't write against a read only slave?
body: <p>I have this <code>redis replication</code> with <code>sentinel</code> architect:</p>
  
  <p>5 servers: <code>.1</code>, <code>.2</code>, <code>.3</code>, <code>.4</code> and <code>.5</code></p>
  
  <ul>
  <li><code>.1</code>: nodejs app, redis master, redis sentinel</li>
  <li><code>.2</code>: nodejs app, redis slave</li>
  <li><code>.3</code>: nodejs app, redis slave</li>
  <li><code>.4</code>: redis sentinel</li>
  <li><code>.5</code>: redis sentinel</li>
  </ul>
  
  <p>Sentinel handle failover as expected. 
  <code>redis-cli -h x.y.z.5 -p 26379 sentinel get-master-addr-by-name mymaster</code> is always return <code>.1</code> server.</p>
  
  <p>My code to connect to this replication (I use <a href="https://github.com/luin/ioredis" rel="noreferrer">ioredis</a>):</p>
  
  <pre><code>let sentinels = [
    { host: process.env.REDIS_SENTINEL_1, port: process.env.REDIS_PORT },
    { host: process.env.REDIS_SENTINEL_2, port: process.env.REDIS_PORT },
    { host: process.env.REDIS_SENTINEL_3, port: process.env.REDIS_PORT }
  ]
  store = new Redis({
    name: 'mymaster',
    sentinels: sentinels
  })
  store.on('ready', () =&gt; {
    store.set('foo', new Date())
    store.get('foo', function (err, result) {
      console.log('redis: get foo: ', result)
    });
  });
  </code></pre>
  
  <p>Then, <code>.1</code> nodejs app run ok, but in <code>.2</code> and <code>.3</code>, the error occurs with this log:</p>
  
  <pre><code> events.js:161
         throw er; // Unhandled 'error' event
         ^
  
   Error: READONLY You can't write against a read only slave.
  </code></pre>
  
  <p>P/s: my redis servers are binding in both server IP and <code>127.0.0.1</code>. Because, without <code>127.0.0.1</code>, I meet this error:</p>
  
  <pre><code>Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
  </code></pre>
  
  <p>So, I guess that, my app isn't connect to <code>redis server</code> via <code>sentinel</code>, but it connect directly with <code>localhost</code>.</p>
  
  <p>Any help are appriciate!</p>
  
  <p>Update: log when I turn on the <code>DEBUG=ioredis:*</code>:</p>
  
  <pre><code>2|MyApp     | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis status[x.y.z.5:26379]: connecting -&gt; connect
  2|MyApp     | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis status[x.y.z.5:26379]: connect -&gt; ready
  2|MyApp     | Wed, 15 Mar 2017 12:58:42 GMT ioredis:connection send 1 commands in offline queue
  2|MyApp     | Wed, 15 Mar 2017 12:58:42 GMT ioredis:redis write command[0] -&gt; sentinel(get-master-addr-by-name,mymaster)
  2|MyApp     | events.js:161
  2|MyApp     |       throw er; // Unhandled 'error' event
  2|MyApp     |       ^
  2|MyApp     | Error: READONLY You can't write against a read only slave.
  2|MyApp     |     at JavascriptReplyParser._parseResult (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:43:16)
  2|MyApp     |     at JavascriptReplyParser.try_parsing (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:114:21)
  2|MyApp     |     at JavascriptReplyParser.run (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:126:22)
  2|MyApp     |     at JavascriptReplyParser.execute (/home/demo/demo_api/source/node_modules/redis/lib/parsers/javascript.js:107:10)
  2|MyApp     |     at Socket.&lt;anonymous&gt; (/home/demo/demo_api/source/node_modules/redis/index.js:131:27)
  2|MyApp     |     at emitOne (events.js:96:13)
  2|MyApp     |     at Socket.emit (events.js:189:7)
  2|MyApp     |     at readableAddChunk (_stream_readable.js:176:18)
  2|MyApp     |     at Socket.Readable.push (_stream_readable.js:134:10)
  2|MyApp     |     at TCP.onread (net.js:551:20)
  2|MyApp     | [nodemon] app crashed - waiting for file changes before starting...
  2|MyApp     | sentinel production
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[localhost:6379]: [empty] -&gt; connecting
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[x.y.z.5:26379]: [empty] -&gt; connecting
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis queue command[0] -&gt; sentinel(get-master-addr-by-name,mymaster)
  2|MyApp     | sentinel production
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[localhost:6379]: [empty] -&gt; connecting
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis status[x.y.z.5:26379]: [empty] -&gt; connecting
  2|MyApp     | Wed, 15 Mar 2017 12:58:43 GMT ioredis:redis queue command[0] -&gt; sentinel(get-master-addr-by-name,mymaster)
  </code></pre>
  
  <p>Update 2: one more thing, I run the app in my local machine with the <code>production</code> environment and everything work like charm!!!</p>