SlideShare a Scribd company logo
1 of 28
Save time, money and be happy!
Ruby & Redis Like a Pro
Stefano Fontanelli <s.fontanelli@gmail.com>
Italian Ruby Day Conference 2013
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
About Me
Keep in touch!
http://about.me/stefanofontanelli
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Table of content
• What is Redis?
• Redis in action
• Advanced use: a real use case
• The Reliable Queue Pattern
• Locking with Redis
• The Publish/Subscribe Messaging Paradigm
• Drawbacks
• Conclusions
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
What is Redis?
More Information: http://redis.io
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis: an advanced key-value store
Credits: http://strata.oreilly.com/2013/03/large-scale-data-collection-and-real-time-analytics-using-redis.html
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Outstanding performance
No. of connections
Source: http://redis.io/topics/benchmarks
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Why I love Redis
• Up & Running in few minutes
• brew install redis
• redis-server
• Impressive performance[1]
• Data types are similar to Ruby’s ones [2]
• Known time complexity
• Atomic operations
• Advanced features are outstanding!
• Keys with a limited time-to-live
• Pub/Sub messaging paradigm
• Easily share your data structures between processes!
[1] http://redis.io/topics/benchmarks [2] http://redis.io/topics/data-types
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis in action: strings
More information: http://redis.io/topics/data-types
> get key => key
> set key, value => key = value
> incr key => key = 0
key += 1
> decr key => key = 0
key += 1
> incrby key, value => key = 0
key += value
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis in action: lists
More information: http://redis.io/topics/data-types
> lpush key, value => key = []
key << value
> lpop key, value => key.pop
> llen key => key.length
LPUSH RPOP
RPUSHLPOP
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis in action: hashes
More information: http://redis.io/topics/data-types
> hget k, f, v => k[f]
> hset k, f, v => k = {}
k[f] = v
> hkeys k => k.keys
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis in action: sets
More information: http://redis.io/topics/data-types
> sadd k, v => k = Set.new
k << v
> srem k, v => k.delete v
> sdiff k1, k2 => k1 - k2
> sunion k1, k2 => k1 + k2
> sismember k, v => key.member? v
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis in action: ordered set
More information: http://redis.io/topics/data-types
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Complex things made easy: a real use case
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Complex things made easy: a real use case (2)
Q1
Q2
Q3
Q4
Q5
Q6
S1
S2
S3
Qi = queue i | Sj = service j
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Exchange data between services: queues
Friday, June 14, 13
LPUSH RPOP
RPUSHLPOP
Milan, 14th June 2013 Italian Ruby Day Conference 2013
The Reliable Queue Pattern (1/2)
Friday, June 14, 13
Messages queueProducer Consumer
Processing list
/dev/null
push
push
pop
pop
Milan, 14th June 2013 Italian Ruby Day Conference 2013
The Reliable Queue Pattern (2/2)
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Reliable Queue in Ruby
Fork & Play:
http://github.com/stefanofontanelli/backend-toolkit
• A fully working implementation in less than 30 lines
• Interface similar to Ruby’s one
• q = Queue.new ‘my_first_reliable_queue’
• q << {‘message’ => ‘I love ruby!’}
• msg = q.pop
• You don’t need polling anymore!
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Don’t mess up your data, use locks!
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Locking with Redis
• Use SETNX as lock primitive
• SETNX mylock <timestamp + timeout + 1>
• 1/true lock acquired,
• 0/false lock not acquired.
• Use GET and GETSET to detect deadlocks
1. GET mylock can be use to identify an expired lock;
2. GETSET mylock <timestamp + timeout + 1>
atomically sets key to value and returns the old value;
3. the lock was acquired
when old value is still an expired timestamp.
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Locking with Redis: a Ruby implementation
Fork & Play:
http://github.com/stefanofontanelli/backend-toolkit
• A fully working implementation in ~50 lines of code
• Easy to use
• Lock.new.acquire(obj) do |lock|
# put your code here
lock.keep_alive obj
# put your code here
end
• No need to explicitly release lock.
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
What about services?
Fork & Play:
http://github.com/stefanofontanelli/backend-toolkit
• You can simply define class MyService < Daemon
• It must implement the process(msg, lock) method
• That’s all. Your service is ready to start!
• MyService.new(‘input_queue’,‘output_queue’).run
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
The Publish/Subscribe Messaging Paradigm[1]
[1] http://redis.io/topics/pubsub
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Pub/Sub pattern using Ruby & Redis
require ‘redis’
redis = Redis.new
redis.subscribe(:mychannel) do |on|
on.message do |channel, message|
# puts your code here
end
end
[1] https://github.com/redis/redis-rb/blob/master/examples/pubsub.rb
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Redis is terrific. No drawbacks?
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
How can we address these issues?
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
Conclusions
Friday, June 14, 13
Milan, 14th June 2013 Italian Ruby Day Conference 2013
The End
Any Questions?
Keep in touch!
http://about.me/stefanofontanelli
Friday, June 14, 13

