Redis - Usability and Use Cases

Post on 12-Feb-2017

281 views 0 download

Transcript of Redis - Usability and Use Cases

Fabrizio Farinacci

!!!!i!

i iiiiii i iii iii

i i i i sssss

1

1. What is Redis?REmote DIctionary Server

2

“ is an in-memory data structure store, that can be served as a database, cache or message broker (Pub/Sub).

3

?!?i?eii!i?!i?ei!

4

… a ‘‘data structure server’’?

Redis is essentially a key-value database… but it’s NOT a plain key-value database: it’s not limited to string values, but can also hold complex data structures.

5

2. Supported Data Types

6

Strings

Command line:> set mystr fooOK> set myint 1OK> get mystr"foo"> append mystr foo(integer) 6> incr myint(integer) 2> mget mystr myint1) "foofoo"2) "2"

Are key-value pairs, to store strings or integers, with:□Common operations on strings (APPEND, STRLEN, exc.);□Atomic increment/decrement (INCR/DECR) on integers; □Get multiple values at once (MGET).

Python:>>> import redis>>> r = redis.StrictRedis(

host='localhost',port=6379, db=0

)>>> r.set('mystr', 'foo')True>>> r.set('myint', 1)True>>> r.get('mystr')'foo'>>> r.append('mystr', 'foo')6L>>> r.incr('myint')2>>> r.mget('mystr', 'myint')['foofoo', '2']

7

ListsAre linked-lists of strings: □Index-based access to the entries;□Insertion and deletion at head/tail,in constant-time (push/pop);□Trim/Range operations available.

8

Interactingwith Lists

Command line:> rpush mylist A(integer) 1> rpush mylist B(integer) 2> lpush mylist first(integer) 3> lrange mylist 0 ‐11) "first"2) "A"3) "B"> lrange mylist 1 31) "A"2) "B"

Python:>>> import redis>>> r = redis.StrictRedis(

host='localhost',port=6379, db=0

)>>> r.rpush('mylist', 'A')1L>>> r.rpush('mylist', 'B')2L>>> r.lpush('mylist', 'first')3L>>> r.lrange('mylist', 0, ‐1)['first', 'A', 'B']>>> r.lrange('mylist', 1, 3)['A', 'B']

9

SetsAre sets of strings:□Unordered collection of non-repeating elements;□Intersection/Union/Differencebetween multiple sets;□Membership test available.

10

Interactingwith Sets

Command line:> sadd myset A(integer) 1> sadd myset B(integer) 1> sadd myset2 C(integer) 1> sismember myset C(integer) 0> smembers myset1) "A"2) "B"> sunion myset myset21) "A"2) "B"3) "C"

Python:>>> import redis>>> r = redis.StrictRedis(

host='localhost',port=6379, db=0

)>>> r.sadd('myset', 'A')1>>> r.sadd('myset', 'B')1>>> r.sadd('myset2', 'C')1>>> r.sismember('myset', 'C')False>>> r.smembers('myset')set(['A', 'B']) >>> r.sunion('myset', 'myset2')set(['A', 'C', 'B'])

11

Sorted Set (ZSET)12

Are sorted sets of strings:□Collection of non-repeating elementssorted by floating-point numbers(the score) and lexicographically;□Range operations on score/lexicon;□Intersection/Union between sets.

Interacting with ZSETs

Command line:> zadd myzset 2 C(integer) 1> zadd myzset 3 D(integer) 1> zadd myzset 1 A(integer) 1> zadd myzset 2 B(integer) 1> zrange myzset 0 ‐11) "A" 2) "B" 3) "C" 4) "D"> zrangebyscore myzset 1 21) "A" 2) "B" 3) "C"> zrangebylex myzset (A [D 1) "B" 2) "C" 3) "D"

Python:>>> import redis>>> r = redis.StrictRedis(

host='localhost',port=6379, db=0

)>>> r.zadd('myzset', 2, 'C')1>>> r.zadd('myzset', 3, 'D') 1>>> r.zadd('myzset', A=1) 1>>> r.zadd('myzset', B=2) 1>>> r.zrange('myzset', 0, ‐1) ['A', 'B', 'C', 'D']>>> r.zrangebyscore('myzset', 1, 2) ['A', 'B', 'C'] >>> r.zrangebylex('myzset', '(A', '[D') ['B', 'C', 'D']

13

HashA map of field-value pairs:□Key-based access, specifyingselected field or fields;□To implement objects, specifyingthe name of the field and its value.

14

Interactingwith Hashes

Command line:> hset myhash key1 A(integer) 1> hmset myhash key2 B key3 COK> hget myhash key2"B"> hmget myhash key1 key31) "A"2) "C"> hgetall myhash1) "key1"2) "A"3) "key2"4) "B"5) "key3"6) "C"

