Here are a few things you could do with the Redis Python module and the Singapore history timeline data:1. Store each timeline entry as a hash with fields like "date", "event" etc and get/set/update entries.2. Maintain running counts of events by decade as sorted sets with decade as key and count as score. 3. Publish new timeline entries to a channel and have subscribers print/process the entries.4. Maintain a list of years and for each year, a hash of month numbers to event strings. 5. Expire timeline entries on their date so they only "exist" on that date. 6. Search/filter entries by date
Ähnlich wie Here are a few things you could do with the Redis Python module and the Singapore history timeline data:1. Store each timeline entry as a hash with fields like "date", "event" etc and get/set/update entries.2. Maintain running counts of events by decade as sorted sets with decade as key and count as score. 3. Publish new timeline entries to a channel and have subscribers print/process the entries.4. Maintain a list of years and for each year, a hash of month numbers to event strings. 5. Expire timeline entries on their date so they only "exist" on that date. 6. Search/filter entries by date
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuRedis Labs
Ähnlich wie Here are a few things you could do with the Redis Python module and the Singapore history timeline data:1. Store each timeline entry as a hash with fields like "date", "event" etc and get/set/update entries.2. Maintain running counts of events by decade as sorted sets with decade as key and count as score. 3. Publish new timeline entries to a channel and have subscribers print/process the entries.4. Maintain a list of years and for each year, a hash of month numbers to event strings. 5. Expire timeline entries on their date so they only "exist" on that date. 6. Search/filter entries by date (20)
Here are a few things you could do with the Redis Python module and the Singapore history timeline data:1. Store each timeline entry as a hash with fields like "date", "event" etc and get/set/update entries.2. Maintain running counts of events by decade as sorted sets with decade as key and count as score. 3. Publish new timeline entries to a channel and have subscribers print/process the entries.4. Maintain a list of years and for each year, a hash of month numbers to event strings. 5. Expire timeline entries on their date so they only "exist" on that date. 6. Search/filter entries by date
2. What is Redis?
Overview
• Few of the fundamental and commonly used data types in
software would be string, list, hash, set.
• Redis is an in-memory key-value store. It allows you to
add/update/delete the above data types as values to a key.
i.e. Key:Value aka “String”:<Data Type>.
Thus named as a data structure server.
• Redis is free, fast, easy to install ,scales well as more data is
inserted/operation performed.
• Following languages have libraries that communicate with redis
“ActionScript, C, C++, C#, Clojure, Common Lisp, Erlang, Go, Haskell, haXe, Io, Java, server-side
JavaScript (Node.js), Lua, Objective-C, Perl, PHP, Pure Data, Python, Ruby, Scala, Smalltalk and Tcl “
Wikipedia
3. 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 in
milliseconds 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 hit
RAM then HDD ?.
Using Redis developers can satisfy architectural paradigm found in modern web application
development.
4. Who made Redis?
Motivation, Support, Future
Salvatore Sanfilippo is founder developer of Redis since April
2009,
• 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.
6. Installing Redis
Linux 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
doesn't 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 …
7. Running Redis
After compilation you have following binaries generated
a) redis-cli – Command line to talk to redis server
b) redis-server - Redis Server
c) redis-benchmark - is used to check Redis performances
d) 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.conf
Check redis.conf for default redis configuration used when no separate config file is
given. ( We will take a tour of the configuration file ahead )
8. Playing with redis-cli
Start Server
$ redis-server
Help
$ redis-cli --help
Can the command line client listen to server
$ redis-cli ping
Start redis-cli interactive mode
$ redis-cli
Let us try using creating the following data structures using redis-cli
a) String
b) hashes,
c) lists,
d) sets,
e) sorted sets.
9. String
$ redis-cli
redis 127.0.0.1:6379> set aKey aValue
OK
redis 127.0.0.1:6379> get aKey
"aValue"
redis 127.0.0.1:6379> set PyConDate "8th - 9th June 2012"
OK
redis 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) 30
redis 127.0.0.1:6379> get PyConDate
"8th - 9th June 2012: Singapore"
redis 127.0.0.1:6379> exists PyConDate
(integer) 1
10. More String
redis 127.0.0.1:6379> setex boardingpasstosingapore 10 "Ankur Gupta"
OK
redis 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"
OK
redis 127.0.0.1:6379> incr attendance
(integer) 1
redis 127.0.0.1:6379> get attendance
"1"
redis 127.0.0.1:6379> incr attendance
(integer) 2
redis 127.0.0.1:6379> get attendance
"2"
redis 127.0.0.1:6379> set attendance "A"
OK
redis 127.0.0.1:6379> incr attendance
(error) ERR value is not an integer or out of range
11.
12. 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
18. Exercise
a) Download the file
http://uptosomething.in/sghistorytimeline.csv
b) Copy the first complete line from the file
i.e. "3rd century","Early Chinese account of Singapore describes the island of Pu
Luo 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 historical
events as list elements,
d) Use the 6th and 7th line and create a sorted set. Show events in reverse
chronological order,
e) Use the line no 112 onwards till EOF and create a hash such that it takes Year as
hashname, month+day as key, and historical event as value,
f) Find what events happened in the month of june and create any datastructure
you please with them such that they expire when the event day is over. e.g. event
on june 12 will get over when june 13th arrives thus will expire.
19. Installing Redis Python Library
https://github.com/andymccurdy/redis-py
redis-py is versioned after Redis. For example, redis-py 2.0.0
should 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 install
redis-py exposes two client classes that implement these
commands. The StrictRedis class adhere to the official
command syntax/names with few exceptions. The library also
offers another class that gives backward compatibility with
commands that are now deprecated.
20. Installing Redis Python Libraries
https://github.com/andymccurdy/redis-py
redis-py is versioned after Redis. For example, redis-py 2.0.0
should 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 install
redis-py exposes two client classes that implement these
commands. The StrictRedis class adhere to the official
command syntax/names with few exceptions. The library also
offers another class that gives backward compatibility with
commands that are now deprecated.
25. Exercise
a) Download the file
http://uptosomething.in/sghistorytimeline.csv
Write a Python program that uses the redis module and try to do whatever cool
idea comes in your mind with the above data and redis.
26. Creating Live Counters
import redis
r = 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)
27. 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.
28. Real-time notification backend
Lets 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 meant
Ideas
● Hashes
– Timestamp → user from UserList → msgtxt:msgsourceuser
● Sorted Sets
– Msgsourceuser → msgtxt:userlist → timestamp (score)
● Redis Pub / Sub
– ???
29. 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']
30. Naive Redis Based Queue
● Let us audit an open source redis
queue implementation and see what
is happening inside …
31. Auto-complete using redis
Salvatore Sanfilippo Auto-complete
implementation's code review
https://gist.github.com/925979/b4689bfd8b0bc0408cd61efe674ea305f06524b9
( The above is python port of original ruby code )
32. Redis In Production Environment
● Daemon mode + init script
● Server options and
33. Redis In Production Environment
● Daemon mode + init script
● Server options and config file