0

On Windows Server 2019, under WSL I ran by mistake redis-server a 2nd time, and it didn't report a failure in binding the listening socket. Here's the output of netstat:

C:\Users\Administrator>netstat -ano | grep 6379 | grep LISTEN
  TCP    0.0.0.0:6379           0.0.0.0:0              LISTENING       11080
  TCP    127.0.0.1:6379         0.0.0.0:0              LISTENING       13292
  TCP    [::]:6379              [::]:0                 LISTENING       11080
  TCP    [::1]:6379             [::]:0                 LISTENING       13292

Two questions:

  1. How is that even possible?
  2. How can I select which instance to connect to using redis-cli?

Thanks.

EDIT

I ran it a 3rd time just to check, and here's the netstat output:

C:\Users\Administrator>netstat -ano | grep 6379 | grep LISTEN
  TCP    0.0.0.0:6379           0.0.0.0:0              LISTENING       13916
  TCP    0.0.0.0:6379           0.0.0.0:0              LISTENING       11080
  TCP    127.0.0.1:6379         0.0.0.0:0              LISTENING       13292
  TCP    [::]:6379              [::]:0                 LISTENING       11080
  TCP    [::]:6379              [::]:0                 LISTENING       13916
  TCP    [::1]:6379             [::]:0                 LISTENING       13292

These are the process entries from task manager: enter image description here

2
  • Use netstat's -o argument to see the Process IDs
    – Attie
    Commented Jul 15, 2020 at 13:02
  • Updated the question, there are two process ids
    – Aviad P.
    Commented Jul 15, 2020 at 13:20

1 Answer 1

1

If the socket has the SO_REUSEADDR flag set, then multiple processes will be able to bind to a given port.

"Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE"

Using SO_REUSEADDR

The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket. The second socket calls setsockopt with the optname parameter set to SO_REUSEADDR and the optval parameter set to a boolean value of TRUE before calling bind on the same port as the original socket.

This is intended to allow services to share the load among a number of independent processes, and should only be used if the service is able to handle this correctly... Redis may well be among the services that support this (and enable it by default).

See net.c:

static int redisSetReuseAddr(redisContext *c) {
    int on = 1;
    if (setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
        redisNetClose(c);
        return REDIS_ERR;
    }
    return REDIS_OK;
}

static int redisCreateSocket(redisContext *c, int type) {
    redisFD s;
    if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) {
        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
        return REDIS_ERR;
    }
    c->fd = s;
    if (type == AF_INET) {
        if (redisSetReuseAddr(c) == REDIS_ERR) {
            return REDIS_ERR;
        }
    }
    return REDIS_OK;
}

If you need to deremine which server you've connected to, you can use the Redis command INFO server, and look for the process_id field.

Alternatively, you can use netstat -ano on Windows (netstat -tnp on Linux) to determine the server's PID for a give client port (not client PID though).

I don't believe that there is a simple OS-level method to determine this relationship.


If you have multiple server instances using different configuration and/or data for each, then you have a configuration error that you should rectify - i.e: don't run multiple services at once, or use a different port for each.

This isn't something that Redis is responsible for, much like choosing the listen port isn't something that Redis is responsible for.

7
  • When I use redis-cli it always connectes to the same one tho. Or does it? How can I tell where cli connected to? And what about the dump file?
    – Aviad P.
    Commented Jul 15, 2020 at 13:31
  • The same what? Server process? That's possible, I don't believe there are any strict rules about how the requests are distributed (e.g: round-robin)
    – Attie
    Commented Jul 15, 2020 at 13:32
  • "How can I tell where cli connected to?" - There may be a way to retrieve the Redis server's PID, or you'll need to interrogate the server process to determine if a given client is connected... I don't think that will be a simple OS-level task.
    – Attie
    Commented Jul 15, 2020 at 13:33
  • When I use redis-cli and run info Server one of the variables is process_id and it is always process_id:73 regardless of how many cli's I run in parallel... Also, what about redis.conf and the dump file? The other two instances use a different redis.conf file
    – Aviad P.
    Commented Jul 15, 2020 at 13:37
  • I don't know what "dump file" you're referring to (no mention in your question)... if there are multiple server instances using different configuration and/or data, then you have a configuration error that you should rectify - i.e: don't run multiple services at once, or use a different port for each.
    – Attie
    Commented Jul 15, 2020 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .