SlideShare a Scribd company logo
Redis
NoSQL key-data store
1
La 1ère école 100 % dédiée à l'open source
Open Source School est fondée à l'initiative de Smile, leader de
l'intégration et de l'infogérance open source, et de l'EPSI,établissement
privé pionnier de l’enseignement supérieur en informatique.
Dans le cadre du Programme d’Investissements d’Avenir (PIA), le
gouvernement français a décidé de soutenir la création de cette école en
lui attribuant une première aide de 1,4M€ et confirme sa volonté de
soutenir la filière du Logiciel Libre actuellement en plein développement.
Avec une croissance annuelle de plus de 10%, et 4 000
postes vacants chaque année dans le secteur du Logiciel
Libre, OSS entend répondre à la pénurie de compétences du
secteur en mobilisant l’ensemble de l’écosystème et en
proposant la plus vaste offre en matière de formation aux
technologies open source tant en formation initiale qu'en
formation continue.
2
Les formations du plein emploi !
 Formation Continue
Open Source School "Executive Education" est un organisme
de formation qui propose un catalogue de plus de 200
formations professionnelles et différents dispositifs de
reconversion permettant le retour à l’emploi (POE) ou une
meilleure employabilité pour de nombreux professionnels de
l’informatique.
 Pour vos demandes : formations@opensourceschool.fr
 Formation Initiale
