Memory-related Issues
For a general understanding of how your Redis server is using memory, you can run the memory stats
command:
memory stats
The following are the metrics reported by memory stats
:
peak.allocated
: The peak number of bytes consumed by Redistotal.allocated
: The total number of bytes allocated by Redisstartup.allocated
: The initial number of bytes consumed by Redis at startupreplication.backlog
: The size of the replication backlog, in bytesclients.slaves
: The total size of all replica overheads (the output and query buffers and connection contexts)clients.normal
: The total size of all client overheadsaof.buffer
: The total size of the current and rewrite append-only file buffersdb.0
: The overheads of the main and expiry dictionaries for each database in use on the server, reported in bytesoverhead.total
: The sum of all overheads used to manage Redis’s keyspacekeys.count
: The total number of keys stored in all the databases on the serverkeys.bytes-per-key
: The ratio of the server’s net memory usage andkeys.count
dataset.bytes
: The size of the dataset, in bytesdataset.percentage
: The percentage of Redis’s net memory usage taken bydataset.bytes
peak.percentage
: The percentage ofpeak.allocated
taken out oftotal.allocated
fragmentation
: The ratio of the amount of memory currently in use divided by the physical memory Redis is actually using
memory malloc-stats
provides an internal statistics report from jemalloc, the memory allocator used by Redis on Linux systems:
memory malloc-stats
If it seems like you’re running into memory-related issues, but parsing the output of the previous commands proves to be unhelpful, you can try running memory doctor
:
memory doctor
This feature will output any memory consumption issues that it can find and suggest potential solutions.
General Information about Your Redis
A debugging command that isn’t directly related to memory management is monitor
. This command allows you to see a constant stream of every command processed by the Redis server:
monitor
Another command useful for debugging is info
, which returns several blocks of information and statistics about the server:
info
This command returns a lot of information. If you only want to see one info block, you can specify it as an argument to info
:
info CPU
Managing Clients
A client is any machine or software that connects to a server in order to access a service. Redis comes with several commands that help with tracking and managing client connections.
The client list
command returns a set of human-readable information about current client connections:
client list
[Output] "id=18165 addr=[2001:db8:0:0::12]:47460 fd=7 name=jerry age=72756 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=ping id=18166 addr=[2001:db8:0:1::12]:47466 fd=8 name= age=72755 idle=5 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=19381 addr=[2001:db8:0:2::12]:54910 fd=9 name= age=9 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"
Here is what each of these fields mean:
id
: a unique 64-bit client IDname
: the name of the client connection, as defined by a priorclient setname
commandaddr
: the address and port from which the client is connectingfd
: the file descriptor that corresponds to the socket over which the client is connectingage
: the total duration of the client connection, in secondsflags
: a set of one or more single-character flags that provide more granular detail about the clients; see theclient list
command documentation for more detailsdb
: the current database ID number that the client is connected to (can be from0
to15
)sub
: the number of channels the client is subscribed topsub
: the number of the client’s pattern-matching subscriptionsmutli
: the number of commands the client has queued in a transaction (will show-1
if the client hasn’t begun a transaction or0
if it has only started a transaction and not queued any commands)qbuf
: the client’s query buffer length, with0
meaning it has no pending queriesqbuf-free
: the amount of free space in the client’s query buffer, with0
meaning that the query buffer is fullobl
: the client’s output buffer lengtholl
: the length of the client’s output list, where replies are queued when its buffer is fullomem
: the memory used by the client’s output bufferevents
: the client’s file descriptor events, these can ber
for “readable”,w
for “writable,” or bothcmd
: the last command run by the client
Setting client names is useful for debugging connection leaks in whatever application is using Redis
Every new connection starts without an assigned name, but client setname
can be used to create one for the current client connection. There’s no limit to how long client names can be, although Redis usually limits string lengths to 512 MB. Note, though, that client names cannot include spaces:
client setname elaine
To retrieve the name of a client connection, use the client getname
command:
client getname
[Output] "elaine"
To retrieve a client’s connection ID, use the client id
command:
client id
[Output] (integer) "19492"
Blocking Clients
wait
blocks the current client connection for a specified amount of time (in milliseconds) until all the previous write commands are successfully transferred and accepted by a specified number of replicas. This command uses the following syntax:
wait number_of_replicas number_of_milliseconds
For example, if you want to block your client connection until all the previous writes are registered by at least 3 replicas within a 30 millisecond timeout, your wait
syntax would look like this:
wait 3 30
To unblock a client connection that has been previously blocked, whether from a wait
, brpop
, or xread
command, you can run a client unblock
command with the following syntax:
client unblock client_id
To temporarily suspend every client currently connected to the Redis server, you can use the client pause
command. This is useful in cases where you need to make changes to your Redis setup in a controlled way.
Closing Client Connections
The client kill
syntax allows you to close a single connection or a set of specific connections based on a number of different filters. The syntax looks like this:
client kill client-id