mini-training: redis

23
ARE YOU REDIS TO GO ? MAXIME LEMAITRE – 10/04/2014 1

Upload: betclic-everest-group-tech-team

Post on 10-May-2015

2.758 views

Category:

Software


2 download

DESCRIPTION

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 …

TRANSCRIPT

Page 1: Mini-Training: Redis

ARE YOU REDIS TO GO ?

MAXIME LEMAITRE – 10/04/2014

1

Page 2: Mini-Training: Redis

> 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

Page 3: Mini-Training: Redis

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. Redisproject does not directly support Windows, however MSOpenTechdevelops and maintains an Windows port targeting Win64.

3

Page 4: Mini-Training: Redis

Command Processinghttp://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

Page 5: Mini-Training: Redis

What is a Command ?http://redis.io/commands

Command name + parameters >

Version >

Complexity (really important) >

Description >

Result/return value >

Example >

Approx. 160 availablecommands 5

Page 6: Mini-Training: Redis

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

Page 7: Mini-Training: Redis

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

Page 8: Mini-Training: Redis

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

Page 9: Mini-Training: Redis

Redis Feature : Persistencehttp://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

Page 10: Mini-Training: Redis

Redis Feature : Transactionhttp://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

Page 11: Mini-Training: Redis

Redis Feature : Replicationhttp://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

Page 12: Mini-Training: Redis

Redis Feature : PubSubhttp://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

Page 13: Mini-Training: Redis

Use case #1 : Who’s online ?

//cybermaxs has 3 friends

> SADD cybermaxs:friends maxs

> SADD cybermaxs:friends nicocanicola

> SADD cybermaxs:friends oinant

//cybermaxs connects

> SADD users:online cybermaxs

//maxs connects

> SADD users:online maxs

//oinant connects

> SADD users:online oinantWho’s online ?> SMEMBERS users:online

Maxs

oinant

cybermaxs

Are cybermaxs’s friends online ?> SINTER users:online cybermaxs:friends

maxs

oinantIs nicocanicola connected ?> SISMEMBERS users:online nicocanicola

0

How many connected users ?> SCARD users:online

3

13

Page 14: Mini-Training: Redis

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

Page 15: Mini-Training: Redis

> 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 5question:1234

question:486

Subscribe to all changes on a question> SUBSCRIBE __key*question:1234

+ Socket.IO/SignalR

15

Page 16: Mini-Training: Redis

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

Page 17: Mini-Training: Redis

Who is using Redis ?

17

Page 18: Mini-Training: Redis

Conclusion

18

Page 19: Mini-Training: Redis

Questions(mine : are you Redis to Go ?)

19

Page 20: Mini-Training: Redis

References

• http://redis.io/

• http://en.wikipedia.org/wiki/Redis

• http://try.redis.io/ (online introduction)

• http://redis.io/topics/data-types-intro

• http://highscalability.com/blog/2011/7/6/11-common-web-use-cases-solved-in-redis.html

• http://oldblog.antirez.com/post/take-advantage-of-redis-adding-it-to-your-stack.html

• http://mono.servicestack.net/docs/redis-client/designing-nosql-database

• http://fr.slideshare.net/dvirsky/kicking-ass-with-redis

• http://fr.slideshare.net/noahd1/redis-in-practice

20

Page 21: Mini-Training: Redis

Find out more

• On https://techblog.betclicgroup.com/

21

Page 22: Mini-Training: Redis

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 accounton Stackoverflow

22

Page 23: Mini-Training: Redis

About Us• Betclic Everest Group, one of the world leaders in online

gaming, has a unique portfolio comprising variouscomplementary 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 ourstrategy to deliver an on-line gaming offer attuned to thepassion of our players. We want our brands to be easy to usefor every gamer around the world. We’re building ourcompany to make that happen.

• Active in 100 countries with more than 12 million customersworldwide, the Group is committed to promoting secure andresponsible gaming and is a member of several internationalprofessional associations including the EGBA (EuropeanGaming and Betting Association) and the ESSA (EuropeanSports Security Association).

23