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

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 IntegrationJulio Javier Cicchelli
 
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 AngularJSPablo Godel
 
*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...Alessio Piccioli
 
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 AnglePablo Godel
 
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 CoreHiroshi SHIBATA
 
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 platformAlexandru Badiu
 
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 AppJoel Byler
 
Rubymotion trip to inspect 2013
Rubymotion trip to inspect 2013Rubymotion trip to inspect 2013
Rubymotion trip to inspect 2013Hussein Morsy
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLBarry Jones
 
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 RubyAstrails
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
Microservices and functional programming
Microservices and functional programmingMicroservices and functional programming
Microservices and functional programmingMichael Neale
 
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 2016gregorbyte
 
Evolutionary Architecture at Work
Evolutionary  Architecture at WorkEvolutionary  Architecture at Work
Evolutionary Architecture at WorkPhil 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 RubyJeff Cohen
 
Drupal 8 for site builders
Drupal 8 for site buildersDrupal 8 for site builders
Drupal 8 for site buildersswentel
 
Red Dirt Ruby Conference
Red Dirt Ruby ConferenceRed Dirt Ruby Conference
Red Dirt Ruby ConferenceJohn Woodell
 
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 APIC4Media
 
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 2009Frank Karlitschek
 

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

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
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
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 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
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
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
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

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