100% logiciels libres et 100% alternance, le cursus Open
Source School s’appuie sur le référentiel des blocs de
compétences de l’EPSI.
Il est sanctionné par un titre de niveau I RNCP, Bac+5.
Le programme est proposé dans 6 campus à Bordeaux, Lille,
Lyon, Montpellier, Nantes, Paris.
3
Nos domaines de formations
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Plan
1 NoSQL introduction
2 Redis overview
3 Installing and configuring Redis
4 Types and commands
5 Redis in your applications
6 Questions ?
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 2/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
NoSQL introduction
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 3/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Why not a RDBMS
Short answer : because performance
Computer science is all about the integrity/performance
tradeoff
RDBMS choose integrity
This is important, but sometimes you don’t care
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 4/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Not Only SQL
Business always needs some integrity, why not split the data by
type and constraints?
Traditional design Using not only SQL
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 5/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis overview
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 6/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
What is Redis?
Redis is:
an advanced key-value store
an in-memory non-relational database
a data structure server
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 7/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Identity Card
Created by: Salvatore Sanfilippo (Italian)
first release: April 10, 2009
Written in: C
Licence: BSD
Last Realease: 2.8.9 - April 22, 2014
Sponsored by: VMware until May 2013, Pivotal Software after
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 8/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Useful links
Official Website: http://redis.io/
Sources: https://github.com/antirez/redis
Community Group:
https://groups.google.com/forum/?fromgroups#!forum/redis-db
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 9/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Interesting features
Data structures
Atomic operations
Configurable persistence
Asynchronous replication
TTL and LRU (cache-like behavious)
Pub/Sub
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 10/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
What the website won’t tell you
Redis is mono threaded, a single process cannot scale to
multiple CPUs
Sharding is possible if the client supports it
Redis better used for making persistent caches, advanced
caches (tags) or session storage
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 11/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis vs Memcached
Memcached is only in memory, Redis can persist the data
Memcached has only 1 namespace, Redis can have multiple
namespaces
Memcached can only store data in plain string, Redis has 5
types of structure
Memcached has limited data manipulation commands, Redis
has an advanced query language
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 12/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installing and configuring Redis
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 13/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Installation
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 14/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Version
Redis is moving fast
Make sure you’re using at least version 2.4
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 15/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Install on Debian
For Wheezy and above :
aptitude install redis-server
For Squeeze, you need to activate the ”backports” repository
aptitude install -t squeeze-backports
redis-server
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 16/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Install on RHEL/CentOS 6
First you need to activate the EPEL repository
Install the package :
yum install redis
Activate and start the service :
chkconfig redis on && service redis start
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 17/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Configuration
The configuration file is in /etc/redis/redis.conf
(/etc/redis.conf on RHEL/CentOS)
You might want to change the listening address :
bind 0.0.0.0
Configure redis as a cache
maxmemory 2147483648
maxmemory-policy allkeys-lru
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 18/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Installation
Databases
One redis instance can have multiple namespaces
They are called ”databases”
They are referenced by an integer
By default, redis has 16 databases
Database numbering begins at 0
use SELECT N to change databases in redis-cli
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 19/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Persistency
Persistency
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 20/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Persistency
Persistency methods
2 methods:
snapshoting data in memory to disk
append-only file (AOF) write/delete commands saved in order
in a file
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 21/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Persistency
Snapshoting config
#path of the snapshot file
dbfilename dump.rdb
#compress the data
rdbcompression yes
#if snapshot can’t be performed writes are
#suspended until a manual operation fix the problem
stop-writes-on-bgsave-error no
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 22/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Persistency
initiate a snaphot
5 methods to initiate a snapshot:
command BGSAVE sent by a client, redis will fork and child
make the snapshot in parallel the father continue to respond
for client requests
command SAVE sent by a client, redis stop processing all
client request and make a snapshot
command SHUTDOWN or a TERM signal, redis launch an internal
SAVE command before stopping
save <sec> <nb> parameter in config file, if the condition
match redis launch an internal BGSAVE command
a SYNC command received from another redis server, redis
launch an internal BGSAVE command if one isn’t already
executing or recently completed
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 23/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Persistency
AOF config
general configuration information:
#activate AOF
appendonly <yes|no>
#manage writes on disk
appendfsync <always|everysec|no>
#disable sync on rewrite
no-appendfsync-on-rewrite no
#pourcentage of the current file size against
#file size after last rewrite
auto-aof-rewrite-percentage 100
#minimum size of the file before lauching a rewrite
auto-aof-rewrite-min-size 64mb
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 24/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Scaling
Scaling
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 25/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Scaling
Replication
Master/Slave architecture
steps:
slave send a SYNC command to the master
master launch a BGSAVE and keep in a backlog all the write
commands received after
master send the snapshot to the slave
slave discard all old data and load the new snapshot
when the new snapshot is loaded master send all the backlog
to the slave
master live stream all the write commands to the slave
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 26/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Scaling
Replication Config
Replication is asynchronous
It’s easy to setup :
add in config file slaveof <masterHostname>
<masterPort>
or send command SLAVEOF <masterHostname>
<masterPort>
Stop it with : SLAVEOF NO ONE
Check it with : INFO
Look for role and master link status keys
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 27/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Types and commands
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 28/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Key Naming
Best practice is to name your key with an object name followed by
an id separated by : or , or / and to keep the same rule for all the
pair in a database.
post:254
article.10
session/45656665
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 29/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
Structure Types
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 30/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
STRING
strings, integers, floating-point values (max 512 MB)
basic commands:
GET: fetch data at the given key
SET: set data at the given key
DEL: delete the data at the given key
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 31/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
STRING: example
redis 127.0.0.1:6379> set hello world
OK
redis 127.0.0.1:6379> get hello
"world"
redis 127.0.0.1:6379> del hello
(integer) 1
redis 127.0.0.1:6379> get hello
(nil)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 32/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
STRING: Increment and decrement
if the value is an integer or a floating-point
INCR: add 1
DECR: minus 1
INCRBY/DECRBY: add/minux X
INCRBYFLOAT: add float
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 33/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
STRING: substring manipulation
if the value is a string
APPEND: concatenate the value with the string given
GETRANGE: get a substring of the value
SETRANGE: replace the substring value starting at the offset X
by the string given
GETBIT, SETBIT, BITCOUNT, BITOP: binary manipulation of
the value
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 34/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
LIST
linked list of strings (max. 2ˆ32 -1)
basic commands:
LPUSH, RPUSH: push the value(s) onto the left/right end of
the list
LRANGE: fetch a range of value from the list
LINDEX: fetch an item at a given position in the list
LPOP, RPOP: remove and return the value from the left/right
end of the list
LTRIM: trim the list to only include items at position between
start and end, inclusive
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 35/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
LIST: example
redis 127.0.0.1:6379> rpush ingredients egg
(integer) 1
redis 127.0.0.1:6379> rpush ingredients milk
(integer) 2
redis 127.0.0.1:6379> rpush ingredients egg
(integer) 3
redis 127.0.0.1:6379> lrange ingredients 0 -1
1) "egg"
2) "milk"
3) "egg"
redis 127.0.0.1:6379> lindex ingredients 1
"milk"
redis 127.0.0.1:6379> lpop ingredients
"egg"
redis 127.0.0.1:6379> lrange ingredients 0 -1
1) "milk"
2) "egg"
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 36/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
LIST: blocking and moving
BLPOP, BRPOP: pop the left/right most item of the first non
empty list given or wait the timeout to get an item
RPOPLPUSH: pop the righ most item from the first list, left
push it to the second list and send it back to the user
BRPOPLPUSH: same as RPOPLPUSH wait the timeout to get an
item in the source list if its empty
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 37/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
SET
Unordered collection of unique strings (max. 2ˆ32 -1)
basic commands:
SADD: add item(s) to the set
SMEMBERS: return all the items contains in the set
SISMEMBER: check if an item is present in the set
SCARD: number of items in the set
SREM: remove item(s) from the set
SRANDMEMBER: return randomly x items of the set, if X is
positive the will be distinct, if X is negative a same value
could be sent more than one time
SPOP: remove and return a random item of the set
SMOVE: move an item from a set to aonther set and return it
to the user
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 38/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
LIST: example
redis 127.0.0.1:6379> sadd ingredients sugar
(integer) 1
redis 127.0.0.1:6379> sadd ingredients floor
(integer) 1
redis 127.0.0.1:6379> sadd ingredients butter
(integer) 1
redis 127.0.0.1:6379> sadd ingredients sugar
(integer) 0
redis 127.0.0.1:6379> smembers ingredients
1) "sugar"
2) "floor"
3) "butter"
redis 127.0.0.1:6379> sismember ingredients chocolate
(integer) 0
redis 127.0.0.1:6379> sismember ingredients sugar
(integer) 1
redis 127.0.0.1:6379> srem ingredients floor
(integer) 1
redis 127.0.0.1:6379> srem ingredients floor
(integer) 0
redis 127.0.0.1:6379> smembers ingredients
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 39/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
SET: combining and manipulating
SDIFF: make the difference of X sets and return the result
SDIFFSTORE: make the difference of X sets and store the
result in another set
SINTER: make the intersection of X sets and return the result
SINTERSTORE: make the intersection of X sets and store the
result in another set
SUNION: returns the items that are in at least one of the SETs
SUNIONSTORE: store in a given set the items that are in at
least one of the SETs
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 40/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
HASH
Unordered hash table of keys to values
basic commands:
HSET: set a value at a given key in a hash (HMSET to multiple
key)
HGET: get a value at a given key in a hash (HSET to multiple
key)
HGETALL: fetch all the hash
HDEL: remove a key from a hash
HLEN: return the number of pair in the hash
HEXISTS: check if key exist in the hash
HKEYS: return the list of keys
HVALS: return the list of values
HINCRBY/HINCRBYFLOAT: add X to the value of a key
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 41/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
HASH: example
redis 127.0.0.1:6379> hset ingredients eggs 2
(integer) 1
redis 127.0.0.1:6379> hset ingredients floor 250
(integer) 1
redis 127.0.0.1:6379> hset ingredients eggs 2
(integer) 0
redis 127.0.0.1:6379> hgetall ingredients
1) "eggs"
2) "2"
3) "floor"
4) "250"
redis 127.0.0.1:6379> hdel ingredients floor
(integer) 1
redis 127.0.0.1:6379> hdel ingredients floor
(integer) 0
redis 127.0.0.1:6379> hget ingredients eggs
"2"
redis 127.0.0.1:6379> hgetall ingredients
1) "eggs"
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 42/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
ZSET
Ordered mapping of -string- members (keys) to -floating-point-
scores (values), ordered by score first then by member name can
be accessed by member or order number of score or score
basic commands:
ZADD: add member with a given score to the ZSET
ZRANGE: fetch the items by theire position in sorted order
ZRANGEBYSCORE: fetch the items by score range
ZCARD: number of members
ZINCRBY: increment the score of a member
ZCOUNT: return the number of members with a score between
the provided arguments
ZRANK: position of the given member
ZSCORE: get the score of a member
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 43/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Structure Types
HASH: example
redis 127.0.0.1:6379> zadd ingredients 110 sugar
(integer) 1
redis 127.0.0.1:6379> zadd ingredients 250 floor
(integer) 1
redis 127.0.0.1:6379> zadd ingredients 250 floor
(integer) 0
redis 127.0.0.1:6379> zrange ingredients 0 -1 withscores
1) "sugar"
2) "110"
3) "floor"
4) "250"
redis 127.0.0.1:6379> zrangebyscore ingredients 0 200 withscores
1) "sugar"
2) "110"
redis 127.0.0.1:6379> zrem ingredients sugar
(integer) 1
redis 127.0.0.1:6379> zrem ingredients sugar
(integer) 0
redis 127.0.0.1:6379> zrange ingredients 0 -1 withscores
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 44/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Sort
Sort
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 45/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Sort
Sort
can sort LIST, SET, ZSET
redis 127.0.0.1:6379> rpush sort-input 23 15 110 7
4
redis 127.0.0.1:6379> sort sort-input
[’7’, ’15’, ’23’, ’110’]
redis 127.0.0.1:6379> sort sort-input alpha
[’110’, ’15’, ’23’, ’7’]
redis 127.0.0.1:6379> hset d-7 field 5
1L
redis 127.0.0.1:6379> hset d-15 field 1
1L
redis 127.0.0.1:6379> hset d-23 field 9
1L
redis 127.0.0.1:6379> hset d-110 field 3
1L
redis 127.0.0.1:6379> sort sort-input by d-*->field
[’15’, ’110’, ’7’, ’23’]
redis 127.0.0.1:6379> sort sort-input by d-*->field’ get d-*->field
[’1’, ’3’, ’5’, ’9’]
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 46/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Transactions
Transactions
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 47/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Transactions
Transactions
Pipeline of commands:
When redis receive the command MULTI it store the following
commands
When it receive EXEC it execute all the commands receivend
in a single transaction
MULTI
<command1>
<command2>
<command3>
EXEC
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 48/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Transactions
optimistic locking
if the data is changed before EXEC the client is notified of the
update and can change is action accordingly
UNWATCH ¡key¿ can be used after a WATCH and before a MULTI
to cancel the optimistic locking
DISCARD ¡key¿ can be used after a WATCH and MULTI to
cancel the optimistic locking and the transaction
WATH <key>
MULTI
<command1>
<command2>
<command3>
EXEC
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 49/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Expiration
Expiration
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 50/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Expiration
Expiration
expiration on duration or point in time of a key and all of it’s
content
EXPIRE: set the expiration duration in seconds
EXPIREAT: set the expiration time with a unix timestamp
TTL: return the amount of time remaining before a key will
expire
PERSIST: remove the expiration for a key
PTTL: return the amount of time in milliseconds remaining
before a key will expire
PEXPIRE: set the expiration duration in milliseconds
PEXPIREAT: set the expiration time with a unix timestamp in
milliseconds
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 51/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Publish / subscribe
Publish / subscribe
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 52/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Publish / subscribe
Publish / subscribe
A consumer receive all the data sent on a subscribed channel
SUBSCRIBE: subscribe to a channel
UNSUBSCRIBE: unsubscribe from a channel
PUBLISH: publish message to a channel
PSUBSCRIBE: subscribe to channels witha given pattern
PUNSUBSCRIBE: unsubscribe to channels witha given pattern
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 53/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Publish / subscribe
warning
2 reasons to not use it:
if client is not reading or too slowly it could cause old Redis
version to slow down or crash
if there is network failure the message sent during the failure
will be lost
Use blocking list instead or a real MQ application
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 54/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Scripting
Scripting
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 55/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Scripting
Scripting
Redis 2.6 introduce scripting with Lua scriping language
wins:
execute complexe code on server-side
implement missing functionnalities like locking
performance
drawbacks:
block during execution all other client request (single thread)
type mapping is not complete
can’t named a script use sha1 digest instead
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 56/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis in your applications
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 57/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis and PHP
Redis and PHP
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 58/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis and PHP
The phpredis driver
The most widely used solution to connect to redis from PHP
Distributed by PECL under the name redis
Distributed on the Smile Hosting repositories
apt-get install php5-redis
Has support for sharding (”arrays”)
Object-oriented interface
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 59/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis and PHP
PHP session handler
[PHP]
session.save_handler = redis
session.save_path = "tcp://host1:6379?timeout=3,database=1"
Multiple hosts can be given
Timing out on the session manager is fatal
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 60/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis and Magento
Redis and Magento
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 61/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Redis and Magento
Redis and Magento
Natively supported from Magento 1.13
Smile-provided module for Magento 1.12
Can be used as session manager
Persistent sessions
Replication
Can be used as cache manager
Advanced data types allow to store the entire cache
No more two-level cache
No more tags in databases
Check the wiki for how-to and caveats
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 62/63
NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica
Questions ?
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 63/63

