SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Redis on Rails
   Obie Fernandez
Salvatore Sanfilippo
aka “antirez”


Pieter Noordhuis
It’s a sidekick to your regular database!
clues that you
should consider
using Redis
•SQL database seems overkill
•update-only data (only inserted)
•non-transactional needs
•no relations with other tables
Why Redis
 and not
something
  else?
Redis
   is
blazing
 fast!
Source: http://antirez.com/post/redis-memcached-benchmark.html
Redis
Durability
:REDIS0001<FE>^@^C^RAttendee:27:events^B<C1><A8>^@
       1335369284<C1><A6>^@
       1335369244^C^TAttendee:1:events:11^A<C0>J
       1335364980^@^PAttendee:10:name^SJorge H. Cuadrado
       C^@ESCConference:8:breakevenpoint<C1><A0>^O^C^TAttendee:1:events:
       13^B<C0>a
       1335365085<C0>g
       1335365112^@^PAttendee:22:name
       Mark Bates^D^RConference:1:notes^A<C0>^A<C3>=@C^Xthese are my
       notes, lala ^C@^A^E

       mo ^^<A0>ESC^LI'll addd som 4^Bore^C^QAttendee:4:events^F<C0>^]
       1335314963<C0>^_
       1335314968<C0>D
       1335335712<C0>E



RDB
       1335335715<C0>^S
       1335314271<C0>^T
       1335314300^C^RAttendee:16:events^C<C0>k
       1335365137<C0>^
       1335365082<C0>V



files
       1335365068^B^UAttendee:
       11:followers^B<C0>^A<C0>^O^D^ZAttendeeRegisteredEvent:
       65^B^Kattendee_id^A1^Mconference_id^A8^D^
       AttendeeUnregisteredEvent:64^B^Kattendee_id^A1
       ^Mconference_id^A8^C^TAttendee:13:events:1^A<C0>a
       1335365085^D^AttendeeUnregisteredEvent:
       66^B^Kattendee_id^A1^Mconference_id^A8^D^]AttendeeUnregisteredEven
       t:170^B^Kattendee_id^B25^Mconference_id^A8^C^UAttendee:21:events:
       21^M<C1><97>^@
       1335365767<C1><8F>^@
       1335365733<C1><92>^@
       1335365741<C1><9B>^@
       1335365879<C1><93>^@
       1335365742<C1><94>^@
       1335365743<C1><95>^@
       1335365744<C1><96>^@
       1335365746<C1><98>^@
       1335365783<C1><99>^@
################################ SNAPSHOTTING
#################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and
#   the given number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving at all commenting all the
#   "save" lines.

save 900 1
save 300 10
save 60 10000
Redis AOF
(Append Only File)
AOF options
Redis
has lots
   of
features
Photo Credit: http://www.flickr.com/photos/blingrocks
Nest
Object-oriented Keys for Redis
github.com/soveran/nest



 • Creates a Redis connection by default
 • Calls to_s for key representation
 • Really simple code / hack it for your needs
Example App
http://github.com/obie/redis_on_rails
Adding properties
 to ActiveRecord
     Example
RedisProps
Easy annotation of ActiveRecord objects
http://github.com/obie/redis_props




           example: facebook_authentication.rb
...Redis hash
   Example
Basic Twitter
functionality in about
  60 lines of Ruby
Examples
of intersections
and unions
Events - Sorted sets
     Example
SortedSet
zunionstore and
  zinterstore
   Examples
RedisObjects
Map Redis types directly to Ruby objects
github.com/nateware/redis-objects
RedisObjects
•Values
•Lists
•Sets
•SortedSets
•Atomic Counters and Locks
Atomic Counter
   Example
Testing Tips

•Don’t bother mocking out Redis
•Select a different database number
  so you don’t clobber anything

•Redis.current.flushdb        is your
  friend
Rails
Console
  ♥s
 Redis
Redis
  makes
you more
 creative
Thanks...

Example Code
http://github.com/obie/redis_on_rails

Slides available at my blog
http://blog.obiefernandez.com

Redis
http://redis.io



Check out DueProps
http://dueprops.com

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (9)

региональная образовательная политика
региональная образовательная политикарегиональная образовательная политика
региональная образовательная политика
 
Projekte starten mit Lego - Insidrrr Day 02
Projekte starten mit Lego - Insidrrr Day 02Projekte starten mit Lego - Insidrrr Day 02
Projekte starten mit Lego - Insidrrr Day 02
 
IoTで遊んでみた
IoTで遊んでみたIoTで遊んでみた
IoTで遊んでみた
 
Apoptosis
ApoptosisApoptosis
Apoptosis
 