Python:>>> import redis>>> r = redis.StrictRedis(

host='localhost',port=6379, db=0

)>>> r.hset('myhash', 'key1', 'A') 1L>>> r.hmset('myhash',

{'key2':'B', 'key3':'C'} ) True>>> r.hget('myhash', 'key2')'B'>>> r.hmget('myhash', 'key1', 'key3')  ['A', 'C']>>> r.hgetall('myhash'){'key3': 'C', 'key2': 'B', 'key1': 'A'}

15

AdditionalData Structures

Bitmap: A set of bit-oriented operations (e.g. GETBIT/SETBIT) to manipulate string values as blobs of size up to 512 MB.

HyperLogLog: A probabilistic data structure structure to estimate size of sets (i.e. counting unique elements) efficiently and in constant-space.

Geo: Geospatial items, stored as geospatial indexes (in sorted indexes). Support for distance based operations (eg. GEODIST) and radius queries(GEORADIUS). Available only in the BETA testing version (3.2.0).

16

3. Personal Project

17

RedisPub/Sub18

To implement the Publish/Subscribe paradigm:□Published messages (PUBLISH) are categorizedinto channels and pushed to all the subscribers(SUBSCRIBE).□Publisher and subscriber are completelydecoupled: advantages are high scalability anddynamic network features.

Place your screenshot here

□ Redis Pub/Sub channels are exploited as thematic channels(EG. Sport, Tv Shows, exc.).□ Users subscribe to the channels they’re interested in.□ Once the web-chat session is started the user can:■ Receive messages published on the channels of interest;■ Publish messages onto selected channels.

Redis SubsChat: A multi-thematic web-chat

19

How it hasbeen done?

import redis# At the beginning, to setup the Redis interface object r = redis.StrictRedis(host='localhost', port=6379, db=0) ...# When start is pressed, the session startsdef on_start():

pubsub = r.pubsub() # Disable Widgets and manage subscriptions...ui.checkBox.setEnabled(False)if ui.checkBox.isChecked(): # Setup the the handler for the subscriber tasks

pubsub.subscribe(**{str(ui.checkBox.text()): mess_handler}) ... # This is done for all the checklists...# Run the receiver tasks into a parallel thread ... thread = pubsub.run_in_thread(sleep_time=0.001)

...

20

How it hasbeen done? (2)

...# Handler that pushes the received message onto the web‐chat def mess_handler(message):QtCore.QMetaObject.invokeMethod(ui.textEdit, "append",QtCore.Q_ARG(str, str(message['data'])+'\n')) ... # To send a message when send is presseddef on_send(): 

# Get the info about the message from the UI msg = str(ui.textEdit_2.toPlainText()) if len(msg) > 0:

usrname = str(ui.lineEdit.text())if len(usrname) == 0:

usrname = '<Anonymous>' channel = str(ui.comboBox.currentText())ui.textEdit_2.clear() message = ‘%s [%s]: %s' % (usrname, channel, msg) # Publish the message onto the specified channel r.publish(channel, message)

...

21

How it hasbeen done? (3)

...def on_stop(): 

# Re‐enable the disabled widgets...ui.checkBox_2.setEnabled(True)... # This is done for all the checklists ...pubsub.close() 

... if __name__ == "__main__": 

# To setup the UI and make the application runimport sysapp = QtGui.QApplication(sys.argv)  MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow)  MainWindow.show() sys.exit(app.exec_())

22

Live Demo

Let’s see how it works!!

23

4. Use casesWhen should we use it?

24

Performances and usability

Redis is an in-memory database, persistent on disk.

PROs:□Faster reads and writes: allhappens in memory. Atransaction is consideredcommitted without the need ofwriting on the disk.□Simple complex datastructure manipulation: allis in memory; lower complexity.□Efficient persistencymanagement: snapshotting orjournal mode.

CONs:□Suitable for small datasets,of size up to memory capacity.□Not suitable for applicationwhere durability is a crucialaspect.

25

When itshould be used? We Should use it if:

□Small datasets thatfits in memory: veryhigh performance,similar to a cache.□Assumptions ondata structures andqueries: to takeadvantage of thesupported data types.□Realize a cachelayer: for example, tospeedup a conventionalRDBMS.

We Shouldn’t use it if:□Frequent schemachanges: a traditionalkey-value approach, withthe schema managed bythe application, would bepreferred.□Prototyping: don’twant to waste loads oftime in the design of thedatabase and have theapplication soon ready.□Durability criticalapplications: like seatbooking mechanism.

26

Who’s usingRedis?

And many others!

27

thanks!Any questions?

You can find me at:https://it.linkedin.com/in/fabrizio-farinacci-496679116

https://github.com/FabFari/redis-subschat

?

28