View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
Why Should Redis Matter? Features/Use Case• Caching server, ( e.g. memcache equivalent)• Queue, (store user request that would take time to compute e.g. image resize on flickr/picasa)• User Clicks (Counting), (upvote/downvote on reddit)• Real Time Analytics for Stats, (No of users logged into chat )• Publish – Subscribe, ( e.g. Quora/Facebook Notification system)Modern web application require web developer to build software that responds inmilliseconds and scale as no of users/data/operations increases e.g. increase in pageviews.Yes, You can use a database like MySql to achieve all the above but wouldn’t it be faster to hitRAM then HDD ?.Using Redis developers can satisfy architectural paradigm found in modern web applicationdevelopment.
Who made Redis? Motivation, Support, FutureSalvatore Sanfilippo is founder developer of Redis since April2009,• Read Redis Manifesto to understand motivation behind redis,• Vmware is the sponsor of redis project with few employees working full time on redis,• Redis related questions on stackoverflow and answers thereof gives you a sense of community support and usage.• Why ? Beneficial to invest time mastering tools that are supported and will be there tomorrow.
Installing RedisLinux System – Installation from Source ● Go to http://redis.io/download official download page ● Download source code of Current Stable Release - 2.4.14 http://redis.googlecode.com/files/redis-2.4.14.tar.gz ● Extract the source code $ tar -xvf redis-2.4.14.tar.gz ● Redis is coded in C with dependencies being gcc and libc. If your computer doesnt have the same please use package manager and install the same. ● Execute the below commands in the directory where redis code is extracted $ make $ make test $ sudo make install ● You have Redis on your machine …
Running RedisAfter compilation you have following binaries generateda) redis-cli – Command line to talk to redis serverb) redis-server - Redis Serverc) redis-benchmark - is used to check Redis performancesd) redis-check-aof - Fix corrupted append only data files.e) redis-check-dump - Useful in the event of corrupted data files.Server$ redis-server OR$ redis-server /path/to/redisconfig.confCheck redis.conf for default redis configuration used when no separate config file isgiven. ( We will take a tour of the configuration file ahead )
Playing with redis-cliStart Server$ redis-serverHelp$ redis-cli --helpCan the command line client listen to server$ redis-cli pingStart redis-cli interactive mode$ redis-cliLet us try using creating the following data structures using redis-clia) Stringb) hashes,c) lists,d) sets,e) sorted sets.
String$ redis-cliredis 127.0.0.1:6379> set aKey aValueOKredis 127.0.0.1:6379> get aKey"aValue"redis 127.0.0.1:6379> set PyConDate "8th - 9th June 2012"OKredis 127.0.0.1:6379> get pycondate(nil)redis 127.0.0.1:6379> get PyConDate"8th - 9th June 2012"redis 127.0.0.1:6379> append PyConDate ": Singapore"(integer) 30redis 127.0.0.1:6379> get PyConDate"8th - 9th June 2012: Singapore"redis 127.0.0.1:6379> exists PyConDate(integer) 1
More Stringredis 127.0.0.1:6379> setex boardingpasstosingapore 10 "Ankur Gupta"OKredis 127.0.0.1:6379> get boardingpasstosingapore"Ankur Gupta"redis 127.0.0.1:6379> get boardingpasstosingapore(nil)redis 127.0.0.1:6379> set attendance "0"OKredis 127.0.0.1:6379> incr attendance(integer) 1redis 127.0.0.1:6379> get attendance"1"redis 127.0.0.1:6379> incr attendance(integer) 2redis 127.0.0.1:6379> get attendance"2"redis 127.0.0.1:6379> set attendance "A"OKredis 127.0.0.1:6379> incr attendance(error) ERR value is not an integer or out of range
Key 101 ?● Are Byte Array. Thus binary safe.● Obvious : Short key consume less memory vis a vi long keys. How verbose is good you decide. e.g. “authuser:14573:cacheenable” or “au:14573:ce”● Good practise to create your key naming convention in codebase and sticking to it. redis 127.0.0.1:6379> set " " "blank key ?" OK redis 127.0.0.1:6379> get " " "blank key ?" redis 127.0.0.1:6379> get " " (nil) redis 127.0.0.1:6379> set does work "no" (error) ERR wrong number of arguments for set command redis 127.0.0.1:6379> set "does work" "yes" OK
Exercisea) Download the file http://uptosomething.in/sghistorytimeline.csvb) Copy the first complete line from the file i.e. "3rd century","Early Chinese account of Singapore describes the island of PuLuo Chung" Take first column as key and second as value. Create a string using redis,c) Use the 6th and 7th line and create a list with 1819 as key and respective historicalevents as list elements,d) Use the 6th and 7th line and create a sorted set. Show events in reversechronological order,e) Use the line no 112 onwards till EOF and create a hash such that it takes Year ashashname, month+day as key, and historical event as value,f) Find what events happened in the month of june and create any datastructureyou please with them such that they expire when the event day is over. e.g. eventon june 12 will get over when june 13th arrives thus will expire.
Installing Redis Python Libraryhttps://github.com/andymccurdy/redis-pyredis-py is versioned after Redis. For example, redis-py 2.0.0should support all the commands available in Redis 2.0.0.$ cd andymccurdy-redis-py-d2a7156/$ python2.7 setup.py build$ sudo python2.7 setup.py installredis-py exposes two client classes that implement thesecommands. The StrictRedis class adhere to the officialcommand syntax/names with few exceptions. The library alsooffers another class that gives backward compatibility withcommands that are now deprecated.
Installing Redis Python Librarieshttps://github.com/andymccurdy/redis-pyredis-py is versioned after Redis. For example, redis-py 2.0.0should support all the commands available in Redis 2.0.0.$ cd andymccurdy-redis-py-d2a7156/$ python2.7 setup.py build$ sudo python2.7 setup.py installredis-py exposes two client classes that implement thesecommands. The StrictRedis class adhere to the officialcommand syntax/names with few exceptions. The library alsooffers another class that gives backward compatibility withcommands that are now deprecated.
Exercisea) Download the file http://uptosomething.in/sghistorytimeline.csvWrite a Python program that uses the redis module and try to do whatever coolidea comes in your mind with the above data and redis.
Creating Live Countersimport redisr = redis.StrictRedis(host=localhost, port=6379)def vote(objectid, is_up_vote): """ is_up_vote == true means up vote else down vote """ if not r.exists(objectid): r.set(objectid, 1) return 1 if is_up_vote: return r.incr(objectid, 1) else: return r.decr(objectid, 1)print vote("people who like the redis talk", True)print vote("people who like the redis talk", False)print vote("people who like the redis talk", True)print vote("people who like the redis talk", True)print vote("people who like the redis talk", False)
Real-time notification backend● Facebook User receives notification when his/her mention is made i.e. @user● So we have a) Message Text b) User who wrote the message c) Time-stamp when the message was written d) Other Users who were mentioned in that message.
Real-time notification backendLets denote● msgsourceuser = User who wrote the message msgtxt = Actual message timestamp = In seconds when the msh was written since epoch● Userlist = List of users for whom the message is meantIdeas● Hashes – Timestamp → user from UserList → msgtxt:msgsourceuser● Sorted Sets – Msgsourceuser → msgtxt:userlist → timestamp (score)● Redis Pub / Sub – ???
Real-time notification backend import redis rc = redis.Redis() psredis = rc.pubsub() rc.publish(statusupdate, In Singapore with @user,@user2,@user3)● ______________________________________________________________________ import redis rc = redis.Redis() psredis = rc.pubsub() psredis.subscribe(statusupdate) for item in psredis.listen(): # problem listen is blocking call print item[channel] print item[data]
Naive Redis Based Queue● Let us audit an open source redis queue implementation and see what is happening inside …
Auto-complete using redisSalvatore Sanfilippo Auto-completeimplementations code reviewhttps://gist.github.com/925979/b4689bfd8b0bc0408cd61efe674ea305f06524b9( The above is python port of original ruby code )
Redis In Production Environment● Daemon mode + init script● Server options and
Redis In Production Environment● Daemon mode + init script● Server options and config file