More Related Content

Similar to Ruby & Redis like a pro

Use Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android AppUse Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android App
Joel Byler
 
Evolutionary Architecture at Work
Evolutionary  Architecture at WorkEvolutionary  Architecture at Work
Evolutionary Architecture at Work
Phil Calçado
 
Top 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About RubyTop 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About Ruby
Jeff Cohen
 

Similar to Ruby & Redis like a pro (20)

Rubyists.EU: Stairway to the European Ruby Community Integration
Rubyists.EU: Stairway to the European Ruby Community IntegrationRubyists.EU: Stairway to the European Ruby Community Integration
Rubyists.EU: Stairway to the European Ruby Community Integration
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
Ruby hellug
Ruby hellugRuby hellug
Ruby hellug
 
*Source - towards the semantic annotation of digital content / Wittgenstein S...
*Source - towards the semantic annotation of digital content / Wittgenstein S...*Source - towards the semantic annotation of digital content / Wittgenstein S...
*Source - towards the semantic annotation of digital content / Wittgenstein S...
 
Lone StarPHP 2013 - Building Web Apps from a New Angle
Lone StarPHP 2013 - Building Web Apps from a New AngleLone StarPHP 2013 - Building Web Apps from a New Angle
Lone StarPHP 2013 - Building Web Apps from a New Angle
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
Drupal as a first class mobile platform
Drupal as a first class mobile platformDrupal as a first class mobile platform
Drupal as a first class mobile platform
 
Use Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android AppUse Ruby to Write (and Test) Your Next Android App
Use Ruby to Write (and Test) Your Next Android App
 
Rubymotion trip to inspect 2013
Rubymotion trip to inspect 2013Rubymotion trip to inspect 2013
Rubymotion trip to inspect 2013
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 
RubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with RubyRubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with Ruby
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Microservices and functional programming
Microservices and functional programmingMicroservices and functional programming
Microservices and functional programming
 
Using source control for domino development - AUSLUG 2016
Using source control for domino development - AUSLUG 2016Using source control for domino development - AUSLUG 2016
Using source control for domino development - AUSLUG 2016
 
Evolutionary Architecture at Work
Evolutionary  Architecture at WorkEvolutionary  Architecture at Work
Evolutionary Architecture at Work
 
Top 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About RubyTop 10+ Things .NET Developers Should Know About Ruby
Top 10+ Things .NET Developers Should Know About Ruby
 
Drupal 8 for site builders
Drupal 8 for site buildersDrupal 8 for site builders
Drupal 8 for site builders
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby Conference
 
Functional Reactive Programming in the Netflix API
Functional Reactive Programming in the Netflix APIFunctional Reactive Programming in the Netflix API
Functional Reactive Programming in the Netflix API
 
The State of the Social Desktop 2009
The State of the Social Desktop 2009The State of the Social Desktop 2009
The State of the Social Desktop 2009
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