More Related Content

Similar to Redis : NoSQL key-data store (20)

PPTX
Redis and it's data types
Aniruddha Chakrabarti
 
PPTX
Redis introduction
Federico Daniel Colombo Gennarelli
 
ODP
Redis overview
Ahmad El-khuja
 
PPTX
Redis data structure and Performance Optimization
Knoldus Inc.
 
PDF
An Introduction to Redis for Developers.pdf
Stephen Lorello
 
PDF
Redis acc 2015_eng
DaeMyung Kang
 
PPTX
Redis database
Ñáwrás Ñzár
 
PPT
NoSQL Databases
Carlos Alberto Benitez
 
PDF
Introduction to Redis
Itamar Haber
 
PDF
dba_lounge_Iasi: Everybody likes redis
Liviu Costea
 
KEY
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
PPTX
2_Redis.pptx very useful for begginers yes
soufianbouktaib1
 
PDF
Redis Installation Configuration And Implementation
Abhijeet Shekhar
 
PDF
An Introduction to Redis for .NET Developers.pdf
Stephen Lorello
 
PDF
Redis 101
Abhishek Parwal
 
PDF
Mini-Training: Redis
Betclic Everest Group Tech Team
 
PPTX
redis-demo.pptx
SNEHKUMAR30
 
PPTX
Intro to Redis
Jeff Alstadt
 
