SlideShare uma empresa Scribd logo
1 de 25
Redis
REmote DIctionary Server
  Ryan Findley, Philly.rb, 2010-02-16
Disclaimer
• I’m not using this in production (yet).
Kind of like Memcache


• Key-value store
• All data lives in memory
• Keys can expire (or not)
• Fast, Light-weight
And More
•   Persistence

•   Multiple databases

•   Queryable keyspace

•   Support for integer counters

•   Higher level data structures

•   Atomic operations

•   Ability to paginate lists without mutating them

•   Master-slave replication

•   Optional VM feature (new)
More:
Command Reference: http://
code.google.com/p/redis/wiki/



                                 Other Goodness
CommandReference




                   • Written in C (C99 standard)
                   • No dependencies
                   • Detailed command reference that lists time
                        complexity in Big-O notation.

                   • Very easy to build / install (it’ll probably “just
                        work”)
Persistence(1/2) -
           Snapshotting
• This is the default behavior
• Periodic, asynchronous dump to disk
• Can be every N changes or every N seconds
• For example, every 15 min if at least 1 change
 and every 5 min if at least 10 changes

• Since data is written asynchronously, data can
 be lost during a crash.
Persistence(2/2) - Append
                              Only File
See more here:
http://code.google.com/p/redis/
wiki/AppendOnlyFileHowto




               • Available as of version 1.1
               • Works like a journal
               • Every write command is logged ASAP
               • Commands replayed when the server is
                    restarted

               • Configurable speed/safety (safest by default)
More here:
http://code.google.com/
p/redis/wiki/
SelectCommand

http://code.google.com/
p/redis/wiki/
MoveCommand
                          Multiple Databases

               • Use the SELECT command; 0-15 are valid by
                    default, but you can add more in redis.conf

               • Useful for segmenting key namespaces,
                    especially when key queries are needed

               • MOVE command can be used as a locking
                    primitive
Command Reference
More:

http://code.google.com/p/
redis/wiki/
IntroductionToRedisDataTypes
                               Data Structures

              • Strings
              • Strings-as-integers (used by INCR/DECR)
              • List of Strings
              • Set of Strings (unique list)
              • Sorted Set of Strings (and weights) (ZSET)
Command overview

• Strings: get, set, increment, decrement
• Lists: push, pop, length, range, trim
• Sets: add, remove, move, length, intersect
 union, diff, random

• Other: save, lastsave can be used to force &
 verify disk persistence
More:

http://code.google.com/
p/redis/wiki/
RpoplpushCommand
                           Atomic Operations
               •    LPUSH / RPUSH: append to head/tail of list

               •    LPOP / RPOP: return & remove first/last element of list

               •    RPOPLPUSH: return & remove the last element of
                    source list and push to the head of the destination list

               •    GETSET: set a key to a new value and return the old
                    value

               •    MGET/MSET: get or set multiple keys to multiple values

               •    SMOVE: move a value from one set to another

               •    No way to group commands (transactions)
More here:
http://code.google.com/
p/redis/wiki/
ReplicationHowto

                                Replication
               • Slaves can be used for scalability or redundancy
               • A master can have multiple slaves.
               • Slaves are able to accept other slave connections
               • Redis replication is non-blocking on the master,
                    but blocking on the slave (can’t respond to
                    queries during initial sync)

               • Slaves are able to automatically reconnect after
                    an outage
Blog post about Redis
VM:

http://antirez.com/
post/redis-virtual-
memory-story.html            Optional VM feature
                •     Doesn’t use OS virtual memory

                •     Still new, may not scale well in certain situations.

                •     Don’t know how this will affect replication (initial
                      sync / re-sync may be slow, but shouldn’t block the
                      master)

                •     All keys stay in memory. Minimum req’s:

                        •   1 million keys ~ 160M

                        •   10 million keys ~ 1.6G
Syntax highlighted
sample here:

https://gist.github.com/
b16679cbb5b9573e078c
                                Setup

             • git clone git://github.com/antirez/redis.git &&
                 cd redis

             • make
             • ./redis-server
             • make test
             • (review sample config file)
Redis in Ruby


• Redis library: “gem install redis”
• All Redis commands can be called like methods
 on the connection object as described in the
 Command Reference
Example Uses


