4

Is there a way to check how many current connections there are to the Redis database?

const redis = require('redis');

var client = redis.createClient(port, host);

client.on('connect', () => {
  //Display how many current connections are to Redis
  console.log( numberOfRedisClients );
});
Vardan Betikyan
  • 354
  • 5
  • 20

2 Answers2

3

according to redis documentation try this :

const Redis = require("ioredis");
let client =  new Redis();
async function my_function(){
  console.log(await client.send_command('CLIENT', 'LIST'))

}

the output :

id=24 addr=127.0.0.1:47962 fd=8 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=info
id=25 addr=127.0.0.1:47964 fd=9 name= age=0 idle=0 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=26 addr=127.0.0.1:47966 fd=10 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • I added a function where it goes through the string letter by letter and accumulates the number of 'clients' there are to a number. Seems a bit sloppy and excessive, but if there is no other way, I guess it will have to do – Vardan Betikyan Dec 06 '20 at 11:27
2

CLIENT LIST returns information and statistics about the client connections server in a mostly human readable format as @babak stated in the answer.

If you just need to get total number of connections you may use INFO command. INFO clients will print something like this and connected_clients would be what you are looking for.

# Clients
connected_clients:2
client_recent_max_input_buffer:8
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

The following code do the work;

const Redis = require("ioredis");
const redis = new Redis();

async function clients() {
    console.log(await redis.info('clients'));
}

clients();
Ersoy
  • 8,816
  • 6
  • 34
  • 48
  • 1
    tnx I didn't know that redis.info('clients') could work too. – Babak Abadkheir Dec 06 '20 at 10:59
  • By my testing, the 'connected_clients' doesn't lose count when a connection is lost. It gives the total number of connections it's ever received, which doesn't give the current connection count – Vardan Betikyan Dec 06 '20 at 11:29
  • 1
    @VardanBetikyan i tested on 3 different production redis cluster to see it(all has active > 200 clients connected) - it is not total number since the beginning. Also this answer confirms it https://stackoverflow.com/questions/58847716/what-is-the-difference-of-redis-client-list-with-connected-client-from-info – Ersoy Dec 06 '20 at 11:33
  • 2
    @VardanBetikyan also `info stats` command will print stats and there is a line called `total_connections_received` that is the `Total number of connections accepted by the server`. What you are referring may be that one. In my case it is `478317479`. – Ersoy Dec 06 '20 at 11:36
  • Weird. I have a cluster of 8 cpus connecting to redis. One is showing me 17 connected_clients, the rest are all 40 connected_clients.Every time I restart the server, the first number increments to 18, and the rest stay on a constant 40. Doesn't make sense – Vardan Betikyan Dec 06 '20 at 11:37
  • 1
    `connected_clients` exclude replica connections - so the equation could be (total lines in the `info clients` - `connected_slaves`(that you can get from `info replication`) = `connected_clients`) @VardanBetikyan – Ersoy Dec 06 '20 at 11:41
  • Kind of figured it out -- `connected_clients` is showing me the total number of lines in `send_command('CLIENT', 'LIST')`, which is mainly composed of `cmd=info` types. On my first connection, it shows 1 `cmd=client`, 4 `cmd=subscribe`, 15 `cmd=info` and 4 `cmd=NULL` types, which add up to 24. which causes `connected_clients` to display 24, instead of just 1. Any idea why? -- E.g. in babak's example, it will return `connected_clients` 3, counting all the lines for some reason – Vardan Betikyan Dec 06 '20 at 12:51
  • 1
    @VardanBetikyan Lets say you have a single application server connecting to the redis server. If you don't close your connections (when you are done with the redis) then it is normal to see same ip address multiple times in the `client list`. Even you close your connection in some times you can see it(depending on your traffic traffic or usecase). There can be some `idle` clients in your list and they will stay there until the connection is closed by application or the `timeout` value of your redis configuration is exceed. https://redis.io/topics/clients#client-timeouts – Ersoy Dec 06 '20 at 13:31
  • 1
    I've restarted the redis server and used flushall, still giving me the same thing. The `cmd=client` count is correct. Though `connected_clients` is also counting the other `cmd` types (info, subscribe, NULL) as client connections – Vardan Betikyan Dec 06 '20 at 22:54
  • 1
    Like in babak's method, the `connected_clients` would return 3 since there are 3 lines, even though there is only 1 client – Vardan Betikyan Dec 06 '20 at 23:19
  • 1
    I'll accept this answer since it seems to work for everyone else. Albeit I will be using a different method to accomplish what I'm aiming for. Thank you very much for your help! – Vardan Betikyan Dec 07 '20 at 00:13