PDF
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Andrew Lavers
 
PDF
Redis Overview
Hoang Long
 
Redis and it's data types
Aniruddha Chakrabarti
 
Redis overview
Ahmad El-khuja
 
Redis data structure and Performance Optimization
Knoldus Inc.
 
An Introduction to Redis for Developers.pdf
Stephen Lorello
 
Redis acc 2015_eng
DaeMyung Kang
 
Redis database
Ñáwrás Ñzár
 
NoSQL Databases
Carlos Alberto Benitez
 
Introduction to Redis
Itamar Haber
 
dba_lounge_Iasi: Everybody likes redis
Liviu Costea
 
Blazing Data With Redis (and LEGOS!)
Justin Carmony
 
2_Redis.pptx very useful for begginers yes
soufianbouktaib1
 
Redis Installation Configuration And Implementation
Abhijeet Shekhar
 
An Introduction to Redis for .NET Developers.pdf
Stephen Lorello
 
Redis 101
Abhishek Parwal
 
Mini-Training: Redis
Betclic Everest Group Tech Team
 
redis-demo.pptx
SNEHKUMAR30
 
Intro to Redis
Jeff Alstadt
 
Redis is not just a cache, Andrew Lavers, ConFoo Montreal 2020
Andrew Lavers
 
Redis Overview
Hoang Long
 

