Redis And python at pycon_2011Presentation Transcript
PyCon India, 2011 Sunil Arora Redis And Python
Raising Hands... How many of you have used Redis before ? How many of you have got a laptop ?
About Me
I tweet at Sunil Arora / @_sunil_
I work at ShopSocially
I blog at http://sunilarora.org
Today's talk
What is Redis
How it works and what you can do with it Real life use-cases Real life use-cases Some hand-on with Redis
Redis - Brief History Initially written to improve performance of Web Analytics product LLOOGG out of his startup Salvatore Sanfilippo @antirez http://antirez.com
Redis – Brief History
Released in March 2009 (Open Source, BSD licensed)
VMWare hired Salvatore in March, 2010
Then Pieter Noordhuis (key contributor) was hired
What is Redis ? Its between lot of stuff, so difficult to categorize it precisely
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/
Redis is... Remote Data Structure Server
Redis is...
Supports rich data types of computer science
- Strings, Lists, Sets, Sorted Sets, Hashes...
Rich sets of primitives (commands) to manipulate these types
Predictive complexity measurements
A few fundamentals
Written in C (no external dependency)
Uses memory as main storage
Uses disk for persistence
Single Threaded
Every command performs atomic execution
Performance
Screamingly fast performance
~50K read/write operations per seconds
100K+ read/write ops per second on a regular EC2 instance
Installation
$ git clone http://github.com/antirez/redis
$ cd redis
$ make
$ ./src/redis-server
..........
..........
$./src/redis-cli
Redis> PING
PONG
Python Libraries
Redis-py - Python client library
Txredisapi - An asynchronous Python client for the Redis database, based on Twisted.
Redisco - ORM for redis along the lines Ohm for Ruby
redis-py
The most popular python client library
Andy McCurdy ( [email_address] )
Github: http://github.com/andymccurdy/redis-py
$easy_install redis OR
$pip install redis
Optional
$easy_install hiredis
$pip install hiredis
Lets get started...
$ cd redis
$ ./src/redis-server
.........
>>> from redis import Redis
>>> redis_client = Redis()
>>> redis_client.keys()
>>> help(redis_client)
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”
Which of my friend's are online right now? http://www.lukemelia.com/blog/archives/2010/01/17/redis-in-practice-whos-online/ SINTER online_people my_friends
Which of my friends are online right now?
>>>RENAME online:fresh online:stale #every minute
>>>SUNIONSTORE online:fresh online:stale
>>>#friend jack connects
>>>SADD online:fresh 'jack'
>>>SUNIONSTORE online online:fresh online:stale
>>>#who is online ?
>>>SINTER online friends
Sorted Sets Ordered sets on the basis of score
Sorted Sets
ZADD
ZCARD
ZCOUNT
ZINCRBY
ZINTERSTORE
ZRANGE
ZRANGEBYSCORE
ZRANK
ZREM
ZREMRANGEBYRANK
ZREMRANGEBYSCORE
ZREVRANGE
ZREVRANGEBYSCORE
ZSCORE
ZUNIONSTORE
Sorted Sets – Use Case To build index on your dataset