0

How come that even when only one instance of Redis connection created, every time I call publish or subscribe on that instance, it counts it like another client. So when I connect to redis using python

import redis
redis_server = redis.Redis()

it does not recognize it as new client. Only when I call one of these

redis_server.publish("channel", message) 
redis_server.subscribe("channel")

I can see that there are 2 clients connected. Are the pub/sub clients treated seperately in redis? Why not registering connected client when the new connection is open?

ammar
  • 64
  • 6
  • What do you mean by "it does not recognize"? Recognize how? – Bailey Parker Mar 15 '19 at 19:05
  • Most likely the client library is opening a new connection from a pool. – Klaus D. Mar 15 '19 at 19:06
  • When I run INFO CLIENTS in redis-cli it doesn't show me that there is connected client if I run redis_server = redis.Redis(). However when I run publish or subscribe it shows 3 clients (including redis-cli) – ammar Mar 15 '19 at 19:07
  • Ah, is this true only for the pub/sub command or is this true for any command? – Bailey Parker Mar 15 '19 at 19:23
  • I think only for pub/sub command, since I run "get" and "set" as well and they are not showing as clients. – ammar Mar 15 '19 at 19:27
  • It seems that it creates new connection for every command. Whenever it calls _execute_comand it creates new connection. – ammar Mar 15 '19 at 19:51

1 Answers1

0

By default redis-py gives you get a connection pool with only a maximum number of connections. On the first command you issue a real connection will be made and you'll see it appear in the CLIENT LIST on the server.

Whenever any client library for Redis issues a subscribe command, that entire connection is occupied by this, so redis-py is probably creating a separate connection dedicated to this.

This should explain why you see no clients connected, then 2. It's not necessarily 1 connection for every command issued as the connections in the pool will be reused.

nnog
  • 1,607
  • 16
  • 23