0

I'm new to Redis. How can I get the memory footprint of a specific key in redis?

db0

  1) "unacked_mutex"
  2) "_kombu.binding.celery"
  3) "_kombu.binding.celery.pidbox"
  4) "_kombu.binding.celeryev"

I just want to get the memory footprint of one specific key like "_kombu.binding.celery" , or one specific db like db0 , how can I get it?

redis_version: 2.8.17

Any commentary is very welcome. great thanks.

jia Jimmy
  • 1,693
  • 2
  • 18
  • 38

2 Answers2

3

You are running a very old version of redis. The MEMORY command is not available in that version, so there is no precise way of getting at this information. However, you can approximate this information using the DUMP command.

Simply call DUMP _kombu.binding.celery and save the results to a file. The result is some characters and escape sequences. When you load this file into an environment like node, you can look at the length of the string and multiply by 2 to get the number of bytes. This is not precise, but it will give you a generally close approximation.

Here's what you could do:

in Redis:

$ redis-cli
127.0.0.1:6379> hset c 123 456
(integer) 0
127.0.0.1:6379> dump c
"\r\x12\x12\x00\x00\x00\r\x00\x00\x00\x02\x00\x00\xfe{\x03\xc0\xc8\x01\xff\t\x00\x10\xd4L \x908\x8b2"

in Node:

$ node
> a="\r\x12\x12\x00\x00\x00\r\x00\x00\x00\x02\x00\x00\xfe{\x03\xc0\xc8\x01\xff\t\x00\x10\xd4L \x908\x8b2"
'\r\u0012\u0012\u0000\u0000\u0000\r\u0000\u0000\u0000\u0002\u0000\u0000þ{\u0003ÀÈ\u0001ÿ\t\u0000\u0010ÔL 82'
> a.length
30

This is close to half of the actual amount that redis provides with MEMORY USAGE:

127.0.0.1:6379> MEMORY USAGE c
(integer) 63
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
2

MEMORY USAGE _kombu.binding.celery would give you the number of bytes that a key and value require to be stored in RAM.

Here is the doc for the command.

TheDude
  • 3,796
  • 2
  • 28
  • 51
  • thanks for your reply, tried but got `(error) ERR unknown command 'MEMORY'` for my redis version is `2.8.17` – jia Jimmy Mar 04 '19 at 05:24
  • You could do `DEBUG OBJECT key1` instead. It returns the `serializedlength`, which I think is the size of the object when it is saved to the RDB file (from https://stackoverflow.com/a/7649466/509710). Note that `DEBUG` commands are disabled for AWS ElasticCache. – TheDude Mar 04 '19 at 13:33