Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Redis "used_memory_peak" stat

Tags:

redis

I'm using Redis (2.4.2) and with the INFO command I can read stats about my Redis server.

There are many stats, including some about how much memory is used. And one is "used_memory_peak" that seems to hold the maximum amount of memory Redis has ever taken.

I've deleted a bunch of key, and I'd like to reset this stat since it affects the scale of my Munin graphs.

There is a CONFIG RESETSTAT command, but it doesn't seem to affect this particular stat.

Any idea how I could do this, without having to export/delete/import my dataset ?

EDIT :

According to @antirez himself (issue 369 on GitHub), this is an intended behavior, but it this feature could be improved to be more useful in a future release.

like image 330
jlecour Avatar asked Mar 02 '12 15:03

jlecour


People also ask

How do I delete all Redis keys?

There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands. The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.

How do I know if my Redis is healthy?

you can do it by this way. $redis = new Redis(); $redis->connect('127.0. 0.1', 6379); echo $redis->ping(); and then check if it print +PONG , which show redis-server is running.

What is memory purge?

The MEMORY PURGE command attempts to purge dirty pages so these can be reclaimed by the allocator. This command is currently implemented only when using jemalloc as an allocator, and evaluates to a benign NOOP for all others.


2 Answers

The implementation of CONFIG RESETSTAT is quite simple:

} else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
    if (c->argc != 2) goto badarity;
    server.stat_keyspace_hits = 0;
    server.stat_keyspace_misses = 0;
    server.stat_numcommands = 0;
    server.stat_numconnections = 0;
    server.stat_expiredkeys = 0;
    addReply(c,shared.ok);

So it does not initialize the server.stat_peak_memory field used to store the maximum amount of memory ever used by Redis. I don't know if it is a bug or a feature.

Here is a hack to reset the value without having to stop Redis. The idea is to use gdb in batch mode to just change the value of the variable (which is part of a static structure). Normally Redis is compiled with debugging symbols.

# Here we have plenty of things in this instance
> ./redis-cli info  | grep peak
used_memory_peak:1363052184
used_memory_peak_human:1.27G

# Let's do some cleaning: everything is wiped out
# don't do this in production !!!
> ./redis-cli flushdb
OK

# Again the same values, while some memory has been freed
> ./redis-cli info  | grep peak
used_memory_peak:1363052184
used_memory_peak_human:1.27G

# Here is the magic command: reset the parameter with gdb (output and warnings to be ignored)
> gdb -batch -n -ex 'set variable server.stat_peak_memory = 0' ./redis-server `pidof redis-server`
Missing separate debuginfo for /lib64/libm.so.6
Missing separate debuginfo for /lib64/libdl.so.2
Missing separate debuginfo for /lib64/libpthread.so.0
[Thread debugging using libthread_db enabled]
[New Thread 0x41001940 (LWP 22837)]
[New Thread 0x40800940 (LWP 22836)]
Missing separate debuginfo for /lib64/libc.so.6
Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2

warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff51ff000
0x00002af0b5eef218 in epoll_wait () from /lib64/libc.so.6

# And now, result is different: great !
> ./redis-cli info  | grep peak
used_memory_peak:718768
used_memory_peak_human:701.92K

This is a hack: think twice before applying this trick on a production instance.

like image 79
Didier Spezia Avatar answered Sep 27 '22 18:09

Didier Spezia


Simple trick to clear peal memory::

Step 1:

/home/logproc/redis/bin/redis-cli BGREWRITEAOF

wait till it finish rewriting aof file.

Step 2:

restart redis db

Done. Thats It.

like image 29
Rahul Gojame Avatar answered Sep 27 '22 17:09

Rahul Gojame