redis in 20 minutes

13
Version: 1st February, 2015 Andras Fehér Redis (REmote Dictionary Server)

Upload: andras-feher

Post on 16-Jan-2017

49 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Redis in 20 minutes

Version: 1st February, 2015

Andras Fehér

Redis (REmote Dictionary Server)

Page 2: Redis in 20 minutes

• Open-source (ANSI C)• Non-relational• Key-value• In-memory, but persists to disk in background• Scalable• Sponsored by VMware, Pivotal Software• Lead developer: Salvatore Sanfilippo• Most popular key-value store, 10th most popular

database (db-engines.com)• Official project page: redis.io

What is Redis?

Page 3: Redis in 20 minutes

• Fast (benchmarks): default slow log setting: 0.01s / MySQL: 10s (1000x)

• Easy to learn, short, but powerful commands• Tunable persistence• Transaction support (type of)• Scripting support• Efficient memory management• Supported by all popular languages• Effective datatypes

Why Redis?

Page 4: Redis in 20 minutes

• Maps keys to 5 + 2 different types of values:

Data types

strings hashes lists sets sorted sets bitmaps HyperLogLogs

same as in modern programminglanguages

probabilistic data structure to count unique things, standard error 0.81% up to 264 items

Page 5: Redis in 20 minutes

• Bitmap– bit operations on string– capacity: 232 bit– space efficient, but high performance boolean

associations

• HyperLogLog– probabilistic data structure to count unique

things– standard error < 1%

Bitmap and HyperLogLog

Page 6: Redis in 20 minutes

• redis-cli: the command line interface

• Keys:

– KEYS: Find all keys matching the given pattern

• Strings

– SET: Set the string value of a key

– GET: Get the value of a key

– INCR/DECR: Increment/decrement the integer value of a key by one

• Hashes

– HSET: Set the string value of a hash field

– HGET: Get the value of a hash field

• Key expiry

– EXPIRE/PEXPIRE: Set key's time to live in seconds/miliseconds

– TTL: Get the time to live for the key

– SETEX/PSETEX: Set the value and expiration of a key

Some basic commands demo

Page 7: Redis in 20 minutes

• MULTI, EXEC, DISCARD, WATCH• Commands serialized and executed

sequentially• Atomic: all commands or none processed• No rollback support!• WATCH:

– optimistic locking– if monitored key changes, the transaction is

aborted

TransactionsExample:WATCH aMULTISET a 1SET b 1EXEC

Page 8: Redis in 20 minutes

• In-memory persistent storage on disk:– dump-to-disk: automatic / manual (SAVE,

BGSAVE)– command log

• Tunable from disabling persistance up to traditional database level

Persistance

Page 9: Redis in 20 minutes

• Replication: – master-slave– failover: manual / automatic (Redis Sentinel)

• Clustering: not production ready, but usable

Scalability

slot = crc16("foo") mod NUMER_SLOTS

Page 10: Redis in 20 minutes

• Implements the Publish/Subscribe messaging paradigm (SUBSCRIBE, UNSUBSCRIBE, PUBLISH)

• Can substitute complex and resorce consuming message queing applications

• Mainly used for scaling applications

Publish/Subscribe

Page 11: Redis in 20 minutes

Publish/Subscribe (Demo)

PUBLISHERpublishes

messages to „mychannel”

SUBCRIBER1subscribes to„mychannel”

SUBCRIBER2subscribes to„mychannel”

Pattern matching subscription / unsubscription example:PSUBSCRIBE news.*PUNSUBSCRIBE news.*Applies to news.art.figurative, news.music.jazz, etc channels

Page 12: Redis in 20 minutes

• Lua scripting language, built-in interpreter• EVAL, EVALSHA: run script• Extends Redis commands• Should only operate on Redis data and

passed arguments• No other script or Redis command will be

executed while a script is being executed: default timeout: 5s

• Slow runs are logged

Scripting

Page 13: Redis in 20 minutes

„Redis is designed to be accessed by trusted clients inside trusted environments.”

Security

• Password authentication only• No data encryption support• Block Redis port in Firewall • Bind to a single interface (e.g. 127.0.0.1) if

possible• Disable / rename specific commands• Injection is impossible• Run Redis as an upriviledged redis user