Redis 101

  • 10,041 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
10,041
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
264
Comments
0
Likes
22

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. REDIS 101Desert Code Camp November, 2011 CardinalPath.com
  • 2. Redis 101 About Me • Geoffrey Hoffman • Advertising MSU • 20 yrs software dev • Web Apps for 15 yrs • MySQL Admin for 10 yrs • Scalability, H.A. Sys & Deployment for 5 yrsTwitter: @m2guru Http://github.com/phpguru CardinalPath.com
  • 3. Redis 101About Cardinal Path CardinalPath.com
  • 4. Redis 101About Cardinal Path CardinalPath.com
  • 5. Redis 101About Cardinal Path CardinalPath.com
  • 6. Redis 101 Think of Google Instant search. Showingresults with “as you type” latency enables a whole new class of use cases.Scaling Redis http://bit.ly/iSY04K CardinalPath.com
  • 7. Redis 101Redis can perform >100k+ SETs per second, and >80k+ GETs per second. (Youre lucky to get over 6kt/s from MySQL.)Redis from the ground up http://bit.ly/cJfRov CardinalPath.com
  • 8. Redis 101 We use Redis in every single project now. IMHO this is one of the most significant pieces of software to come down the pipeline in the last few years. Keep doing what you do @antirez! - kidvelop Comment @ antirez.comEverything about Redis 2.4 http://bit.ly/qZRYHw CardinalPath.com
  • 9. Redis 101 Instead of trying to keep track of text logs spread across multiple machines we just push any log line we are interested in onto a single Redis queue. Then, we have one process pop from that queue and write those log lines to disk. If that process or the logging server goes down, no big deal, events just queue up in Redis until the logging service is back online.How we use Redis at Bump http://bit.ly/gHJ89P CardinalPath.com
  • 10. Session Overview • About Me & Cardinal Path • Overview of Caching • What is Redis? • What makes Redis unique? • Try Redis • How to use Redis from PHP • Use Cases & Examples • Quotes, Links & Resources • Questions CardinalPath.com
  • 11. Redis 101 Overview of Caching • A cache (/ˈkæʃ/ kash) is a component that transparently stores data so that future requests for that data can be served faster.http://en.wikipedia.org/wiki/Cache CardinalPath.com
  • 12. Redis 101Overview of Caching Types of caches • Disk drive cache Mechanical / component • Browser cache client-side / local • Server cache Server-side / remote CardinalPath.com
  • 13. Redis 101Overview of Caching Caching Methods • SQL Cache Run queries less often • Cached files on disk Process code less frequently • In-Memory Cache Store frequently-used data CardinalPath.com
  • 14. Redis 101Overview of Caching PHP Caching Software • Smarty Templates Creates parsed files on disk • WordPress Caching Plugins W3 Total Cache, SuperCache • MVC Frameworks Symfony, Kohana, CodeIgniter, Zend CardinalPath.com
  • 15. Redis 101Overview of CachingPHP Caching Extensions• Memcache Key-value store, in memory (volatile)• APC The Alternative PHP Cache Key-value store (memory) & opcode cache (disk)• Redis Remote Dictionary Server Data store, in memory (disk-backed) CardinalPath.com
  • 16. Redis 101 What is Redis? Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.Http://redis.io CardinalPath.com
  • 17. Redis 101 What is Redis? Originally written by Salvatore Sanfilippo, he now works full-time on Redis, sponsored by VMWare. @antirez on Twitter.Http://redis.io CardinalPath.com
  • 18. Redis 101What makes Redis Unique? • simple to learn, lightweight text-based tcp protocol • very, very, very fast – in RAM • hashes, list, sets and sorted sets • widely supported (C, .NET, PHP, Java... more) • Disk-backed, Background writes! • Master-Slave configurations Redis Cluster coming soon... CardinalPath.com
  • 19. Redis 101What makes Redis Unique? APC Memcache Redis Strings ✔ ✔ ✔ Hashes ✔ Lists ✔ Sets ✔ Disk-backed ✔ Replication ✔ Op-code ✔ CardinalPath.com
  • 20. Redis 101InstallationDownload, extract and compile Redis with:$ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz$ tar xzf redis-2.4.2.tar.gz$ cd redis-2.4.2$ makeNeed Linux? VirtualBox Change 2.4.2 to the latest version CardinalPath.com
  • 21. Redis 101Check InstallationStart Redis like so: redis-serverMacbookPro: geoffh$ redis-server[40306] 03 Nov 20:00:59 # Warning: no config file specified, usingthe default config. In order to specify a config file use redis-server /path/to/redis.conf[40306] 03 Nov 20:00:59 * Server started, Redis version 2.2.11[40306] 03 Nov 20:00:59 * The server is now ready to acceptconnections on port 6379[40306] 03 Nov 20:00:59 - 0 clients connected (0 slaves), 922064bytes in use[40306] 03 Nov 20:01:04 - 0 clients connected (0 slaves), 922064bytes in use CardinalPath.com
  • 22. Redis 101Check InstallationYou can interact with Redis using the built-in client: $ src/redis-cli redis> set foo bar OK redis> get foo "bar" CardinalPath.com
  • 23. Redis 101Basic Usage - Strings$ src/redis-cliredis> set foo barOKredis> get foo"bar" CardinalPath.com
  • 24. Redis 101Basic Usage - Strings$ src/redis-cli <?phpredis> set foo bar $redis = new Redis();OK $redis->connect(127.0.0.1,6379);redis> get foo $redis->set(foo,bar);"bar" $stored = $redis->get(foo); echo $stored; CardinalPath.com
  • 25. Redis 101 Basic Usage - Strings$ src/redis-cliredis> set bar "{"a":"1","b":"2"}"OKredis> get bar"{"a":"1","b":"2"}" CardinalPath.com
  • 26. Redis 101 Basic Usage - Strings$ src/redis-cliredis> set bar "{"a":"1","b":"2"}"OKredis> get bar <?php"{"a":"1","b":"2"}" $redis = new Redis(); $redis->connect(127.0.0.1,6379); $data = array(a=>1,b=>2); $json = json_encode($data); $redis->set(bar,$json); $stored = $redis->get(bar); echo $stored; CardinalPath.com
  • 27. Redis 101Basic Usage - Strings $ src/redis-cli redis> set baz "a=1&b=2&c=3&d=4" OK reds> get baz "a=1&b=2&c=3&d=4" You get the idea. CardinalPath.com
  • 28. Redis 101Basic Usage - Strings At this point you now know (basically) everything you need to use Memcache and/or APC. CardinalPath.com
  • 29. Redis 101 Basic Usage - Strings At this point you now know (basically) everything you need to use Memcache and/or APC.$mc = new Memcache;$mc->set(a,b);$a = $mc->get(a);// $a = "b" CardinalPath.com
  • 30. Redis 101 Basic Usage - Strings At this point you now know (basically) everything you need to use Memcache and/or APC.$mc = new Memcache; $b = b;$mc->set(a,b); apc_add(a, $b);$a = $mc->get(a); $a = apc_fetch(a);// $a = "b" // $a == "b" CardinalPath.com
  • 31. Redis 101 But with Redis,theres so much more you can do. CardinalPath.com
  • 32. Redis 101Before we dive back into more Redis, check it out:our redis-server process has already forked a bg save.[40306] 03 Nov 21:00:59 - 0 clients connected (0 slaves), 922368 bytes in use[40306] 03 Nov 21:01:00 * 1 changes in 3600 seconds. Saving...[40306] 03 Nov 21:01:00 * Background saving started by pid 40865[40865] 03 Nov 21:01:00 * DB saved on disk[40306] 03 Nov 21:01:00 * Background saving terminated with success[40306] 03 Nov 21:01:04 - DB 0: 3 keys (0 volatile) in 4 slots HT. CardinalPath.com
  • 33. Redis 101Why are we excited about a bg save?If youre using Memcache or APC, you may know whata cache "warm up" is. Youve implemented on-demandin-memory caching, which means that your app reallyhums... once a ton of stuff is in the cache.BUT... if memcache goes down, your DB server isabout to get hammered, until the most frequently-cached objects get rebuilt and recached.Cache warm-up is the time it takes for your app torebuild its cache. CardinalPath.com
  • 34. Redis 101Why are we excited about a bg save?With Redis, you can restart your caching server(s)periodically at opportune times.Redis re-populates from the "Append Only File" ondisk in seconds. You can also backup the AOF... Or Replicate it... CardinalPath.com
  • 35. Redis 101 Yes, we added two million items into a list in 1.28 seconds, with a networking layer between us and the server. Just saying... - Salvatore Sanfilippo, CreatorEverything about Redis 2.4 http://bit.ly/qZRYHw CardinalPath.com
  • 36. Redis 101 Basic Usage – More with StringsAPPEND – Make a string longerGET – Fetch a string at keyGETRANGE – Fetch a substring of keys valueGETSET – Get and Set in one operationMGET – Multiple GetsMSET – Multiple SetsMSETNX – SET Multiple if Not eXistsSET – SET a value if not existsSETEX – SET a value that EXpiresSETNX – Set if Not eXistsSETRANGE – Rewrite a substringSTRLEN – Integer length of string CardinalPath.com
  • 37. Redis 101Basic Usage – More with Strings INCR, INCRBY, DECR, DECRBY, Atomic Counters! Atomic operation - prevents any other processor or I/O device from writing or reading memory until the operation is complete. CardinalPath.com
  • 38. Redis 101Basic Usage – More with Strings In fact, all of the built-in commands of Redis are atomic. And they can be chained together (pipelined) to create complex atomic transactions. CardinalPath.com
  • 39. Redis 101Basic Usage - Keys redis> keys b* 1) "baz" 2) "bar"Memcache & APC dont have this feature!You have to remember all your own keys! (Use sparingly, though; keys is expensive.) CardinalPath.com
  • 40. Redis 101Basic Usage - Lists Lists are to Redis, as arrays are to PHP. Duplicates are OK. CardinalPath.com
  • 41. Redis 101Basic Usage - Lists Lists are to Redis, as arrays are to PHP. redis> LPUSH mylist "world" (integer) 1 redis> LPUSH mylist "hello" (integer) 2 // [ world, hello ] redis> LRANGE mylist 0 -1 1) "hello" 2) "world" CardinalPath.com
  • 42. Redis 101Basic Usage - Lists Lists are to Redis, as arrays are to PHP. <?php $redis = new Redis(); $redis->connect(127.0.0.1,6379); $redis->delete(key1); $redis->rpush(key1, A); // returns 1 $redis->rpush(key1, B); // returns 2 $redis->rpush(key1, C); // returns 3 $redis->rpush(key1, A); // returns 4 /* key1 now points to the following list: [ A, B, C, A ] */ CardinalPath.com
  • 43. Redis 101 Basic Usage – More with ListsLINDEX – Return the list item at a certain positionLINSERT – Insert item into list BEFORE|AFTER itemLLEN – Number of list items (length)LRANGE – Return some of the itemsLREM – Remove one or more itemsLSET – Sets the list element at index to valueLTRIM – Lop off some itemsLPOP, LPUSH, LPUSHX – Left pop/pushRPOP, RPUSH, RPUSHX, RPOPLPUSH – Right pop/pushBLPOP, BRPOP, BRPOPLPUSH – Blocking pop/push CardinalPath.com
  • 44. Redis 101Basic Usage - Sets Sets are to Redis, as arrays are to PHP.* * NO DUPLICATES ALLOWED! CardinalPath.com
  • 45. Redis 101Basic Usage - Sets Sets are to Redis, as arrays are to PHP. <?php $redis = new Redis(); $redis->connect(127.0.0.1,6379); $redis->delete(key2); $redis->sadd(key2, A); // returns 1 $redis->sadd(key2, B); // returns 2 $redis->sadd(key2, C); // returns 3 $redis->sadd(key2, A); // returns false /* key2 now points to the following list: [ A, B, C ] */ CardinalPath.com
  • 46. Redis 101 Basic Usage - SetsSADD – Add a member to a setSCARD – Set CARDinality – Number of set membersSDIFF – Calculate difference between 2 setsSDIFFSTORE – Store the Difference of 2 sets in a setSINTER – Calculate the intersection between 2 setsSINTERSTORE – Store the intersection of 2 sets in a setSISMEMBER – Bool test whether X is a member of set SSMEMBERS – list out all the membersSMOVE – Move a member from a set to another setSPOP – Pop a member off from a setSRANDMEMBER – Fetch a random set memberSREM – Remove a member from the setSUNION – Merge two sets togetherSUNIONSTORE – Store the merger of two sets in a set CardinalPath.com
  • 47. Redis 101Basic Usage - Hashes Hashes are to Redis, as associative arrays are to PHP.redis> hmset firsthash a "1" b "2" c "3" d "4"OKredis> hget firsthash c"3"redis> hset firsthash e "5"(integer) 1redis> hget firsthash e"5"redis> hget firsthash d"4" CardinalPath.com
  • 48. Redis 101 Basic Usage - Hashes Hashes are to Redis, as associative arrays are to PHP.<?php$redis = new Redis();$redis->connect(127.0.0.1,6379);$redis->delete(user:1);$redis->hmset(user:1, array(name => Joe, salary => 2000) );// Give Joe a $100 Raise:$redis->hincrby(user:1, salary, 100); CardinalPath.com
  • 49. Redis 101 Basic Usage - Hashes Hashes are to Redis, as associative arrays are to PHP. Hashes are also great representations of Database table rows, simple objects,JSON packets... and you can use Redis to store JSON encoded strings, or serialized objects, as simple key-value pairs. But with hashes, No de-serialization isnecessary to get one field from the hash map CardinalPath.com
  • 50. Redis 101Basic Usage - Hashes<?php$redis = new Redis();$redis->connect(127.0.0.1,6379);$redis->hmset(user:2, array(name => Sally, salary => 5000));$redis->hmset(user:3, array(name => Bill, salary => 6000));$uid = 3;$user = $redis->hgetall(user:.$uid)// {name:Bill,salary:6000}echo $user[salary]; // 6000 CardinalPath.com
  • 51. Redis 101 Basic Usage - HashesHDEL – Delete a field & value from a hashHEXISTS – Bool test whether field is in hashHGET – fetch value stored under hash → fieldHGETALL – fetch the whole hashHINCRBY – Increment a value stored under a hash fieldHKEYS – keys as a set → array_keys( )HLEN – Integer – Number of fields stored in hashHMGET – Array – Fetch multiple fields from hashHMSET – Set multiple fields in a hashHSET – Set one field in a hashHSETNX – Set a hash field if it doesnt existHVALS – vals as a list → array_values( ) CardinalPath.com
  • 52. Redis 101 Basic Usage – Pub/Sub redis>redis> subscribe vh1 redis> subscribe mtvReading messages... (press Reading messages... (pressCtrl-C to quit) Ctrl-C to quit)1) "subscribe" 1) "subscribe"2) "vh1" 2) "mtv"3) (integer) 1 3) (integer) 1 CardinalPath.com
  • 53. Redis 101 Basic Usage – Pub/Sub redis> publish mtv "lady gaga" (integer) 1 redis>redis> subscribe vh1 redis> subscribe mtvReading messages... (press Reading messages... (pressCtrl-C to quit) Ctrl-C to quit)1) "subscribe" 1) "subscribe"2) "vh1" 2) "mtv"3) (integer) 1 3) (integer) 1 1) "message" 2) "mtv" 3) "lady gaga" CardinalPath.com
  • 54. Redis 101 Basic Usage – Pub/Sub redis> publish mtv "lady gaga" (integer) 1 redis> publish vh1 "rihanna" (integer) 1redis> subscribe vh1 redis> subscribe mtvReading messages... (press Reading messages... (pressCtrl-C to quit) Ctrl-C to quit)1) "subscribe" 1) "subscribe"2) "vh1" 2) "mtv"3) (integer) 1 3) (integer) 11) "message" 1) "message"2) "vh1" 2) "mtv"3) "rihanna" 3) "lady gaga" CardinalPath.com
  • 55. Redis 101 Advanced Usage – Sorted SetsZADD – Add a member to a set with a scoreZCARD – Count how many members are in the sorted setZCOUNT – Count how many elemenents between 2 scoresZINCRBY – Increase/Decrease a members scoreZINTERSTORE – Intersect on scores and store itZRANGE – Return some members between 2 indexesZRANGEBYSCORE – Return some members between 2 scoresZRANK – Returns a member index from Lo to HiZREM – Remove a sorted sets memberZREMRANGEBYRANK – Remove some members by rankZREMRANGEBYSCORE – Remove some members by scoreZREVRANGE – Fetch members by index in desc orderZREVRANGEBYSCORE – Fetch members by score in desc orderZREVRANK – Returns the index from Hi to LoZSCORE – Fetch the score of a memberZUNIONSTORE – Merge 2 sorted sets into a 3rd sorted set CardinalPath.com
  • 56. Redis 101 Advanced Usage – TransactionsMULTI, EXEC, DISCARD and WATCH are the foundation oftransactions in Redis. They allow the execution of agroup of commands in a single step, with twoimportant guarantees:1) All the commands in a transaction are serializedand executed sequentially. It can never happen that arequest issued by another client is served in themiddle of the execution of a Redis transaction. Thisguarantees that the commands are executed as a singleatomic operation.2) Either all or none of the commands are executed. CardinalPath.com
  • 57. Redis 101 Advanced Usage – TransactionsMULTI – Start a transactionEXEC – Execute a transactionDISCARD – Ditch a transactionUNWATCH – Stop watching a key for changesWATCH – Start watching a key for changes Example: > MULTI OK > INCR foo QUEUED > INCR bar QUEUED > EXEC 1) (integer) 1 2) (integer) 1 CardinalPath.com
  • 58. Redis 101 Advanced Usage – Redis AdminBGREWRITEAOF – Write backup Append-Only FileBGSAVE – Perform an on-demand saveCONFIG GET – Read configuration variableCONFIG RESETSTAT – Reset the statistics reportedCONFIG SET – Set a configuration variableDBSIZE – Return the size of the Redis DBFLUSHALL – Erase everything! All databasesFLUSHDB – Erase the current Redis DatabaseINFO – General statisticsLASTSAVE – Unix Timestamp of last successful saveMONITOR – Watch Redis in real time (via telnet)SAVE – Save (not in the background)SHUTDOWN – Turn off the Redis ServerSLAVEOF – Slave to a master Redis ServerSLOWLOG – See where Redis is slowing down CardinalPath.com
  • 59. Redis 101Redis ResourcesRedis Sharding at Craigslist http://bit.ly/gVx0B4Storing hundreds of millions of key value pairs inRedis http://bit.ly/tayAXyRedis-DB Google Group http://bit.ly/RBhvG CardinalPath.com
  • 60. Redis 101Documentation• http://redis.io/• http://redis.io/commandsTry Redis – in real time• http://try.redis-db.com CardinalPath.com
  • 61. Redis 101Similar / RelatedNoSQL MongoDB CassandraCouchDB LevelDB ZeroMQNodeJS ActiveMQ RabbitMQ CardinalPath.com