SlideShare uma empresa Scribd logo
1 de 61
Baixar para ler offline
REDIS 101


Desert Code Camp
 November, 2011




              CardinalPath.com
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
Redis 101



About Cardinal Path




                 CardinalPath.com
Redis 101



About Cardinal Path




                 CardinalPath.com
Redis 101



About Cardinal Path




                 CardinalPath.com
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
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
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
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
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
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
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
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
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
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
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
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
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
Redis 101



What makes Redis Unique?
               APC   Memcache      Redis
 Strings       ✔       ✔             ✔
 Hashes                              ✔
 Lists                               ✔
 Sets                                ✔
 Disk-backed                         ✔
 Replication                         ✔
 Op-code       ✔
                           CardinalPath.com
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
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
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
Redis 101



Basic Usage - Strings


$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"




                        CardinalPath.com
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
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
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
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
Redis 101



Basic Usage - Strings

    At this point you now know
  (basically) everything you need
    to use Memcache and/or APC.




                                 CardinalPath.com
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
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
Redis 101




        But with Redis,
there's so much more you can do.




                            CardinalPath.com
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
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
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
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
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
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
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
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
Redis 101



Basic Usage - Lists

           Lists are to Redis, as
              arrays are to PHP.


           Duplicates are OK.




                                CardinalPath.com
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
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
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
Redis 101



Basic Usage - Sets

           Sets are to Redis, as
             arrays are to PHP.*

           *
               NO DUPLICATES ALLOWED!




                                CardinalPath.com
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Redis 101




Documentation
• http://redis.io/
• http://redis.io/commands


Try Redis – in real time
• http://try.redis-db.com




                     CardinalPath.com
Redis 101




Similar / Related


NoSQL       MongoDB    Cassandra
CouchDB     LevelDB    ZeroMQ
NodeJS      ActiveMQ   RabbitMQ




                           CardinalPath.com

Mais conteúdo relacionado

Mais procurados

Caching solutions with Redis
Caching solutions   with RedisCaching solutions   with Redis
Caching solutions with RedisGeorge Platon
 
Redis Introduction
Redis IntroductionRedis Introduction
Redis IntroductionAlex Su
 
High-Volume Data Collection and Real Time Analytics Using Redis
High-Volume Data Collection and Real Time Analytics Using RedisHigh-Volume Data Collection and Real Time Analytics Using Redis
High-Volume Data Collection and Real Time Analytics Using Rediscacois
 
An Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdfAn Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdfStephen Lorello
 
Redis cluster
Redis clusterRedis cluster
Redis clusteriammutex
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in PracticeNoah Davis
 
Redis Overview
Redis OverviewRedis Overview
Redis OverviewHoang Long
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to RedisArnab Mitra
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2Dvir Volk
 
Amazon RDS for MySQL: Best Practices and Migration
Amazon RDS for MySQL: Best Practices and MigrationAmazon RDS for MySQL: Best Practices and Migration
Amazon RDS for MySQL: Best Practices and MigrationAmazon Web Services
 
How to Get Started With NGINX
How to Get Started With NGINXHow to Get Started With NGINX
How to Get Started With NGINXNGINX, Inc.
 
Découverte de Redis
Découverte de RedisDécouverte de Redis
Découverte de RedisJEMLI Fathi
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLMorgan Tocker
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slidesDocker, Inc.
 

Mais procurados (20)

Redis and it's data types
Redis and it's data typesRedis and it's data types
Redis and it's data types
 
Caching solutions with Redis
Caching solutions   with RedisCaching solutions   with Redis
Caching solutions with Redis
 
Redis Introduction
Redis IntroductionRedis Introduction
Redis Introduction
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
High-Volume Data Collection and Real Time Analytics Using Redis
High-Volume Data Collection and Real Time Analytics Using RedisHigh-Volume Data Collection and Real Time Analytics Using Redis
High-Volume Data Collection and Real Time Analytics Using Redis
 
An Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdfAn Introduction to Redis for Developers.pdf
An Introduction to Redis for Developers.pdf
 