More from Open Source School (13)

PDF
Linux Administrateur
Open Source School
 
PDF
Linux Scripting
Open Source School
 
PDF
PHP & Initiation Symfony
Open Source School
 
PDF
Strategie de Virtualisation Open Source
Open Source School
 
PDF
GIT pour développeur
Open Source School
 
PDF
Kerberos : The network authentification protocol
Open Source School
 
PDF
LDAP : Theory and OpenLDAP implementation
Open Source School
 
PDF
Monitoring : The art of knowing when and why things go wrong
Open Source School
 
PDF
PostgreSQL : Introduction
Open Source School
 
PDF
Redhat enterprise Linux
Open Source School
 
PDF
SSL : Theory and practice
Open Source School
 
PDF
Varnish : Advanced and high-performance HTTP caching
Open Source School
 
Linux Administrateur
Open Source School
 
Linux Scripting
Open Source School
 
PHP & Initiation Symfony
Open Source School
 
Strategie de Virtualisation Open Source
Open Source School
 
GIT pour développeur
Open Source School
 
Kerberos : The network authentification protocol
Open Source School
 
LDAP : Theory and OpenLDAP implementation
Open Source School
 
Monitoring : The art of knowing when and why things go wrong
Open Source School
 
PostgreSQL : Introduction
Open Source School
 
Redhat enterprise Linux
Open Source School
 
SSL : Theory and practice
Open Source School
 
Varnish : Advanced and high-performance HTTP caching
Open Source School
 
Ad

Recently uploaded (20)

PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Ad