IBM Watson IoT - New Possibilities in a Connected World
IBM Watson IoT - New Possibilities in a Connected WorldIBM Watson IoT - New Possibilities in a Connected World
IBM Watson IoT - New Possibilities in a Connected World
 
Concepto y definicion de educacion fisica
Concepto y definicion de educacion fisicaConcepto y definicion de educacion fisica
Concepto y definicion de educacion fisica
 
App Sharing - Flipboard by Pan Weifan
App Sharing - Flipboard by Pan WeifanApp Sharing - Flipboard by Pan Weifan
App Sharing - Flipboard by Pan Weifan
 
Standard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch DeckStandard Treasury Series A Pitch Deck
Standard Treasury Series A Pitch Deck
 
Scrum zum Anfassen
Scrum zum AnfassenScrum zum Anfassen
Scrum zum Anfassen
 

Ähnlich wie Redis on Rails at RedDotRubyConference 2012

Redis+Spark Structured Streaming: Roshan Kumar
Redis+Spark Structured Streaming: Roshan KumarRedis+Spark Structured Streaming: Roshan Kumar
Redis+Spark Structured Streaming: Roshan Kumar
Redis Labs
 

Ähnlich wie Redis on Rails at RedDotRubyConference 2012 (20)

Redispresentation apac2012
Redispresentation apac2012Redispresentation apac2012
Redispresentation apac2012
 
AngularJS
AngularJSAngularJS
AngularJS
 
Introduction no sql solutions with couchbase and .net core
Introduction no sql solutions with couchbase and .net coreIntroduction no sql solutions with couchbase and .net core
Introduction no sql solutions with couchbase and .net core
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuPostgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
TechDays 2016 - Developing websites using asp.net core mvc6 and entity framew...
 
10 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 201910 Ways to Scale Your Website Silicon Valley Code Camp 2019
10 Ways to Scale Your Website Silicon Valley Code Camp 2019
 
Redis
RedisRedis
Redis
 
RedisConf18 - Writing modular & encapsulated Redis code
RedisConf18 - Writing modular & encapsulated Redis codeRedisConf18 - Writing modular & encapsulated Redis code
RedisConf18 - Writing modular & encapsulated Redis code
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
 
Best practices tekx
Best practices tekxBest practices tekx
Best practices tekx
 
LambdaFlow: Scala Functional Message Processing
LambdaFlow: Scala Functional Message Processing LambdaFlow: Scala Functional Message Processing
LambdaFlow: Scala Functional Message Processing
 
SQL Server R Services: What Every SQL Professional Should Know
SQL Server R Services: What Every SQL Professional Should KnowSQL Server R Services: What Every SQL Professional Should Know
SQL Server R Services: What Every SQL Professional Should Know
 
Domain Driven Design Tactical Patterns
Domain Driven Design Tactical PatternsDomain Driven Design Tactical Patterns
Domain Driven Design Tactical Patterns
 
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
 
When OLAP Meets Real-Time, What Happens in eBay?
When OLAP Meets Real-Time, What Happens in eBay?When OLAP Meets Real-Time, What Happens in eBay?
When OLAP Meets Real-Time, What Happens in eBay?
 
Redis+Spark Structured Streaming: Roshan Kumar
Redis+Spark Structured Streaming: Roshan KumarRedis+Spark Structured Streaming: Roshan Kumar
Redis+Spark Structured Streaming: Roshan Kumar
 
Developing a Redis Module - Hackathon Kickoff
 Developing a Redis Module - Hackathon Kickoff Developing a Redis Module - Hackathon Kickoff
Developing a Redis Module - Hackathon Kickoff
 
Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)Redis Use Patterns (DevconTLV June 2014)
Redis Use Patterns (DevconTLV June 2014)
 
Exploring Relay land
Exploring Relay landExploring Relay land
Exploring Relay land
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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?
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 

Redis on Rails at RedDotRubyConference 2012