Redis cluster
Redis clusterRedis cluster
Redis cluster
 
Redis in Practice
Redis in PracticeRedis in Practice
Redis in Practice
 
Redis Overview
Redis OverviewRedis Overview
Redis Overview
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
MySQL SQL Tutorial
MySQL SQL TutorialMySQL SQL Tutorial
MySQL SQL Tutorial
 
Introduction to redis - version 2
Introduction to redis - version 2Introduction to redis - version 2
Introduction to redis - version 2
 
Amazon RDS for MySQL: Best Practices and Migration
Amazon RDS for MySQL: Best Practices and MigrationAmazon RDS for MySQL: Best Practices and Migration
Amazon RDS for MySQL: Best Practices and Migration
 
How to Get Started With NGINX
How to Get Started With NGINXHow to Get Started With NGINX
How to Get Started With NGINX
 
Découverte de Redis
Découverte de RedisDécouverte de Redis
Découverte de Redis
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
 
Query logging with proxysql
Query logging with proxysqlQuery logging with proxysql
Query logging with proxysql
 
Redis database
Redis databaseRedis database
Redis database
 

Semelhante a Redis 101: An Introduction to Caching with Redis

Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012Ankur Gupta
 
(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep DiveAmazon Web Services
 
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Redis Labs
 
Cost Savings at High Performance with Redis Labs and AWS
Cost Savings at High Performance with Redis Labs and AWSCost Savings at High Performance with Redis Labs and AWS
Cost Savings at High Performance with Redis Labs and AWSAmazon Web Services
 
Secure Redis Cluster At Box: Vova Galchenko, Ravitej Sistla
Secure Redis Cluster At Box: Vova Galchenko, Ravitej SistlaSecure Redis Cluster At Box: Vova Galchenko, Ravitej Sistla
Secure Redis Cluster At Box: Vova Galchenko, Ravitej SistlaRedis Labs
 
Redis. Performance on custom searches. Production screw up
Redis. Performance on custom searches. Production screw upRedis. Performance on custom searches. Production screw up
Redis. Performance on custom searches. Production screw upPiotrWasiak5
 
This is redis - feature and usecase
This is redis - feature and usecaseThis is redis - feature and usecase
This is redis - feature and usecaseKris Jeong
 
What's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsWhat's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsRedis Labs
 
Handling Redis failover with ZooKeeper
Handling Redis failover with ZooKeeperHandling Redis failover with ZooKeeper
Handling Redis failover with ZooKeeperryanlecompte
 
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::ClientBuilding Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::ClientMike Friedman
 
Facing enterprise specific challenges – utility programming in hadoop
Facing enterprise specific challenges – utility programming in hadoopFacing enterprise specific challenges – utility programming in hadoop
Facing enterprise specific challenges – utility programming in hadoopfann wu
 
Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Dinh Pham
 
10 Ways to Scale with Redis - LA Redis Meetup 2019
10 Ways to Scale with Redis - LA Redis Meetup 201910 Ways to Scale with Redis - LA Redis Meetup 2019
10 Ways to Scale with Redis - LA Redis Meetup 2019Dave Nielsen
 
Getting started with Amazon ElastiCache
Getting started with Amazon ElastiCacheGetting started with Amazon ElastiCache
Getting started with Amazon ElastiCacheAmazon Web Services
 

Semelhante a Redis 101: An Introduction to Caching with Redis (20)

Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive(DAT407) Amazon ElastiCache: Deep Dive
(DAT407) Amazon ElastiCache: Deep Dive
 
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
Moving Beyond Cache by Yiftach Shoolman Redis Labs - Redis Day Seattle 2020
 
Redis
RedisRedis
Redis
 
Cost Savings at High Performance with Redis Labs and AWS
Cost Savings at High Performance with Redis Labs and AWSCost Savings at High Performance with Redis Labs and AWS
Cost Savings at High Performance with Redis Labs and AWS
 
Secure Redis Cluster At Box: Vova Galchenko, Ravitej Sistla
Secure Redis Cluster At Box: Vova Galchenko, Ravitej SistlaSecure Redis Cluster At Box: Vova Galchenko, Ravitej Sistla
Secure Redis Cluster At Box: Vova Galchenko, Ravitej Sistla
 
Redis. Performance on custom searches. Production screw up
Redis. Performance on custom searches. Production screw upRedis. Performance on custom searches. Production screw up
Redis. Performance on custom searches. Production screw up
 
This is redis - feature and usecase
This is redis - feature and usecaseThis is redis - feature and usecase
This is redis - feature and usecase
 
What's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis LabsWhat's new with enterprise Redis - Leena Joshi, Redis Labs
What's new with enterprise Redis - Leena Joshi, Redis Labs
 
Handling Redis failover with ZooKeeper
Handling Redis failover with ZooKeeperHandling Redis failover with ZooKeeper
Handling Redis failover with ZooKeeper
 
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::ClientBuilding Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::Client
 
REDIS327
REDIS327REDIS327
REDIS327
 
Redis
RedisRedis
Redis
 
Redis meetup
Redis meetupRedis meetup
Redis meetup
 
Facing enterprise specific challenges – utility programming in hadoop
Facing enterprise specific challenges – utility programming in hadoopFacing enterprise specific challenges – utility programming in hadoop
Facing enterprise specific challenges – utility programming in hadoop
 
KeyValue Stores
KeyValue StoresKeyValue Stores
KeyValue Stores
 
Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...Beyond relational database - Building high performance websites using Redis a...
Beyond relational database - Building high performance websites using Redis a...
 
Mini-Training: Redis
Mini-Training: RedisMini-Training: Redis
Mini-Training: Redis
 
10 Ways to Scale with Redis - LA Redis Meetup 2019
10 Ways to Scale with Redis - LA Redis Meetup 201910 Ways to Scale with Redis - LA Redis Meetup 2019
10 Ways to Scale with Redis - LA Redis Meetup 2019
 
Getting started with Amazon ElastiCache
Getting started with Amazon ElastiCacheGetting started with Amazon ElastiCache
Getting started with Amazon ElastiCache
 

Último

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

Redis 101: An Introduction to Caching with Redis

  • 1. REDIS 101 Desert 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 yrs Twitter: @m2guru Http://github.com/phpguru CardinalPath.com
  • 3. Redis 101 About Cardinal Path CardinalPath.com
  • 4. Redis 101 About Cardinal Path CardinalPath.com
  • 5. Redis 101 About Cardinal Path CardinalPath.com
  • 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. 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. 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. 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 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. 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. 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. 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. 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 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. Redis 101 What makes Redis Unique? APC Memcache Redis Strings ✔ ✔ ✔ Hashes ✔ Lists ✔ Sets ✔ Disk-backed ✔ Replication ✔ Op-code ✔ CardinalPath.com
  • 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. 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. 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. Redis 101 Basic Usage - Strings $ src/redis-cli redis> set foo bar OK redis> get foo "bar" CardinalPath.com
  • 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. 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. 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. 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. Redis 101 Basic 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, there's so much more you can do. CardinalPath.com
  • 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. 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. 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. 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. 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. 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. 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. 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. Redis 101 Basic Usage - Lists Lists are to Redis, as arrays are to PHP. Duplicates are OK. CardinalPath.com
  • 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. 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. 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. Redis 101 Basic Usage - Sets Sets are to Redis, as arrays are to PHP.* * NO DUPLICATES ALLOWED! CardinalPath.com
  • 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. 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. 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. 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 is necessary to get one field from the hash map CardinalPath.com
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Redis 101 Documentation • http://redis.io/ • http://redis.io/commands Try Redis – in real time • http://try.redis-db.com CardinalPath.com
  • 61. Redis 101 Similar / Related NoSQL MongoDB Cassandra CouchDB LevelDB ZeroMQ NodeJS ActiveMQ RabbitMQ CardinalPath.com