Ruby & Redis like a pro

  • 1. Save time, money and be happy! Ruby & Redis Like a Pro Stefano Fontanelli <s.fontanelli@gmail.com> Italian Ruby Day Conference 2013 Friday, June 14, 13
  • 2. Milan, 14th June 2013 Italian Ruby Day Conference 2013 About Me Keep in touch! http://about.me/stefanofontanelli Friday, June 14, 13
  • 3. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Table of content • What is Redis? • Redis in action • Advanced use: a real use case • The Reliable Queue Pattern • Locking with Redis • The Publish/Subscribe Messaging Paradigm • Drawbacks • Conclusions Friday, June 14, 13
  • 4. Milan, 14th June 2013 Italian Ruby Day Conference 2013 What is Redis? More Information: http://redis.io Friday, June 14, 13
  • 5. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis: an advanced key-value store Credits: http://strata.oreilly.com/2013/03/large-scale-data-collection-and-real-time-analytics-using-redis.html Friday, June 14, 13
  • 6. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Outstanding performance No. of connections Source: http://redis.io/topics/benchmarks Friday, June 14, 13
  • 7. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Why I love Redis • Up & Running in few minutes • brew install redis • redis-server • Impressive performance[1] • Data types are similar to Ruby’s ones [2] • Known time complexity • Atomic operations • Advanced features are outstanding! • Keys with a limited time-to-live • Pub/Sub messaging paradigm • Easily share your data structures between processes! [1] http://redis.io/topics/benchmarks [2] http://redis.io/topics/data-types Friday, June 14, 13
  • 8. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis in action: strings More information: http://redis.io/topics/data-types > get key => key > set key, value => key = value > incr key => key = 0 key += 1 > decr key => key = 0 key += 1 > incrby key, value => key = 0 key += value Friday, June 14, 13
  • 9. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis in action: lists More information: http://redis.io/topics/data-types > lpush key, value => key = [] key << value > lpop key, value => key.pop > llen key => key.length LPUSH RPOP RPUSHLPOP Friday, June 14, 13
  • 10. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis in action: hashes More information: http://redis.io/topics/data-types > hget k, f, v => k[f] > hset k, f, v => k = {} k[f] = v > hkeys k => k.keys Friday, June 14, 13
  • 11. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis in action: sets More information: http://redis.io/topics/data-types > sadd k, v => k = Set.new k << v > srem k, v => k.delete v > sdiff k1, k2 => k1 - k2 > sunion k1, k2 => k1 + k2 > sismember k, v => key.member? v Friday, June 14, 13
  • 12. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis in action: ordered set More information: http://redis.io/topics/data-types Friday, June 14, 13
  • 13. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Complex things made easy: a real use case Friday, June 14, 13
  • 14. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Complex things made easy: a real use case (2) Q1 Q2 Q3 Q4 Q5 Q6 S1 S2 S3 Qi = queue i | Sj = service j Friday, June 14, 13
  • 15. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Exchange data between services: queues Friday, June 14, 13
  • 16. LPUSH RPOP RPUSHLPOP Milan, 14th June 2013 Italian Ruby Day Conference 2013 The Reliable Queue Pattern (1/2) Friday, June 14, 13
  • 17. Messages queueProducer Consumer Processing list /dev/null push push pop pop Milan, 14th June 2013 Italian Ruby Day Conference 2013 The Reliable Queue Pattern (2/2) Friday, June 14, 13
  • 18. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Reliable Queue in Ruby Fork & Play: http://github.com/stefanofontanelli/backend-toolkit • A fully working implementation in less than 30 lines • Interface similar to Ruby’s one • q = Queue.new ‘my_first_reliable_queue’ • q << {‘message’ => ‘I love ruby!’} • msg = q.pop • You don’t need polling anymore! Friday, June 14, 13
  • 19. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Don’t mess up your data, use locks! Friday, June 14, 13
  • 20. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Locking with Redis • Use SETNX as lock primitive • SETNX mylock <timestamp + timeout + 1> • 1/true lock acquired, • 0/false lock not acquired. • Use GET and GETSET to detect deadlocks 1. GET mylock can be use to identify an expired lock; 2. GETSET mylock <timestamp + timeout + 1> atomically sets key to value and returns the old value; 3. the lock was acquired when old value is still an expired timestamp. Friday, June 14, 13
  • 21. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Locking with Redis: a Ruby implementation Fork & Play: http://github.com/stefanofontanelli/backend-toolkit • A fully working implementation in ~50 lines of code • Easy to use • Lock.new.acquire(obj) do |lock| # put your code here lock.keep_alive obj # put your code here end • No need to explicitly release lock. Friday, June 14, 13
  • 22. Milan, 14th June 2013 Italian Ruby Day Conference 2013 What about services? Fork & Play: http://github.com/stefanofontanelli/backend-toolkit • You can simply define class MyService < Daemon • It must implement the process(msg, lock) method • That’s all. Your service is ready to start! • MyService.new(‘input_queue’,‘output_queue’).run Friday, June 14, 13
  • 23. Milan, 14th June 2013 Italian Ruby Day Conference 2013 The Publish/Subscribe Messaging Paradigm[1] [1] http://redis.io/topics/pubsub Friday, June 14, 13
  • 24. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Pub/Sub pattern using Ruby & Redis require ‘redis’ redis = Redis.new redis.subscribe(:mychannel) do |on| on.message do |channel, message| # puts your code here end end [1] https://github.com/redis/redis-rb/blob/master/examples/pubsub.rb Friday, June 14, 13
  • 25. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Redis is terrific. No drawbacks? Friday, June 14, 13
  • 26. Milan, 14th June 2013 Italian Ruby Day Conference 2013 How can we address these issues? Friday, June 14, 13
  • 27. Milan, 14th June 2013 Italian Ruby Day Conference 2013 Conclusions Friday, June 14, 13
  • 28. Milan, 14th June 2013 Italian Ruby Day Conference 2013 The End Any Questions? Keep in touch! http://about.me/stefanofontanelli Friday, June 14, 13