pycon india, 2011 (work in progress) sunil arora

46
Redis And Python PyCon India, 2011 (Work in Progress) Sunil Arora

Upload: genero

Post on 15-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

PyCon India, 2011 (Work in Progress) Sunil Arora. Redis And Python. Raising Hands. How many of you have used Redis before ?. How many of you have got a laptop here ?. Who. Sunil Arora / @_sunil_ Work for ShopSocially ( http://shopsocially.com ) Interests: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: PyCon India, 2011 (Work in Progress) Sunil Arora

Redis And Python

PyCon India, 2011

(Work in Progress)Sunil Arora

Page 2: PyCon India, 2011 (Work in Progress) Sunil Arora

Raising Hands...

How many of you have used Redis before ?

How many of you have got a laptop here ?

Page 3: PyCon India, 2011 (Work in Progress) Sunil Arora

Who

Sunil Arora / @_sunil_ Work for ShopSocially (http://shopsocially.com) Interests:

Backend, Frontend, scaling, internet technologies in general, startups ... all kind of shit

Page 4: PyCon India, 2011 (Work in Progress) Sunil Arora

Who

Sunil Arora / @_sunil_ Work for ShopSocially (http://shopsocially.com) Interests:

Backend, Frontend, scaling, internet technologies in general, startups...

Page 5: PyCon India, 2011 (Work in Progress) Sunil Arora

Today's talk

What is Redis

How it works and what you can do with it

Real life use-cases

Page 6: PyCon India, 2011 (Work in Progress) Sunil Arora

What is Redis ?

Its between lot of stuff, so difficult to categorize it precisely

Page 7: PyCon India, 2011 (Work in Progress) Sunil Arora

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 herzogbrhttp://www.flickr.com/photos/herzogbr/2274372747/sizes/z/in/photostream/

Page 8: PyCon India, 2011 (Work in Progress) Sunil Arora

Brief History of Redis

Released in March 2009 by Salvator Sanfilippo (@antirez)

Open source, BSD licensed

VMWare sponsored its development in March, 2010

Page 9: PyCon India, 2011 (Work in Progress) Sunil Arora

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

Page 10: PyCon India, 2011 (Work in Progress) Sunil Arora

Installation

$ git clone http://github.com/antirez/redis

$ cd redis

$ make

$ ./src/redis-server

..........

..........

Page 11: PyCon India, 2011 (Work in Progress) Sunil Arora

redis-py

The most popular python client library Andy McCurdy ([email protected]) Github: http://github.com/andymccurdy/redis-py easy_install redis OR pip install redis Optional: easy_install hiredis or pip install

hiredis

Page 12: PyCon India, 2011 (Work in Progress) Sunil Arora

Lets get started...

$ cd redis

$ ./src/redis-server

.........

>>> from redis import Redis

>>> redis_client = Redis()

>>> redis_client.keys()

>>> help(redis_client)

Page 13: PyCon India, 2011 (Work in Progress) Sunil Arora

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”

Page 14: PyCon India, 2011 (Work in Progress) Sunil Arora

Operations on Keys

KEYS EXISTS DEL EXPIRE OBJECT PERSIST

RANDOMKEY RENAME TYPE TTL EXPIREAT MOVE

Page 15: PyCon India, 2011 (Work in Progress) Sunil Arora

Lets play with keys

>>>redis_client.keys()

>>>redis_client.exists('key')

>>>redis_client.delete('key')

>>>redis_client.type('key')

>>>......

>>>......

Page 16: PyCon India, 2011 (Work in Progress) Sunil Arora

Data Structures

Strings Lists Sets Sorted Sets Hashes

Page 17: PyCon India, 2011 (Work in Progress) Sunil Arora

Strings

SET GET MSET MGET SETEX

INCR INCRBY DECR DECRBY

Page 18: PyCon India, 2011 (Work in Progress) Sunil Arora

Strings – with redis client

>>> redis_client.set('key', 'value')

>>> redis_client.get('key')

>>> redis_client.delete('key')

Page 19: PyCon India, 2011 (Work in Progress) Sunil Arora

Fetch multiple keys at once

mget/mset redis_client.mset({'key1': 'val1', 'key2': 'val2'}) redis_client.mget('key1', 'key2',......)

Page 20: PyCon India, 2011 (Work in Progress) Sunil Arora

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

Page 21: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

To store transient states in your web application

Page 22: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Who is online?

Page 23: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Redis as LRU cache (http://antirez.com/post/redis-as-LRU-cache.html)

Page 24: PyCon India, 2011 (Work in Progress) Sunil Arora

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

Page 25: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

High Speed counters (views/clicks/votes/likes..)

Page 26: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

API Rate Limiting

Page 27: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Generating unique IDs

Page 28: PyCon India, 2011 (Work in Progress) Sunil Arora

Lists

Ordered list of binarysafe strings Doubly linked list Memory footprint optimized for smaller list O(1) insertion/deletion at both ends

Page 29: PyCon India, 2011 (Work in Progress) Sunil Arora

Lists - operations

LPUSH RPUSH LSET LRANGE LPOP BLPOP BRPOP BRPOPLPUSH

LINSERT RPOP RPOPLPUSH LPUSHX RPUSHX

Page 30: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Web apps are full of lists :)

Page 31: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Capped List

Page 32: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Normal QueueReal time message Queue

Page 33: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Social Activity Streams

Page 34: PyCon India, 2011 (Work in Progress) Sunil Arora

Sets

An unordered collection of distinct byte strings

Nothing different from the data type in python

Page 35: PyCon India, 2011 (Work in Progress) Sunil Arora

Sets - Operations

SADD SCARD SDIFF SDIFFSTORE SINTER SINTERSTORE SISMEMBER SMEMBERS

SMOVE SPOP SRANDMEMBER SREM SUNION SUNIONSTORE

Page 36: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Picking random items from a set using SRANDMEMBER

Page 37: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Primitive to construct filtering mechanism on items

Page 38: PyCon India, 2011 (Work in Progress) Sunil Arora

Which of my friend's are online right now?

Uses

Page 39: PyCon India, 2011 (Work in Progress) Sunil Arora

Sorted Sets

ZADD ZCARD ZCOUNT ZINCRBY ZINTERSTORE ZRANGE ZRANGEBYSCORE ZRANK

ZREM ZREMRANGEBYRA

NK ZREMRANGEBYSC

ORE ZREVRANGE ZREVRANGEBYSCO

RE ZSCORE ZUNIONSTORE

Page 40: PyCon India, 2011 (Work in Progress) Sunil Arora

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

Page 41: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Generating sorted transient views

Page 42: PyCon India, 2011 (Work in Progress) Sunil Arora

Publish/Subscribe

A simple and efficient implementation of publish/subscribe messaging paradigm

Client can subscribe/psubscribe to receive messages on channels (key patterns)

Page 43: PyCon India, 2011 (Work in Progress) Sunil Arora

Publish/Subscribe

PSUBSCRIBE PUBLISH PUNSUBSCRIBE SUBSCRIBE UNSUBSCRIBE

Page 44: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Many to Many message passing

Page 45: PyCon India, 2011 (Work in Progress) Sunil Arora

Uses

Web Chat

Page 46: PyCon India, 2011 (Work in Progress) Sunil Arora

Questions