• A Memcached replacement (fast)
• A work queue (sets & lists)
• Fast auto-completion (persistent, queryable)
• ORDER BY RAND() replacement (sets)
Memcache Replacement
•   Why? Same as Memcached, but you get more “for
    free”:

•   Many databases from a single instance of Redis
    (instead of using namespaces)

•   Ability to easily backup/transfer state (dump.rdb)

•   Watch live commands on a running instance with the
    MONITOR command (instead of restarting with -v)

•   Opportunity to use Redis for other things once
    you’ve switched
Memcache Replacement
                      Note: namespace was replaced
                      with a DB number
• It’s pretty easy:
More:
                                                     Redis allows negative
http:/
                         Work Queue
      /oxfordrepo.blogspot.com/
2010/01/usage-stats-and-redis.html
                                                     array indices to count
                                                     backwards from the end.




                     A VERY simple example
              • LPUSH and RPOP were made for this:
1
2
3
4
5
6
“Not a ‘better DelayedJob’”.
More:                                                          Compare them and see what is
                                                               best for your project
http://github.com/
defunkt/resque

http://github.com/
blog/542-introducing-
                        Work Queue - Resque

               •     Created by Chris Wanstrath. Heavily inspired by
                     DelayedJob. Currently used by GitHub. Provides:

               •     A Ruby library for creating, querying, and
                     processing jobs

               •     A Rake task for starting a worker which
                     processes jobs

               •     A Sinatra app for monitoring queues, jobs, and
                     workers.
Fast Auto-Completion
• Lives in memory, doesn’t expire, persists
Replace MySQL ORDER
More:

                              BY RAND()
http://nosql.mypopescu.com/post/295433623/redis-
usecase-replacing-mysql-order-by-rand




                • ORDER BY RAND() is very slow (even with a
                     LIMIT clause)

                • Duplicating the list of IDs as a Redis set is
                     relatively cheap

                • SRANDMEMBER is fast
Other Interesting Uses of
             Redis
•   Nginx HTTP Redis module for caching with Redis
    (ngx_http_redis)

•   LLOOGG.com: Realtime site usage statistics, use
    alongside Google analytics

•   EZMobius’ Fair Work Scheduler example

•   Sikwamic: Simple Redis-backed Comet server

•   RestMQ - A REST/JSON/HTTP based message queue
    built on Redis

•   redis-textsearch - A simple full-text search for multiple
    data stores
Further reading
•   http://code.google.com/p/redis/wiki/CommandReference

•   http://antirez.com/post/redis-virtual-memory-story.html

•   http://www.slideshare.net/ezmobius/redis-remote-dictionary-server

•   http://www.paperplanes.de/2010/2/16/
    a_collection_of_redis_use_cases.html

•   http://www.dorkalev.com/2010/02/sikwamic-simple-key-value-with-
    comet.html

•   http://github.com/blog/542-introducing-resque

•   http://nosql.mypopescu.com/post/295433623/redis-usecase-replacing-
    mysql-order-by-rand

•   http://github.com/nateware/redis-textsearch

Mais conteúdo relacionado

Destaque

Webinar: How MongoDB is Used to Manage Reference Data - May 2014
Webinar: How MongoDB is Used to Manage Reference Data - May 2014Webinar: How MongoDB is Used to Manage Reference Data - May 2014
Webinar: How MongoDB is Used to Manage Reference Data - May 2014MongoDB
 
Securing Cassandra for Compliance
Securing Cassandra for ComplianceSecuring Cassandra for Compliance
Securing Cassandra for ComplianceDataStax
 
Redis And python at pycon_2011
Redis And python at pycon_2011Redis And python at pycon_2011
Redis And python at pycon_2011sunilar0ra
 
Redis/Lessons learned
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learnedTit Petric
 
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...DataStax
 
Distributed Applications with Apache Zookeeper
Distributed Applications with Apache ZookeeperDistributed Applications with Apache Zookeeper
Distributed Applications with Apache ZookeeperAlex Ehrnschwender
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBRavi Teja
 
BigData_Chp4: NOSQL
BigData_Chp4: NOSQLBigData_Chp4: NOSQL
BigData_Chp4: NOSQLLilia Sfaxi
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askCarlos Abalde
 
Introduction to Apache ZooKeeper
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeperSaurav Haloi
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcachedJurriaan Persyn
 
Découverte de Redis
Découverte de RedisDécouverte de Redis
Découverte de RedisJEMLI Fathi
 
