Redis is an open source advanced key-value store, created by antirez. Here is a quick overview of this awesome NoSql DB.
Like a swiss knife, Redis will help you by many ways : LRU cache, high scores, UID generator, queues, social feeds, autocomplete …
2. > GET agenda
> GET introduction
> SMEMBERS datatypes
> INFO commands
> CLIENT LIST
> PSUBSCRIBE performance?
> LRANGE features 0 3
persitence
transaction
replication
pubsub
> ZREVRANGE usecases 0 -1
> SEGFAULT
> ?
I see Redis definitely more as a flexible tool
that as a solution specialized to solve a
specific problem: his mixed soul of cache,
store, and messaging server shows this
very well - Salvatore Sanfilippo (antirez),
creator of Redis
2
3. Brief Introduction
• Redis is an open source (since March 2009), advanced key-value
store. It is often referred to as a data structure server since keys
can contain strings, hashes, lists, sets and sorted sets.
• Originally written in order to help address the scaling needs of
http://lloogg.com/, a web analytics startup, by Salvatore (antirez)
Sanfilippo.
• Redis is written in ANSI C and works in most POSIX systems like
Linux, *BSD, OS X and Solaris without external dependencies. Redis
project does not directly support Windows, however MSOpenTech
develops and maintains an Windows port targeting Win64.
3
4. Command Processing
http://redis.io/commands
• Protocol RESP
– clients communicate with the Redis using REdis Serialization Protocol
• Simple, Fast & Human readable
– Commands are processed in serial in a single thread (like Node.js)
• Superfast processing of commands (a few ms, sometimes less)
• If you want to use more CPUs, shard your data client-side
• Command groups
– Request/response
– Pipelined: Several requests / responses
– Transactions:
• Traditional: Several requests processed in order atomically
• Check-And-Set: Request(s), data manipulation in client, more
requests
– Scripts (>v2.6): execute client-defined Lua scripts in the server for
more complex actions 4
5. What is a Command ?
http://redis.io/commands
Command name + parameters >
Version >
Complexity (really important) >
Description >
Result/return value >
Example >
Approx. 160 available
commands 5
6. Data Types
List Set Sorted Set Hash
[A, B, C, D]
“A”
“B”
“C”
“D”
D
C
B
A
A:3
C:1
D:2
B:4
{A, B, C, D} {C:1, D:2, A:3, D:4}
“A”
“B”
“C”
“D”
field1
field2
field3
field4
{field1:“A”, field2:“B”…}
{value:score} {key:value}
Basically maps keys to values (Strings), but also contains more advanced data
structures here :
* New Data Structure since April, 1st : HyperLogLog 6
7. Popular .net clients
Many clients for nearly all languages
• ActionScript
• C / C++
• C#
• Clojure
• Lisp
• Dart
• Erlang
• Fancy
• Go
• Haskell
• haXe
• Io
• Java
• Lua
• Node.js
• Objective-C
• Perl
• PHP
• Python
• Ruby
• Scala
• Smalltalk
• Tcl
7
8. How fast is Redis ?
(note : it’s common a manage millions of keys)
Redis includes the redis-benchmark utility that simulates running commands done by
N clients at the same time sending M total queries.
• Example on Intel(R) Xeon(R) CPU E5520 @ 2.27GHz (without pipelining)
Commands are generally processed in a few ms. Network
bandwidth and latency usually have a direct impact on the
performance. CPU is another very important factor. 8
9. Redis Feature : Persistence
http://redis.io/topics/persistence
Redis provides a different range of persistence options: RDB, AOF and None.
• RDB
– Periodic, asynchronous dump to disk
– Can be every N changes or every N seconds
– Written to file in “dbfilename”.rdb (could be backup elsewhere)
– may loss several minutes during a crash
• AOF
– Copies incoming commands as it happens
– Log-based, redo whole steps since startup
– Still fast enough, but often limited by disk performance
– may loss a few seconds during a crash
• None
– Keep data only in Memory.
9
10. Redis Feature : Transaction
http://redis.io/topics/transactions
• Multiple actions can be put into a transaction
– execute multiple command in a single step.
– Either all of the commands or none are processed
• Command MULTI to start transaction
– EXEC to execute
– DISCARD to discard
– WATCH for Optimistic locking
• Guarantees:
– Executed in the defined order
– Atomic (all or nothing)
– No isolation
– Can use optimistic locking
> MULTI
OK
> INCR pageviews:count
QUEUED
> EXPIRE pageviews:count 60
QUEUED
> SADD pageviews http://google.fr
QUEUED
> EXPIRE pageviews 60
QUEUED
> EXEC
1) (integer) 4660294
2) (integer) 1
3) (integer) 569023
4) (integer) 1 10
11. Redis Feature : Replication
http://redis.io/topics/replication
• Redis uses classic master / slave replication
• Slaves can replicate to other slaves (in a graph-like structure)
• Replication does not block the master/slave (async)
• Boost scalability & allow redundancy
Scaling performance by using the replicas for intensive read
operations.
Making data redundant in multiple locations.
Offloading data persistency costs from the master by
delegating it to the slaves
> SLAVEOF PAR-REDIS01:6379
> SLAVEOF PAR-REDIS01:6379
> SLAVEOF PAR-REDIS01:6379
11
12. Redis Feature : PubSub
http://redis.io/topics/pubsub (my favorite)
• A client can subscribe some topics, and when someone publish topic matches the
interest (topic), redis send it to it.
• Clients can subscribe to channels (eg user:1245) or patterns (eg user:*)
• Subscribing is O(1), posting messages is O(n)
• Use cases : Think chats, real-time analytics, twitter, …
CLIENT 1
> PSUBSCRIBE user:*
psubscribe
user:*
1
…
//wait notification
…
pmessage
user:*
user:1000
themessage
EMITTER 1
> PUBLISH user:1001 themessage
1 //number of message sent
Keyspace Notifications (>2.8) allows client to receive events
affecting the Redis data set (commands affecting a given key,
expiration, any command processed, …)
2
12
14. Use case #2 : leaderboard
> ZADD leaderboard 1543 cybermaxs
> ZADD leaderboard 4564 nicocanicola
> ZADD leaderboard 8954 toto
> ZADD leaderboard 6164 oinant
> ZADD leaderboard 9642 cybermaxs
> ZADD leaderboard 4123 toto
> ZADD leaderboard 8713 maxs
How to get top 3 ?
> ZREVRANGE leaderboard 0 2
WITHSCORES
cybermaxs
9642
toto
8954
maxs
8713
How to get rank of player X ?
> ZREVRANK leaderboard oinant
3
How to get score of player X ?
> ZSCORE leaderboard toto
8954
How to add score to player ?
> ZINCRBY leaderboard 3000
nicocanicolas
7563
SELECT TOP 10 * FROM leaderboard
WHERE ... ORDER BY score DESC ?
14
15. > INCR questions:nextid
1234 //last question id was 1233
> HMSET question:1234 title “Are you Redis to go?"
asked_by “cybermaxs“ votes 0
OK //add question to redis
> LPUSH questions question:1234
1 //add to list
> SADD question:1234:tags redis nosql
2 //add to tags
// later upvote by a user
> HINCRBY question:1234 votes 1
1
> ZINCRBY tagged:redis 1 question:1
1
Use case #3 : StackExchange Clone
Lastest 200 questions for Homepage
> LRANGE questions 0 199
question:200
question:199
…
Get Votes for a question
> HMGET question:1234 votes
2
Get Tags for a question
> SMEMBERS question:1234
redis
nosql
Questions by Tags, Sorted by Votes
> ZREVRANGE tagged:redis 0 5
question:1234
question:486
…
Subscribe to all changes on a question
> SUBSCRIBE __key*question:1234
+ Socket.IO
/SignalR
15
16. Common uses cases
• Cache out-of-process
• Duplicate detector
• FIFO/LIFO Queue
• Priority Queue
• Distributed hashmap
– e.g. url shortener
• UID generator
• Game high scores
• Geolocation lookup
– lat & long -> city
• Real-time analytics
• Metrics DB
• API throttling (rate-
limits)
• Autocomplete
• Twitter
• digg / Hacker News
• Social activity feed
• …
16
22. We want our brands to be easy to use for
every gamer around the world.
Join us to make that happen.
Everything we do reflect our values
Come and work in a friendly atmosphere
based on trust & cooperation between IT
Teams.
Learn & Share with us
Friday tech trainings, BBL, Meetups,
Coding Dojo, Innovation Day & more
If you want to contribute to the success of
our group, look at all the challenges we
offer HERE
Want to be part of a great online gambling company?
Check out our Carreers account
on Stackoverflow
22
23. About Us
• Betclic Everest Group, one of the world leaders in online
gaming, has a unique portfolio comprising various
complementary international brands: Betclic, Everest, bet-at-
home.com, Expekt, Monte-Carlo Casino…
• Through our brands, Betclic Everest Group places expertise,
technological know-how and security at the heart of our
strategy to deliver an on-line gaming offer attuned to the
passion of our players. We want our brands to be easy to use
for every gamer around the world. We’re building our
company to make that happen.
• Active in 100 countries with more than 12 million customers
worldwide, the Group is committed to promoting secure and
responsible gaming and is a member of several international
professional associations including the EGBA (European
Gaming and Betting Association) and the ESSA (European
Sports Security Association).
23