Redis : NoSQL key-data store

  • 2. 1 La 1ère école 100 % dédiée à l'open source Open Source School est fondée à l'initiative de Smile, leader de l'intégration et de l'infogérance open source, et de l'EPSI,établissement privé pionnier de l’enseignement supérieur en informatique. Dans le cadre du Programme d’Investissements d’Avenir (PIA), le gouvernement français a décidé de soutenir la création de cette école en lui attribuant une première aide de 1,4M€ et confirme sa volonté de soutenir la filière du Logiciel Libre actuellement en plein développement. Avec une croissance annuelle de plus de 10%, et 4 000 postes vacants chaque année dans le secteur du Logiciel Libre, OSS entend répondre à la pénurie de compétences du secteur en mobilisant l’ensemble de l’écosystème et en proposant la plus vaste offre en matière de formation aux technologies open source tant en formation initiale qu'en formation continue.
  • 3. 2 Les formations du plein emploi !  Formation Continue Open Source School "Executive Education" est un organisme de formation qui propose un catalogue de plus de 200 formations professionnelles et différents dispositifs de reconversion permettant le retour à l’emploi (POE) ou une meilleure employabilité pour de nombreux professionnels de l’informatique.  Pour vos demandes : [email protected]  Formation Initiale 100% logiciels libres et 100% alternance, le cursus Open Source School s’appuie sur le référentiel des blocs de compétences de l’EPSI. Il est sanctionné par un titre de niveau I RNCP, Bac+5. Le programme est proposé dans 6 campus à Bordeaux, Lille, Lyon, Montpellier, Nantes, Paris.
  • 4. 3 Nos domaines de formations
  • 5. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Plan 1 NoSQL introduction 2 Redis overview 3 Installing and configuring Redis 4 Types and commands 5 Redis in your applications 6 Questions ? www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 2/63
  • 6. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica NoSQL introduction www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 3/63
  • 7. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Why not a RDBMS Short answer : because performance Computer science is all about the integrity/performance tradeoff RDBMS choose integrity This is important, but sometimes you don’t care www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 4/63
  • 8. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Not Only SQL Business always needs some integrity, why not split the data by type and constraints? Traditional design Using not only SQL www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 5/63
  • 9. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis overview www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 6/63
  • 10. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica What is Redis? Redis is: an advanced key-value store an in-memory non-relational database a data structure server www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 7/63
  • 11. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Identity Card Created by: Salvatore Sanfilippo (Italian) first release: April 10, 2009 Written in: C Licence: BSD Last Realease: 2.8.9 - April 22, 2014 Sponsored by: VMware until May 2013, Pivotal Software after www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 8/63
  • 12. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Useful links Official Website: http://redis.io/ Sources: https://github.com/antirez/redis Community Group: https://groups.google.com/forum/?fromgroups#!forum/redis-db www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 9/63
  • 13. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Interesting features Data structures Atomic operations Configurable persistence Asynchronous replication TTL and LRU (cache-like behavious) Pub/Sub www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 10/63
  • 14. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica What the website won’t tell you Redis is mono threaded, a single process cannot scale to multiple CPUs Sharding is possible if the client supports it Redis better used for making persistent caches, advanced caches (tags) or session storage www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 11/63
  • 15. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis vs Memcached Memcached is only in memory, Redis can persist the data Memcached has only 1 namespace, Redis can have multiple namespaces Memcached can only store data in plain string, Redis has 5 types of structure Memcached has limited data manipulation commands, Redis has an advanced query language www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 12/63
  • 16. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installing and configuring Redis www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 13/63
  • 17. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Installation www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 14/63
  • 18. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Version Redis is moving fast Make sure you’re using at least version 2.4 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 15/63
  • 19. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Install on Debian For Wheezy and above : aptitude install redis-server For Squeeze, you need to activate the ”backports” repository aptitude install -t squeeze-backports redis-server www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 16/63
  • 20. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Install on RHEL/CentOS 6 First you need to activate the EPEL repository Install the package : yum install redis Activate and start the service : chkconfig redis on && service redis start www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 17/63
  • 21. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Configuration The configuration file is in /etc/redis/redis.conf (/etc/redis.conf on RHEL/CentOS) You might want to change the listening address : bind 0.0.0.0 Configure redis as a cache maxmemory 2147483648 maxmemory-policy allkeys-lru www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 18/63
  • 22. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Installation Databases One redis instance can have multiple namespaces They are called ”databases” They are referenced by an integer By default, redis has 16 databases Database numbering begins at 0 use SELECT N to change databases in redis-cli www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 19/63
  • 23. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Persistency Persistency www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 20/63
  • 24. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Persistency Persistency methods 2 methods: snapshoting data in memory to disk append-only file (AOF) write/delete commands saved in order in a file www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 21/63
  • 25. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Persistency Snapshoting config #path of the snapshot file dbfilename dump.rdb #compress the data rdbcompression yes #if snapshot can’t be performed writes are #suspended until a manual operation fix the problem stop-writes-on-bgsave-error no www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 22/63
  • 26. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Persistency initiate a snaphot 5 methods to initiate a snapshot: command BGSAVE sent by a client, redis will fork and child make the snapshot in parallel the father continue to respond for client requests command SAVE sent by a client, redis stop processing all client request and make a snapshot command SHUTDOWN or a TERM signal, redis launch an internal SAVE command before stopping save <sec> <nb> parameter in config file, if the condition match redis launch an internal BGSAVE command a SYNC command received from another redis server, redis launch an internal BGSAVE command if one isn’t already executing or recently completed www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 23/63
  • 27. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Persistency AOF config general configuration information: #activate AOF appendonly <yes|no> #manage writes on disk appendfsync <always|everysec|no> #disable sync on rewrite no-appendfsync-on-rewrite no #pourcentage of the current file size against #file size after last rewrite auto-aof-rewrite-percentage 100 #minimum size of the file before lauching a rewrite auto-aof-rewrite-min-size 64mb www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 24/63
  • 28. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Scaling Scaling www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 25/63
  • 29. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Scaling Replication Master/Slave architecture steps: slave send a SYNC command to the master master launch a BGSAVE and keep in a backlog all the write commands received after master send the snapshot to the slave slave discard all old data and load the new snapshot when the new snapshot is loaded master send all the backlog to the slave master live stream all the write commands to the slave www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 26/63
  • 30. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Scaling Replication Config Replication is asynchronous It’s easy to setup : add in config file slaveof <masterHostname> <masterPort> or send command SLAVEOF <masterHostname> <masterPort> Stop it with : SLAVEOF NO ONE Check it with : INFO Look for role and master link status keys www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 27/63
  • 31. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Types and commands www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 28/63
  • 32. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Key Naming Best practice is to name your key with an object name followed by an id separated by : or , or / and to keep the same rule for all the pair in a database. post:254 article.10 session/45656665 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 29/63
  • 33. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types Structure Types www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 30/63
  • 34. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types STRING strings, integers, floating-point values (max 512 MB) basic commands: GET: fetch data at the given key SET: set data at the given key DEL: delete the data at the given key www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 31/63
  • 35. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types STRING: example redis 127.0.0.1:6379> set hello world OK redis 127.0.0.1:6379> get hello "world" redis 127.0.0.1:6379> del hello (integer) 1 redis 127.0.0.1:6379> get hello (nil) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 32/63
  • 36. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types STRING: Increment and decrement if the value is an integer or a floating-point INCR: add 1 DECR: minus 1 INCRBY/DECRBY: add/minux X INCRBYFLOAT: add float www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 33/63
  • 37. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types STRING: substring manipulation if the value is a string APPEND: concatenate the value with the string given GETRANGE: get a substring of the value SETRANGE: replace the substring value starting at the offset X by the string given GETBIT, SETBIT, BITCOUNT, BITOP: binary manipulation of the value www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 34/63
  • 38. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types LIST linked list of strings (max. 2ˆ32 -1) basic commands: LPUSH, RPUSH: push the value(s) onto the left/right end of the list LRANGE: fetch a range of value from the list LINDEX: fetch an item at a given position in the list LPOP, RPOP: remove and return the value from the left/right end of the list LTRIM: trim the list to only include items at position between start and end, inclusive www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 35/63
  • 39. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types LIST: example redis 127.0.0.1:6379> rpush ingredients egg (integer) 1 redis 127.0.0.1:6379> rpush ingredients milk (integer) 2 redis 127.0.0.1:6379> rpush ingredients egg (integer) 3 redis 127.0.0.1:6379> lrange ingredients 0 -1 1) "egg" 2) "milk" 3) "egg" redis 127.0.0.1:6379> lindex ingredients 1 "milk" redis 127.0.0.1:6379> lpop ingredients "egg" redis 127.0.0.1:6379> lrange ingredients 0 -1 1) "milk" 2) "egg" www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 36/63
  • 40. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types LIST: blocking and moving BLPOP, BRPOP: pop the left/right most item of the first non empty list given or wait the timeout to get an item RPOPLPUSH: pop the righ most item from the first list, left push it to the second list and send it back to the user BRPOPLPUSH: same as RPOPLPUSH wait the timeout to get an item in the source list if its empty www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 37/63
  • 41. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types SET Unordered collection of unique strings (max. 2ˆ32 -1) basic commands: SADD: add item(s) to the set SMEMBERS: return all the items contains in the set SISMEMBER: check if an item is present in the set SCARD: number of items in the set SREM: remove item(s) from the set SRANDMEMBER: return randomly x items of the set, if X is positive the will be distinct, if X is negative a same value could be sent more than one time SPOP: remove and return a random item of the set SMOVE: move an item from a set to aonther set and return it to the user www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 38/63
  • 42. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types LIST: example redis 127.0.0.1:6379> sadd ingredients sugar (integer) 1 redis 127.0.0.1:6379> sadd ingredients floor (integer) 1 redis 127.0.0.1:6379> sadd ingredients butter (integer) 1 redis 127.0.0.1:6379> sadd ingredients sugar (integer) 0 redis 127.0.0.1:6379> smembers ingredients 1) "sugar" 2) "floor" 3) "butter" redis 127.0.0.1:6379> sismember ingredients chocolate (integer) 0 redis 127.0.0.1:6379> sismember ingredients sugar (integer) 1 redis 127.0.0.1:6379> srem ingredients floor (integer) 1 redis 127.0.0.1:6379> srem ingredients floor (integer) 0 redis 127.0.0.1:6379> smembers ingredients www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 39/63
  • 43. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types SET: combining and manipulating SDIFF: make the difference of X sets and return the result SDIFFSTORE: make the difference of X sets and store the result in another set SINTER: make the intersection of X sets and return the result SINTERSTORE: make the intersection of X sets and store the result in another set SUNION: returns the items that are in at least one of the SETs SUNIONSTORE: store in a given set the items that are in at least one of the SETs www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 40/63
  • 44. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types HASH Unordered hash table of keys to values basic commands: HSET: set a value at a given key in a hash (HMSET to multiple key) HGET: get a value at a given key in a hash (HSET to multiple key) HGETALL: fetch all the hash HDEL: remove a key from a hash HLEN: return the number of pair in the hash HEXISTS: check if key exist in the hash HKEYS: return the list of keys HVALS: return the list of values HINCRBY/HINCRBYFLOAT: add X to the value of a key www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 41/63
  • 45. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types HASH: example redis 127.0.0.1:6379> hset ingredients eggs 2 (integer) 1 redis 127.0.0.1:6379> hset ingredients floor 250 (integer) 1 redis 127.0.0.1:6379> hset ingredients eggs 2 (integer) 0 redis 127.0.0.1:6379> hgetall ingredients 1) "eggs" 2) "2" 3) "floor" 4) "250" redis 127.0.0.1:6379> hdel ingredients floor (integer) 1 redis 127.0.0.1:6379> hdel ingredients floor (integer) 0 redis 127.0.0.1:6379> hget ingredients eggs "2" redis 127.0.0.1:6379> hgetall ingredients 1) "eggs" www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 42/63
  • 46. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types ZSET Ordered mapping of -string- members (keys) to -floating-point- scores (values), ordered by score first then by member name can be accessed by member or order number of score or score basic commands: ZADD: add member with a given score to the ZSET ZRANGE: fetch the items by theire position in sorted order ZRANGEBYSCORE: fetch the items by score range ZCARD: number of members ZINCRBY: increment the score of a member ZCOUNT: return the number of members with a score between the provided arguments ZRANK: position of the given member ZSCORE: get the score of a member www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 43/63
  • 47. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Structure Types HASH: example redis 127.0.0.1:6379> zadd ingredients 110 sugar (integer) 1 redis 127.0.0.1:6379> zadd ingredients 250 floor (integer) 1 redis 127.0.0.1:6379> zadd ingredients 250 floor (integer) 0 redis 127.0.0.1:6379> zrange ingredients 0 -1 withscores 1) "sugar" 2) "110" 3) "floor" 4) "250" redis 127.0.0.1:6379> zrangebyscore ingredients 0 200 withscores 1) "sugar" 2) "110" redis 127.0.0.1:6379> zrem ingredients sugar (integer) 1 redis 127.0.0.1:6379> zrem ingredients sugar (integer) 0 redis 127.0.0.1:6379> zrange ingredients 0 -1 withscores www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 44/63
  • 48. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Sort Sort www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 45/63
  • 49. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Sort Sort can sort LIST, SET, ZSET redis 127.0.0.1:6379> rpush sort-input 23 15 110 7 4 redis 127.0.0.1:6379> sort sort-input [’7’, ’15’, ’23’, ’110’] redis 127.0.0.1:6379> sort sort-input alpha [’110’, ’15’, ’23’, ’7’] redis 127.0.0.1:6379> hset d-7 field 5 1L redis 127.0.0.1:6379> hset d-15 field 1 1L redis 127.0.0.1:6379> hset d-23 field 9 1L redis 127.0.0.1:6379> hset d-110 field 3 1L redis 127.0.0.1:6379> sort sort-input by d-*->field [’15’, ’110’, ’7’, ’23’] redis 127.0.0.1:6379> sort sort-input by d-*->field’ get d-*->field [’1’, ’3’, ’5’, ’9’] www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 46/63
  • 50. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Transactions Transactions www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 47/63
  • 51. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Transactions Transactions Pipeline of commands: When redis receive the command MULTI it store the following commands When it receive EXEC it execute all the commands receivend in a single transaction MULTI <command1> <command2> <command3> EXEC www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 48/63
  • 52. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Transactions optimistic locking if the data is changed before EXEC the client is notified of the update and can change is action accordingly UNWATCH ¡key¿ can be used after a WATCH and before a MULTI to cancel the optimistic locking DISCARD ¡key¿ can be used after a WATCH and MULTI to cancel the optimistic locking and the transaction WATH <key> MULTI <command1> <command2> <command3> EXEC www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 49/63
  • 53. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Expiration Expiration www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 50/63
  • 54. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Expiration Expiration expiration on duration or point in time of a key and all of it’s content EXPIRE: set the expiration duration in seconds EXPIREAT: set the expiration time with a unix timestamp TTL: return the amount of time remaining before a key will expire PERSIST: remove the expiration for a key PTTL: return the amount of time in milliseconds remaining before a key will expire PEXPIRE: set the expiration duration in milliseconds PEXPIREAT: set the expiration time with a unix timestamp in milliseconds www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 51/63
  • 55. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Publish / subscribe Publish / subscribe www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 52/63
  • 56. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Publish / subscribe Publish / subscribe A consumer receive all the data sent on a subscribed channel SUBSCRIBE: subscribe to a channel UNSUBSCRIBE: unsubscribe from a channel PUBLISH: publish message to a channel PSUBSCRIBE: subscribe to channels witha given pattern PUNSUBSCRIBE: unsubscribe to channels witha given pattern www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 53/63
  • 57. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Publish / subscribe warning 2 reasons to not use it: if client is not reading or too slowly it could cause old Redis version to slow down or crash if there is network failure the message sent during the failure will be lost Use blocking list instead or a real MQ application www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 54/63
  • 58. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Scripting Scripting www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 55/63
  • 59. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Scripting Scripting Redis 2.6 introduce scripting with Lua scriping language wins: execute complexe code on server-side implement missing functionnalities like locking performance drawbacks: block during execution all other client request (single thread) type mapping is not complete can’t named a script use sha1 digest instead www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 56/63
  • 60. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis in your applications www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 57/63
  • 61. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis and PHP Redis and PHP www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 58/63
  • 62. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis and PHP The phpredis driver The most widely used solution to connect to redis from PHP Distributed by PECL under the name redis Distributed on the Smile Hosting repositories apt-get install php5-redis Has support for sharding (”arrays”) Object-oriented interface www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 59/63
  • 63. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis and PHP PHP session handler [PHP] session.save_handler = redis session.save_path = "tcp://host1:6379?timeout=3,database=1" Multiple hosts can be given Timing out on the session manager is fatal www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 60/63
  • 64. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis and Magento Redis and Magento www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 61/63
  • 65. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Redis and Magento Redis and Magento Natively supported from Magento 1.13 Smile-provided module for Magento 1.12 Can be used as session manager Persistent sessions Replication Can be used as cache manager Advanced data types allow to store the entire cache No more two-level cache No more tags in databases Check the wiki for how-to and caveats www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 62/63
  • 66. NoSQL introduction Redis overview Installing and configuring Redis Types and commands Redis in your applica Questions ? www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 63/63