Hinweis der Redaktion

  1. \n
  2. Redis is an extremely fast, atomic key-value store. It allows the storage of strings, sets, sorted sets, lists and hashes. Redis keeps all the data in RAM, much like Memcached but unlike Memcached, Redis writes to disk, giving it persistence.\n
  3. \n
  4. \n
  5. \n
  6. to some degree this is criteria for a no-sql solution in general, so why pick Redis?\n
  7. \n
  8. Redis is written in very CPU-efficient C and does all its operations in memory. (Paging to virtual memory on disk is no longer supported in latest version.)\n\nredis-benchmark -q -n 100000\n\nLow-end boxes routinely get 30k to 40k operations per second. High-end server configurations can routinely achieve over 100k operations per second. It&amp;#x2019;s hard to \n\nRedis has been benchmarked at more than 60000 connections, and was still able to sustain 50000 q/s in these conditions.\n\nIn many real world scenarios, Redis throughput is limited by the network well before being limited by the CPU. But being single-threaded, Redis favors fast CPUs with large caches and not many cores.\n\n\n
  9. Atomic operations can be traded for speed\nTo really understand what is the potential of the two systems as caching systems, there is to take into account another aspect. Redis can run complex atomic operations against hashes, lists, sets and sorted sets at the same speed it can run GET and SET operations (you can check this fact using redis-benchmark). The atomic operations and more advanced data structures allow to do much more in a single operation, avoiding check-and-set operations most of the time. So I think that once we drop the limit of just using GET and SET and uncover the full power of the Redis semantic there is a lot you can do with Redis to run a lot of concurrent users with very little hardware.\n
  10. Durability refers to the quality of databases with regards to not losing your data.\n\nIf you&amp;#x2019;re using Redis as more than just a replacement for memcache, like we&amp;#x2019;re going to discuss later in this talk then it&amp;#x2019;s important to understand its durability parameters.\n\nRedis operates in memory and optionally persists its data to disk. If it wasn&amp;#x2019;t persisted to disk then you&amp;#x2019;d lose all of your data when restarting the server or in case of an outage.\n\nRedis has two different ways of persisting data.\n\n
  11. Redis snapshotting is the simplest Redis persistence mode.\n\nRedis can produce point-in-time snapshots of the dataset when specific conditions are met, for instance if the previous snapshot was created more than 2 minutes ago and there are already at least 100 new writes, a new snapshot is created.\n\nSnapshots are produced as a single binary .rdb file that contains the whole dataset. \n
  12. The durability of snapshotting is limited to what the user specified as save points.\n\nIf the dataset is saved every 15 minutes, than in the event of a Redis instance crash or a more catastrophic event, up to 15 minutes of writes can be lost. From the point of view of Redis transactions snapshotting guarantees that either a MULTI/EXEC transaction is fully written into a snapshot, or it is not present at all (as already said RDB files represent exactly point in time images of the dataset). \n\nThe RDB file can not get corrupted, because it is produced by a child process in an append-only way, starting from the image of data in the Redis memory. A new rdb snapshot is created as a temporary file, and gets renamed into the destination file using the atomic rename(2) system call once it was successfully generated by a child process (and only after it gets synched on disk using the fsync system call). \n\nRedis snapshotting does NOT provide good durability guarantees if up to a few minutes of data loss is not acceptable in case of incidents, so it&apos;s usage is limited to applications and environments where losing recent data is not very important. \n
  13. The Append Only File, usually called simply AOF, is the main Redis persistence option. The way it works is extremely simple: every time a write operation that modifies the dataset in memory is performed, the operation gets logged. The log is produced exactly in the same format used by clients to communicate with Redis, so the AOF can be even piped via netcat to another instance, or easily parsed if needed. At restart Redis re-plays all the operations to reconstruct the dataset. \n\nAOF rewrites are generated only using sequential I/O operations, so the whole dump process is efficient even with rotational disks (no random I/O is performed). This is also true for RDB snapshots generation. The complete lack of Random I/O accesses is a rare feature among databases, and is possible mostly because Redis serves read operations from memory, so data on disk does not need to be organized for a random access pattern, but just for a sequential loading on restart.\n\n
  14. What Redis implements when appendfsync is set to always is usually called group commit. This means that instead of using an fsync call for every write operation performed, Redis is able to group this commits in a single write+fsync operation performed before sending the request to the group of clients that issued a write operation during the latest event loop iteration. \n\nIn practical terms it means that you can have hundreds of clients performing write operations at the same time: the fsync operations will be factorized - so even in this mode Redis should be able to support a thousand of concurrent transactions per second while a rotational device can only sustain 100-200 write op/s. \n
  15. \n\n
  16. Let&amp;#x2019;s see what it looks like to do some basic operations in Redis using the Ruby driver.\n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. conference name\n
  24. \n
  25. Event class and subclasses save arbitrary details in hashes.\n
  26. Graphing relationships between (User) objects with Redis sets\n\nattendee.rb\nfollowings.rb\n
  27. \n
  28. \n
  29. Time-ordered activity feeds with Redis sorted sets\nevent.rb and subclasses\n\n
  30. zunionstore...\nfollowing_events method in followings.rb\n\nFollowingsController\nhttp://localhost:3000/attendees/1/following\n\nzinterstore...\nAttendee#events\n
  31. \n
  32. Change conference name to use value\n
  33. tutorial.rb\n
  34. \n
  35. \n
  36. In closing...\n
  37. In closing...\n
  38. \n