Download presentation
Presentation is loading. Please wait.
Published byCornelius Rich Modified over 9 years ago
1
Redis And Python PyCon India, 2011 (Work in Progress) Sunil Arora
2
Raising Hands... How many of you have used Redis before ? How many of you have got a laptop here ?
3
Who Sunil Arora / @_sunil_ Work for ShopSocially (http://shopsocially.com)http://shopsocially.com Interests: Backend, Frontend, scaling, internet technologies in general, startups... all kind of shit
4
Who Sunil Arora / @_sunil_ Work for ShopSocially (http://shopsocially.com)http://shopsocially.com Interests: Backend, Frontend, scaling, internet technologies in general, startups...
5
Today's talk What is Redis How it works and what you can do with it Real life use-cases
6
What is Redis ? Its between lot of stuff, so difficult to categorize it precisely
7
What is Redis ? 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 Picture by herzogbr http://www.flickr.com/photos/herzogbr/2274372747/sizes/z/in/photostream/
8
Brief History of Redis Released in March 2009 by Salvator Sanfilippo (@antirez) Open source, BSD licensed VMWare sponsored its development in March, 2010
9
A few fundamentals Written in C (25K LOC) Uses memory as main storage Single Threaded Uses disk for persistence Screamingly fast performance 50K read/write operations per seconds 200K read/write ops per second on a regular EC2 instance
10
Installation $ git clone http://github.com/antirez/redishttp://github.com/antirez/redis $ cd redis $ make $./src/redis-server..........
11
redis-py The most popular python client library Andy McCurdy (sedrik@gmail.com)sedrik@gmail.com Github: http://github.com/andymccurdy/redis-py easy_install redis OR pip install redis Optional: easy_install hiredis or pip install hiredis
12
Lets get started... $ cd redis $./src/redis-server......... >>> from redis import Redis >>> redis_client = Redis() >>> redis_client.keys() >>> help(redis_client)
13
Redis Keys Not binary safe. Should not contain space or newline character A few rules about keys: Too long keys are not a good idea Too short keys is also not a good idea “object-type:id:field” can be a nice idea, i.e. “user:1001:name”
14
Operations on Keys KEYS EXISTS DEL EXPIRE OBJECT PERSIST RANDOMKEY RENAME TYPE TTL EXPIREAT MOVE
15
Lets play with keys >>>redis_client.keys() >>>redis_client.exists('key') >>>redis_client.delete('key') >>>redis_client.type('key') >>>......
16
Data Structures Strings Lists Sets Sorted Sets Hashes
17
Strings SET GET MSET MGET SETEX INCR INCRBY DECR DECRBY
18
Strings – with redis client >>> redis_client.set('key', 'value') >>> redis_client.get('key') >>> redis_client.delete('key')
19
Fetch multiple keys at once mget/mset redis_client.mset({'key1': 'val1', 'key2': 'val2'}) redis_client.mget('key1', 'key2',......)
20
Expiration Set a value with expire >>>redis_client.setex('key', 'value', 2) #key to expire in 2 secs >>>redis_client.expire('key', 2) >>>redis_client.get('key') >>>None
21
Uses To store transient states in your web application
22
Uses Who is online?
23
Uses Redis as LRU cache (http://antirez.com/post/redis- as-LRU-cache.html)
24
Atomic Increments >>>help(redis_client.incr) >>>help(redis_client.decr) >>> >>> redis_client.incr('counter', 1) >>>1 >>> redis_client.incr('counter') >>>2 >>> redis_client.incr('counter') >>>3
25
Uses High Speed counters (views/clicks/votes/likes..)
26
Uses API Rate Limiting
27
Uses Generating unique IDs
28
Lists Ordered list of binarysafe strings Doubly linked list Memory footprint optimized for smaller list O(1) insertion/deletion at both ends
29
Lists - operations LPUSH RPUSH LSET LRANGE LPOP BLPOP BRPOP BRPOPLPUSH LINSERT RPOP RPOPLPUSH LPUSHX RPUSHX
30
Uses Web apps are full of lists :)
31
Uses Capped List
32
Uses Normal Queue Real time message Queue
33
Uses Social Activity Streams
34
Sets An unordered collection of distinct byte strings Nothing different from the data type in python
35
Sets - Operations SADD SCARD SDIFF SDIFFSTORE SINTER SINTERSTORE SISMEMBER SMEMBERS SMOVE SPOP SRANDMEMBER SREM SUNION SUNIONSTORE
36
Uses Picking random items from a set using SRANDMEMBER
37
Uses Primitive to construct filtering mechanism on items
38
Which of my friend's are online right now? Uses
39
Sorted Sets ZADD ZCARD ZCOUNT ZINCRBY ZINTERSTORE ZRANGE ZRANGEBYSCORE ZRANK ZREM ZREMRANGEBYRA NK ZREMRANGEBYSC ORE ZREVRANGE ZREVRANGEBYSCO RE ZSCORE ZUNIONSTORE
40
SORT SORT KEY SORT key BY pattern (e.g. object-type:*:age) SORT key LIMIT 0 10 SORT key GET user:*:username SORT key BY pattern STORE dstkey
41
Uses Generating sorted transient views
42
Publish/Subscribe A simple and efficient implementation of publish/subscribe messaging paradigm Client can subscribe/psubscribe to receive messages on channels (key patterns)
43
Publish/Subscribe PSUBSCRIBE PUBLISH PUNSUBSCRIBE SUBSCRIBE UNSUBSCRIBE
44
Uses Many to Many message passing
45
Uses Web Chat
46
Questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.