Redis - (nosqlfr meetup #2)
Redis - (nosqlfr meetup #2) Redis - (nosqlfr meetup #2)
Redis - (nosqlfr meetup #2) Frank Denis
 

Destaque (17)

Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
Webinar: How MongoDB is Used to Manage Reference Data - May 2014
Webinar: How MongoDB is Used to Manage Reference Data - May 2014Webinar: How MongoDB is Used to Manage Reference Data - May 2014
Webinar: How MongoDB is Used to Manage Reference Data - May 2014
 
Securing Cassandra for Compliance
Securing Cassandra for ComplianceSecuring Cassandra for Compliance
Securing Cassandra for Compliance
 
Redis And python at pycon_2011
Redis And python at pycon_2011Redis And python at pycon_2011
Redis And python at pycon_2011
 
Redis 101
Redis 101Redis 101
Redis 101
 
Redis/Lessons learned
Redis/Lessons learnedRedis/Lessons learned
Redis/Lessons learned
 
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...
The Promise and Perils of Encrypting Cassandra Data (Ameesh Divatia, Baffle, ...
 
Distributed Applications with Apache Zookeeper
Distributed Applications with Apache ZookeeperDistributed Applications with Apache Zookeeper
Distributed Applications with Apache Zookeeper
 
Redis
RedisRedis
Redis
 
Apache ZooKeeper
Apache ZooKeeperApache ZooKeeper
Apache ZooKeeper
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
BigData_Chp4: NOSQL
BigData_Chp4: NOSQLBigData_Chp4: NOSQL
BigData_Chp4: NOSQL
 
Everything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to askEverything you always wanted to know about Redis but were afraid to ask
Everything you always wanted to know about Redis but were afraid to ask
 
Introduction to Apache ZooKeeper
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeper
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
Découverte de Redis
Découverte de RedisDécouverte de Redis
Découverte de Redis
 
Redis - (nosqlfr meetup #2)
Redis - (nosqlfr meetup #2) Redis - (nosqlfr meetup #2)
Redis - (nosqlfr meetup #2)
 

Último

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
[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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
[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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Redis Overview

  • 1. Redis REmote DIctionary Server Ryan Findley, Philly.rb, 2010-02-16
  • 2. Disclaimer • I’m not using this in production (yet).
  • 3. Kind of like Memcache • Key-value store • All data lives in memory • Keys can expire (or not) • Fast, Light-weight
  • 4. And More • Persistence • Multiple databases • Queryable keyspace • Support for integer counters • Higher level data structures • Atomic operations • Ability to paginate lists without mutating them • Master-slave replication • Optional VM feature (new)
  • 5. More: Command Reference: http:// code.google.com/p/redis/wiki/ Other Goodness CommandReference • Written in C (C99 standard) • No dependencies • Detailed command reference that lists time complexity in Big-O notation. • Very easy to build / install (it’ll probably “just work”)
  • 6. Persistence(1/2) - Snapshotting • This is the default behavior • Periodic, asynchronous dump to disk • Can be every N changes or every N seconds • For example, every 15 min if at least 1 change and every 5 min if at least 10 changes • Since data is written asynchronously, data can be lost during a crash.
  • 7. Persistence(2/2) - Append Only File See more here: http://code.google.com/p/redis/ wiki/AppendOnlyFileHowto • Available as of version 1.1 • Works like a journal • Every write command is logged ASAP • Commands replayed when the server is restarted • Configurable speed/safety (safest by default)
  • 8. More here: http://code.google.com/ p/redis/wiki/ SelectCommand http://code.google.com/ p/redis/wiki/ MoveCommand Multiple Databases • Use the SELECT command; 0-15 are valid by default, but you can add more in redis.conf • Useful for segmenting key namespaces, especially when key queries are needed • MOVE command can be used as a locking primitive
  • 10. More: http://code.google.com/p/ redis/wiki/ IntroductionToRedisDataTypes Data Structures • Strings • Strings-as-integers (used by INCR/DECR) • List of Strings • Set of Strings (unique list) • Sorted Set of Strings (and weights) (ZSET)
  • 11. Command overview • Strings: get, set, increment, decrement • Lists: push, pop, length, range, trim • Sets: add, remove, move, length, intersect union, diff, random • Other: save, lastsave can be used to force & verify disk persistence
  • 12. More: http://code.google.com/ p/redis/wiki/ RpoplpushCommand Atomic Operations • LPUSH / RPUSH: append to head/tail of list • LPOP / RPOP: return & remove first/last element of list • RPOPLPUSH: return & remove the last element of source list and push to the head of the destination list • GETSET: set a key to a new value and return the old value • MGET/MSET: get or set multiple keys to multiple values • SMOVE: move a value from one set to another • No way to group commands (transactions)
  • 13. More here: http://code.google.com/ p/redis/wiki/ ReplicationHowto Replication • Slaves can be used for scalability or redundancy • A master can have multiple slaves. • Slaves are able to accept other slave connections • Redis replication is non-blocking on the master, but blocking on the slave (can’t respond to queries during initial sync) • Slaves are able to automatically reconnect after an outage
  • 14. Blog post about Redis VM: http://antirez.com/ post/redis-virtual- memory-story.html Optional VM feature • Doesn’t use OS virtual memory • Still new, may not scale well in certain situations. • Don’t know how this will affect replication (initial sync / re-sync may be slow, but shouldn’t block the master) • All keys stay in memory. Minimum req’s: • 1 million keys ~ 160M • 10 million keys ~ 1.6G
  • 15. Syntax highlighted sample here: https://gist.github.com/ b16679cbb5b9573e078c Setup • git clone git://github.com/antirez/redis.git && cd redis • make • ./redis-server • make test • (review sample config file)
  • 16. Redis in Ruby • Redis library: “gem install redis” • All Redis commands can be called like methods on the connection object as described in the Command Reference
  • 17. Example Uses • A Memcached replacement (fast) • A work queue (sets & lists) • Fast auto-completion (persistent, queryable) • ORDER BY RAND() replacement (sets)
  • 18. Memcache Replacement • Why? Same as Memcached, but you get more “for free”: • Many databases from a single instance of Redis (instead of using namespaces) • Ability to easily backup/transfer state (dump.rdb) • Watch live commands on a running instance with the MONITOR command (instead of restarting with -v) • Opportunity to use Redis for other things once you’ve switched
  • 19. Memcache Replacement Note: namespace was replaced with a DB number • It’s pretty easy:
  • 20. More: Redis allows negative http:/ Work Queue /oxfordrepo.blogspot.com/ 2010/01/usage-stats-and-redis.html array indices to count backwards from the end. A VERY simple example • LPUSH and RPOP were made for this: 1 2 3 4 5 6
  • 21. “Not a ‘better DelayedJob’”. More: Compare them and see what is best for your project http://github.com/ defunkt/resque http://github.com/ blog/542-introducing- Work Queue - Resque • Created by Chris Wanstrath. Heavily inspired by DelayedJob. Currently used by GitHub. Provides: • A Ruby library for creating, querying, and processing jobs • A Rake task for starting a worker which processes jobs • A Sinatra app for monitoring queues, jobs, and workers.
  • 22. Fast Auto-Completion • Lives in memory, doesn’t expire, persists
  • 23. Replace MySQL ORDER More: BY RAND() http://nosql.mypopescu.com/post/295433623/redis- usecase-replacing-mysql-order-by-rand • ORDER BY RAND() is very slow (even with a LIMIT clause) • Duplicating the list of IDs as a Redis set is relatively cheap • SRANDMEMBER is fast
  • 24. Other Interesting Uses of Redis • Nginx HTTP Redis module for caching with Redis (ngx_http_redis) • LLOOGG.com: Realtime site usage statistics, use alongside Google analytics • EZMobius’ Fair Work Scheduler example • Sikwamic: Simple Redis-backed Comet server • RestMQ - A REST/JSON/HTTP based message queue built on Redis • redis-textsearch - A simple full-text search for multiple data stores
  • 25. Further reading • http://code.google.com/p/redis/wiki/CommandReference • http://antirez.com/post/redis-virtual-memory-story.html • http://www.slideshare.net/ezmobius/redis-remote-dictionary-server • http://www.paperplanes.de/2010/2/16/ a_collection_of_redis_use_cases.html • http://www.dorkalev.com/2010/02/sikwamic-simple-key-value-with- comet.html • http://github.com/blog/542-introducing-resque • http://nosql.mypopescu.com/post/295433623/redis-usecase-replacing- mysql-order-by-rand • http://github.com/nateware/redis-textsearch