1 / 46

PyCon India, 2011 (Work in Progress) Sunil Arora

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:

cheryr
Download Presentation

PyCon India, 2011 (Work in Progress) Sunil Arora

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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) • Interests: Backend, Frontend, scaling, internet technologies in general, startups ... all kind of shit

  4. Who • Sunil Arora / @_sunil_ • Work for ShopSocially (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. Its between lot of stuff, so difficult to categorize it precisely What is Redis ?

  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/redis $ cd redis $ make $ ./src/redis-server .......... ..........

  11. redis-py • The most popular python client library • Andy McCurdy (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. RANDOMKEY RENAME TYPE TTL EXPIREAT MOVE Operations on Keys • KEYS • EXISTS • DEL • EXPIRE • OBJECT • PERSIST

  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. INCR INCRBY DECR DECRBY Strings • SET • GET • MSET • MGET • SETEX

  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. To store transient states in your web application Uses

  22. Who is online? Uses

  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. High Speed counters (views/clicks/votes/likes..) Uses

  26. API Rate Limiting Uses

  27. Generating unique IDs Uses

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

  29. LINSERT RPOP RPOPLPUSH LPUSHX RPUSHX Lists - operations • LPUSH • RPUSH • LSET • LRANGE • LPOP • BLPOP • BRPOP • BRPOPLPUSH

  30. Web apps are full of lists :) Uses

  31. Capped List Uses

  32. Normal Queue Real time message Queue Uses

  33. Social Activity Streams Uses

  34. Sets • An unordered collection of distinct byte strings • Nothing different from the data type in python

  35. SMOVE SPOP SRANDMEMBER SREM SUNION SUNIONSTORE Sets - Operations • SADD • SCARD • SDIFF • SDIFFSTORE • SINTER • SINTERSTORE • SISMEMBER • SMEMBERS

  36. Picking random items from a set using SRANDMEMBER Uses

  37. Primitive to construct filtering mechanism on items Uses

  38. Which of my friend's are online right now? Uses

  39. ZREM ZREMRANGEBYRANK ZREMRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE ZSCORE ZUNIONSTORE Sorted Sets • ZADD • ZCARD • ZCOUNT • ZINCRBY • ZINTERSTORE • ZRANGE • ZRANGEBYSCORE • ZRANK

  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. Generating sorted transient views Uses

  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. Many to Many message passing Uses

  45. Web Chat Uses

  46. Questions

More Related