redis in 20 minutes
TRANSCRIPT
Version: 1st February, 2015
Andras Fehér
Redis (REmote Dictionary Server)
• 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?
• 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?
• 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
• 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
• 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
• 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
• 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
• Replication: – master-slave– failover: manual / automatic (Redis Sentinel)
• Clustering: not production ready, but usable
Scalability
slot = crc16("foo") mod NUMER_SLOTS
• 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
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
• 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
„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