Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
Redis vs Memcached
Redis vs Memcached
Loading in …3
×
1 of 61

Redis 101

34

Share

Download to read offline

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Redis 101

  1. 1. REDIS 101 Desert Code Camp November, 2011 CardinalPath.com
  2. 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 yrs Twitter: @m2guru Http://github.com/phpguru CardinalPath.com
  3. 3. Redis 101 About Cardinal Path CardinalPath.com
  4. 4. Redis 101 About Cardinal Path CardinalPath.com
  5. 5. Redis 101 About Cardinal Path CardinalPath.com
  6. 6. Redis 101 Think of Google Instant search. Showing results with “as you type” latency enables a whole new class of use cases. Scaling Redis http://bit.ly/iSY04K CardinalPath.com
  7. 7. Redis 101 Redis can perform >100k+ SETs per second, and >80k+ GETs per second. (You're lucky to get over 6kt/s from MySQL.) Redis from the ground up http://bit.ly/cJfRov CardinalPath.com
  8. 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.com Everything about Redis 2.4 http://bit.ly/qZRYHw CardinalPath.com
  9. 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. 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. 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. 12. Redis 101 Overview of Caching Types of caches • Disk drive cache Mechanical / component • Browser cache client-side / local • Server cache Server-side / remote CardinalPath.com
  13. 13. Redis 101 Overview 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. 14. Redis 101 Overview 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. 15. Redis 101 Overview of Caching PHP 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. 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. 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. 18. Redis 101 What 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. 19. Redis 101 What makes Redis Unique? APC Memcache Redis Strings ✔ ✔ ✔ Hashes ✔ Lists ✔ Sets ✔ Disk-backed ✔ Replication ✔ Op-code ✔ CardinalPath.com
  20. 20. Redis 101 Installation Download, 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 $ make Need Linux? VirtualBox Change 2.4.2 to the latest version CardinalPath.com
  21. 21. Redis 101 Check Installation Start Redis like so: redis-server MacbookPro: geoffh$ redis-server [40306] 03 Nov 20:00:59 # Warning: no config file specified, using the 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 accept connections on port 6379 [40306] 03 Nov 20:00:59 - 0 clients connected (0 slaves), 922064 bytes in use [40306] 03 Nov 20:01:04 - 0 clients connected (0 slaves), 922064 bytes in use CardinalPath.com
  22. 22. Redis 101 Check Installation You can interact with Redis using the built-in client: $ src/redis-cli redis> set foo bar OK redis> get foo "bar" CardinalPath.com
  23. 23. Redis 101 Basic Usage - Strings $ src/redis-cli redis> set foo bar OK redis> get foo "bar" CardinalPath.com
  24. 24. Redis 101 Basic Usage - Strings $ src/redis-cli <?php redis> 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. 25. Redis 101 Basic Usage - Strings $ src/redis-cli redis> set bar "{"a":"1","b":"2"}" OK redis> get bar "{"a":"1","b":"2"}" CardinalPath.com
  26. 26. Redis 101 Basic Usage - Strings $ src/redis-cli redis> set bar "{"a":"1","b":"2"}" OK redis> 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. 27. Redis 101 Basic 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. 28. Redis 101 Basic Usage - Strings At this point you now know (basically) everything you need to use Memcache and/or APC. CardinalPath.com
  29. 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. 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. 31. Redis 101 But with Redis, there's so much more you can do. CardinalPath.com
  32. 32. Redis 101 Before 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. 33. Redis 101 Why are we excited about a bg save? If you're using Memcache or APC, you may know what a cache "warm up" is. You've implemented on-demand in-memory caching, which means that your app really hums... once a ton of stuff is in the cache. BUT... if memcache goes down, your DB server is about to get hammered, until the most frequently- cached objects get rebuilt and recached. Cache warm-up is the time it takes for your app to rebuild its cache. CardinalPath.com
  34. 34. Redis 101 Why 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" on disk in seconds. You can also backup the AOF... Or Replicate it... CardinalPath.com
  35. 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, Creator Everything about Redis 2.4 http://bit.ly/qZRYHw CardinalPath.com
  36. 36. Redis 101 Basic Usage – More with Strings APPEND – Make a string longer GET – Fetch a string at key GETRANGE – Fetch a substring of key's value GETSET – Get and Set in one operation MGET – Multiple Gets MSET – Multiple Sets MSETNX – SET Multiple if Not eXists SET – SET a value if not exists SETEX – SET a value that EXpires SETNX – Set if Not eXists SETRANGE – Rewrite a substring STRLEN – Integer length of string CardinalPath.com
  37. 37. Redis 101 Basic 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. 38. Redis 101 Basic 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. 39. Redis 101 Basic Usage - Keys redis> keys b* 1) "baz" 2) "bar" Memcache & APC don't have this feature! You have to remember all your own keys! (Use sparingly, though; keys is expensive.) CardinalPath.com
  40. 40. Redis 101 Basic Usage - Lists Lists are to Redis, as arrays are to PHP. Duplicates are OK. CardinalPath.com
  41. 41. Redis 101 Basic 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. 42. Redis 101 Basic 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. 43. Redis 101 Basic Usage – More with Lists LINDEX – Return the list item at a certain position LINSERT – Insert item into list BEFORE|AFTER item LLEN – Number of list items (length) LRANGE – Return some of the items LREM – Remove one or more items LSET – Sets the list element at index to value LTRIM – Lop off some items LPOP, LPUSH, LPUSHX – Left pop/push RPOP, RPUSH, RPUSHX, RPOPLPUSH – Right pop/push BLPOP, BRPOP, BRPOPLPUSH – Blocking pop/push CardinalPath.com
  44. 44. Redis 101 Basic Usage - Sets Sets are to Redis, as arrays are to PHP.* * NO DUPLICATES ALLOWED! CardinalPath.com
  45. 45. Redis 101 Basic 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. 46. Redis 101 Basic Usage - Sets SADD – Add a member to a set SCARD – Set CARDinality – Number of set members SDIFF – Calculate difference between 2 sets SDIFFSTORE – Store the Difference of 2 sets in a set SINTER – Calculate the intersection between 2 sets SINTERSTORE – Store the intersection of 2 sets in a set SISMEMBER – Bool test whether X is a member of set S SMEMBERS – list out all the members SMOVE – Move a member from a set to another set SPOP – Pop a member off from a set SRANDMEMBER – Fetch a random set member SREM – Remove a member from the set SUNION – Merge two sets together SUNIONSTORE – Store the merger of two sets in a set CardinalPath.com
  47. 47. Redis 101 Basic Usage - Hashes Hashes are to Redis, as associative arrays are to PHP. redis> hmset firsthash a "1" b "2" c "3" d "4" OK redis> hget firsthash c "3" redis> hset firsthash e "5" (integer) 1 redis> hget firsthash e "5" redis> hget firsthash d "4" CardinalPath.com
  48. 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. 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 is necessary to get one field from the hash map CardinalPath.com
  50. 50. Redis 101 Basic 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. 51. Redis 101 Basic Usage - Hashes HDEL – Delete a field & value from a hash HEXISTS – Bool test whether field is in hash HGET – fetch value stored under hash → field HGETALL – fetch the whole hash HINCRBY – Increment a value stored under a hash field HKEYS – keys as a set → array_keys( ) HLEN – Integer – Number of fields stored in hash HMGET – Array – Fetch multiple fields from hash HMSET – Set multiple fields in a hash HSET – Set one field in a hash HSETNX – Set a hash field if it doesn't exist HVALS – vals as a list → array_values( ) CardinalPath.com
  52. 52. Redis 101 Basic Usage – Pub/Sub redis> redis> subscribe vh1 redis> subscribe mtv Reading messages... (press Reading messages... (press Ctrl-C to quit) Ctrl-C to quit) 1) "subscribe" 1) "subscribe" 2) "vh1" 2) "mtv" 3) (integer) 1 3) (integer) 1 CardinalPath.com
  53. 53. Redis 101 Basic Usage – Pub/Sub redis> publish mtv "lady gaga" (integer) 1 redis> redis> subscribe vh1 redis> subscribe mtv Reading messages... (press Reading messages... (press Ctrl-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. 54. Redis 101 Basic Usage – Pub/Sub redis> publish mtv "lady gaga" (integer) 1 redis> publish vh1 "rihanna" (integer) 1 redis> subscribe vh1 redis> subscribe mtv Reading messages... (press Reading messages... (press Ctrl-C to quit) Ctrl-C to quit) 1) "subscribe" 1) "subscribe" 2) "vh1" 2) "mtv" 3) (integer) 1 3) (integer) 1 1) "message" 1) "message" 2) "vh1" 2) "mtv" 3) "rihanna" 3) "lady gaga" CardinalPath.com
  55. 55. Redis 101 Advanced Usage – Sorted Sets ZADD – Add a member to a set with a score ZCARD – Count how many members are in the sorted set ZCOUNT – Count how many elemenents between 2 scores ZINCRBY – Increase/Decrease a member's score ZINTERSTORE – Intersect on scores and store it ZRANGE – Return some members between 2 indexes ZRANGEBYSCORE – Return some members between 2 scores ZRANK – Returns a member index from Lo to Hi ZREM – Remove a sorted set's member ZREMRANGEBYRANK – Remove some members by rank ZREMRANGEBYSCORE – Remove some members by score ZREVRANGE – Fetch members by index in desc order ZREVRANGEBYSCORE – Fetch members by score in desc order ZREVRANK – Returns the index from Hi to Lo ZSCORE – Fetch the score of a member ZUNIONSTORE – Merge 2 sorted sets into a 3rd sorted set CardinalPath.com
  56. 56. Redis 101 Advanced Usage – Transactions MULTI, EXEC, DISCARD and WATCH are the foundation of transactions in Redis. They allow the execution of a group of commands in a single step, with two important guarantees: 1) All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single atomic operation. 2) Either all or none of the commands are executed. CardinalPath.com
  57. 57. Redis 101 Advanced Usage – Transactions MULTI – Start a transaction EXEC – Execute a transaction DISCARD – Ditch a transaction UNWATCH – Stop watching a key for changes WATCH – 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. 58. Redis 101 Advanced Usage – Redis Admin BGREWRITEAOF – Write backup Append-Only File BGSAVE – Perform an on-demand save CONFIG GET – Read configuration variable CONFIG RESETSTAT – Reset the statistics reported CONFIG SET – Set a configuration variable DBSIZE – Return the size of the Redis DB FLUSHALL – Erase everything! All databases FLUSHDB – Erase the current Redis Database INFO – General statistics LASTSAVE – Unix Timestamp of last successful save MONITOR – Watch Redis in real time (via telnet) SAVE – Save (not in the background) SHUTDOWN – Turn off the Redis Server SLAVEOF – Slave to a master Redis Server SLOWLOG – See where Redis is slowing down CardinalPath.com
  59. 59. Redis 101 Redis Resources Redis Sharding at Craigslist http://bit.ly/gVx0B4 Storing hundreds of millions of key value pairs in Redis http://bit.ly/tayAXy Redis-DB Google Group http://bit.ly/RBhvG CardinalPath.com
  60. 60. Redis 101 Documentation • http://redis.io/ • http://redis.io/commands Try Redis – in real time • http://try.redis-db.com CardinalPath.com
  61. 61. Redis 101 Similar / Related NoSQL MongoDB Cassandra CouchDB LevelDB ZeroMQ NodeJS ActiveMQ RabbitMQ CardinalPath.com

×