Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Redis Hash Length?

Tags:

redis

I store objects using hashes on Redis and I would like to calculate the length of these Hashes from Redis point of view.

You can do this easily for strings using STRLEN.

But I simply can not find a suitable command for the hash data type in the documentation. It seems to be the same thing for lists or sets.

Basically, the only solution that I found is to get the whole hash with HGETALL and to calculate the length on client size.

Is is something completely out of the box ?

If I'm wrong, please do not hesite to explain me why or give me relevant links/posts/SO questions.

Edit :

HLEN is not a solution because it "Returns the number of fields contained in the hash". I want to calculate this size for Capacity Planning & Active Monitoring on Redis database.

like image 795
Cybermaxs Avatar asked May 22 '13 12:05

Cybermaxs


3 Answers

Just use HLEN.

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HLEN myhash
(integer) 2

EDIT: The question was clarified to be that the OP wants the size on disk of a hash for active monitoring. In that case, I'd definitely go with a Lua script that calculates the size of a hash on your server and returns the value back to you. Do not use HGETALL if you're expecting big hashes, because you'll need to transfer that entire hash from your server to your client computer, and that's going to become your bottleneck really quickly. Just doing this calculation on the Redis server with Lua means you get to just transfer an int of the number of bytes of your network instead of possibly mb of data for your entire hash.

like image 51
Eli Avatar answered Sep 27 '22 00:09

Eli


Use the MEMORY USAGE command.

127.0.0.1:6379> hmset user:1000 username antirez birthyear 1977 verified 1 
OK
127.0.0.1:6379> memory usage user:1000 
(integer) 110

As you are using this for capacity planing, this command has the advantage of including any administrative overhead that redis adds on top of the size of your data itself.

127.0.0.1:6379> SET "" ""
OK
127.0.0.1:6379> MEMORY USAGE ""
(integer) 46
like image 35
dan carter Avatar answered Sep 26 '22 00:09

dan carter


Depends on what you want to do with the length of the hash.

If you want the length to do some diagnostics or monitoring, like finding the memory consumed, then I'd suggest you do it offline using a tool like redis-rdb-tools (disclaimer: I am the author of this tool). The csv dump file will get you statistics about each key - including total size, total memory consumed and so on.

But if you want the size to implement some application feature, then there isn't a readymade solution. HGETALL plus client size calculation of the length is the way to go. You can perhaps optimize by writing a lua script so that the length calculation happens on the redis server itself.

like image 34
Sripathi Krishnan Avatar answered Sep 25 '22 00:09

Sripathi Krishnan