SlideShare a Scribd company logo
1 of 26
Download to read offline
Using Replicated
Databases with Rails
      Max Schöfmann




                       26. Feb. 2009
This talk is about...

• Why replication? What does it look like?
• Dealing with replicated DBs in a Rails app
• not: Setting up replication (I‘m no DBA)

    (I will focus on MySQL here, but most is applicable to any DB)
So why...?
So why...?
So why...?


• Load balancing
• Redundancy
Load balancing
Master                      Slave
            replicates



   writes                reads



              App
Load balancing
                                         Slave 1
Master            replicates
                               Slave 2
             repl
                  icates




                                           s d
                                         rea
                               reads
    writes


                           App
Redundancy (read)
                                       Slave 1
Master
                             Slave 2
             repl
                  icates




                             reads
    writes


                           App
Redundancy (r + w)
   Master                       Master
                 replicates




reads + writes                reads + writes



                   App
Redundancy (r + w)
   Master              Master




reads + writes



                 App
Problems


• Conflicting primary keys           (only multi-master)


• Replication lag


           (Reason: asynchronous replication)
Conflicting Primary
        Keys

• auto_increment MODULO #Master
• Only read from one master and use HA
  (migrating IP)
Failover using virtual IP
„Shadow“-Master 10.0.0.1                Master 10.0.0.2 +10.0.0.10
                           replicates
                           heartbeat



                                              reads + writes
 reads (optional)                            to/from virtual IP: 10.0.0.10




                             App
Failover using virtual IP
New Master 10.0.0.1+10.0.0.10                                          Master 10.0.0.2

                                    10.0.0.10 migrates from .2 to .1




     reads + writes
    to/from virtual IP: 10.0.0.10




                                                App
Failover using virtual IP
• How?
 • hartbeat & Friends
• Benefits:
 • App doesn‘t need to implement failover
 • no specially crafted primary keys needed
 • secondary master can be used for LB
 • add more slaves for more LB
Want it more
      Enterprisey?

• MySQL Cluster
 • + synchronous replication
 • - RAM RAM RAM RAM RAM RAM RAM
 • - not ideal for every scenario
So we need a
   read/write split...

• MySQL Proxy
• DBSlayer
• Various Rails plugins
MySQL Proxy
     Pro
• Widely used (support!)
• Powerful scripting
• Delay adaptive balancing (helps against lag!)
     Con

• Another moving part in your setup
• Seems to have issues with UTF-8
DBSlayer

• NYTimes
• JSON via HTTP
• http://code.nytimes.com/projects/dbslayer

              (haven‘t played with it yet)
Rails Plugins

• Various plugins after the „Twitter-Incident“
 • Magic Multi-Connections (Dr. Nic)
 • acts_as_readonlyable
 • MySQL replication adapter
• Most need special care in your app
  (e.g. Foo.all :use_slave => true)
     (development seemed to have stalled, but check github!)
Rails Plugins

• Still actively maintained:
 • masochism (Rick Olson)
 • data_fabric (Fiveruns)
 • multi_db (yours truly)
•   (dev of both started in spring ’08, based on the ideas of masochism)


• mostly transparent in your app
How they work
• „AR::B#connection“ returns a proxy
• the proxy sends insert/delete/update... to
  the master and selects to the slave
• and sends everything to the master during
  transactions
• forcing the master is possible using: code }
  AR::B.connection.with_master {
multi_db is (a bit) different
• Supports multiple slaves with blacklisting
  and immediate failover
• Sends only „safe“ things to the slave and
  anything else to the master (safe when APIs change...)
• dynamically creates delegation methods for
  perfomance
• distributed as a GEM (use without Rails)
   http://github.com/schoefmax/multi_db
Fighting replication lag


• Set certain controller actions master only
• Declare certain models master only


         (all three plugins support this in different ways)
Comparison
                         masochism             data_fabric              multi_db
   nesting
with_master

data sharding

load balancing

  threadsafe!

compatible*
database.yml
(* disable plugin without having to switch hosts in your database.yml - easy to forget...)
Thank you.

http://github.com/schoefmax/multi_db
http://github.com/fiveruns/data_fabric
http://github.com/technoweenie/masochism
http://jan.kneschke.de/projects/mysql/mysql-proxy
http://code.nytimes.com/projects/dbslayer
http://www.linux-ha.org/

More Related Content

What's hot

CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...Skills Matter Talks
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentationSuresh Kumar
 
Ruby On Google App Engine 2nd Athens Ruby Me
Ruby On Google App Engine 2nd Athens Ruby MeRuby On Google App Engine 2nd Athens Ruby Me
Ruby On Google App Engine 2nd Athens Ruby MePanagiotis Papadopoulos
 
FITC - Here Be Dragons: Advanced JavaScript Debugging
FITC - Here Be Dragons: Advanced JavaScript DebuggingFITC - Here Be Dragons: Advanced JavaScript Debugging
FITC - Here Be Dragons: Advanced JavaScript DebuggingRami Sayar
 
Performance Pack
Performance PackPerformance Pack
Performance Packday
 
Scripting Yor Java Application with BSF3
Scripting Yor Java Application with BSF3Scripting Yor Java Application with BSF3
Scripting Yor Java Application with BSF3day
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraAdam Wiggins
 
Ansible module development 101
Ansible module development 101Ansible module development 101
Ansible module development 101yfauser
 
Jruby synergy-of-ruby-and-java
Jruby synergy-of-ruby-and-javaJruby synergy-of-ruby-and-java
Jruby synergy-of-ruby-and-javaKeith Bennett
 
PHP MVC Tutorial 2
PHP MVC Tutorial 2PHP MVC Tutorial 2
PHP MVC Tutorial 2Yang Bruce
 
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5David Voyles
 
Ansible automation tool with modules
Ansible automation tool with modulesAnsible automation tool with modules
Ansible automation tool with modulesmohamedmoharam
 
Automated Deployment with Capistrano
Automated Deployment with CapistranoAutomated Deployment with Capistrano
Automated Deployment with CapistranoSumit Chhetri
 
JBoss, Rails and the cloud
JBoss, Rails and the cloudJBoss, Rails and the cloud
JBoss, Rails and the cloudelliando dias
 
DevOps, A brief introduction to Vagrant & Ansible
DevOps, A brief introduction to Vagrant & AnsibleDevOps, A brief introduction to Vagrant & Ansible
DevOps, A brief introduction to Vagrant & AnsibleArnaud LEMAIRE
 
Continuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeContinuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeSascha Möllering
 
Jenkins and ansible reference
Jenkins and ansible referenceJenkins and ansible reference
Jenkins and ansible referencelaonap166
 
Carton
CartonCarton
Cartontaggg
 
Application Deployment Using Ansible
Application Deployment Using AnsibleApplication Deployment Using Ansible
Application Deployment Using AnsibleCliffano Subagio
 

What's hot (20)

CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
CukeUp! 2012: Michael Nacos on Just enough infrastructure for product develop...
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
WASM! WASI! WAGI! WAT?
WASM! WASI! WAGI! WAT?WASM! WASI! WAGI! WAT?
WASM! WASI! WAGI! WAT?
 
Ruby On Google App Engine 2nd Athens Ruby Me
Ruby On Google App Engine 2nd Athens Ruby MeRuby On Google App Engine 2nd Athens Ruby Me
Ruby On Google App Engine 2nd Athens Ruby Me
 
FITC - Here Be Dragons: Advanced JavaScript Debugging
FITC - Here Be Dragons: Advanced JavaScript DebuggingFITC - Here Be Dragons: Advanced JavaScript Debugging
FITC - Here Be Dragons: Advanced JavaScript Debugging
 
Performance Pack
Performance PackPerformance Pack
Performance Pack
 
Scripting Yor Java Application with BSF3
Scripting Yor Java Application with BSF3Scripting Yor Java Application with BSF3
Scripting Yor Java Application with BSF3
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and Sinatra
 
Ansible module development 101
Ansible module development 101Ansible module development 101
Ansible module development 101
 
Jruby synergy-of-ruby-and-java
Jruby synergy-of-ruby-and-javaJruby synergy-of-ruby-and-java
Jruby synergy-of-ruby-and-java
 
PHP MVC Tutorial 2
PHP MVC Tutorial 2PHP MVC Tutorial 2
PHP MVC Tutorial 2
 
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
Getting started with Emscripten – Transpiling C / C++ to JavaScript / HTML5
 
Ansible automation tool with modules
Ansible automation tool with modulesAnsible automation tool with modules
Ansible automation tool with modules
 
Automated Deployment with Capistrano
Automated Deployment with CapistranoAutomated Deployment with Capistrano
Automated Deployment with Capistrano
 
JBoss, Rails and the cloud
JBoss, Rails and the cloudJBoss, Rails and the cloud
JBoss, Rails and the cloud
 
DevOps, A brief introduction to Vagrant & Ansible
DevOps, A brief introduction to Vagrant & AnsibleDevOps, A brief introduction to Vagrant & Ansible
DevOps, A brief introduction to Vagrant & Ansible
 
Continuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as CodeContinuous Delivery and Infrastructure as Code
Continuous Delivery and Infrastructure as Code
 
Jenkins and ansible reference
Jenkins and ansible referenceJenkins and ansible reference
Jenkins and ansible reference
 
Carton
CartonCarton
Carton
 
Application Deployment Using Ansible
Application Deployment Using AnsibleApplication Deployment Using Ansible
Application Deployment Using Ansible
 

Similar to DB Replication With Rails

A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)Flowdock
 
At Scale With Style (Erlang User Conference 2012)
At Scale With Style (Erlang User Conference 2012)At Scale With Style (Erlang User Conference 2012)
At Scale With Style (Erlang User Conference 2012)Wooga
 
J Ruby Power On The Jvm
J Ruby Power On The JvmJ Ruby Power On The Jvm
J Ruby Power On The JvmQConLondon2008
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 NotesRoss Lawley
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Coursepeter_marklund
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The EnterpriseMatt Aimonetti
 
Ola Bini J Ruby Power On The Jvm
Ola Bini J Ruby Power On The JvmOla Bini J Ruby Power On The Jvm
Ola Bini J Ruby Power On The Jvmdeimos
 
Setting Up a Cloud Server - Part 3 - Transcript.pdf
Setting Up a Cloud Server - Part 3 - Transcript.pdfSetting Up a Cloud Server - Part 3 - Transcript.pdf
Setting Up a Cloud Server - Part 3 - Transcript.pdfShaiAlmog1
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011bobmcwhirter
 
Bitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBrian Sam-Bodden
 
Desktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronDesktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronMarc MacLeod
 
JRoR Deploying Rails on JRuby
JRoR Deploying Rails on JRubyJRoR Deploying Rails on JRuby
JRoR Deploying Rails on JRubyelliando dias
 
Node Security: The Good, Bad & Ugly
Node Security: The Good, Bad & UglyNode Security: The Good, Bad & Ugly
Node Security: The Good, Bad & UglyBishan Singh
 
Jit complier
Jit complierJit complier
Jit complierKaya Ota
 
Writing Rust Command Line Applications
Writing Rust Command Line ApplicationsWriting Rust Command Line Applications
Writing Rust Command Line ApplicationsAll Things Open
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Toolsguest05c09d
 

Similar to DB Replication With Rails (20)

A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)A Tale of a Server Architecture (Frozen Rails 2012)
A Tale of a Server Architecture (Frozen Rails 2012)
 
At Scale With Style (Erlang User Conference 2012)
At Scale With Style (Erlang User Conference 2012)At Scale With Style (Erlang User Conference 2012)
At Scale With Style (Erlang User Conference 2012)
 
At Scale With Style
At Scale With StyleAt Scale With Style
At Scale With Style
 
J Ruby Power On The Jvm
J Ruby Power On The JvmJ Ruby Power On The Jvm
J Ruby Power On The Jvm
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 Notes
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Ola Bini J Ruby Power On The Jvm
Ola Bini J Ruby Power On The JvmOla Bini J Ruby Power On The Jvm
Ola Bini J Ruby Power On The Jvm
 
Setting Up a Cloud Server - Part 3 - Transcript.pdf
Setting Up a Cloud Server - Part 3 - Transcript.pdfSetting Up a Cloud Server - Part 3 - Transcript.pdf
Setting Up a Cloud Server - Part 3 - Transcript.pdf
 
Scaling the Rails
Scaling the RailsScaling the Rails
Scaling the Rails
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
 
MySQL Proxy tutorial
MySQL Proxy tutorialMySQL Proxy tutorial
MySQL Proxy tutorial
 
Ruby on the server
Ruby on the serverRuby on the server
Ruby on the server
 
Bitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRubyBitter Java, Sweeten with JRuby
Bitter Java, Sweeten with JRuby
 
Desktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - ElectronDesktop Apps in a Javascript World - Electron
Desktop Apps in a Javascript World - Electron
 
JRoR Deploying Rails on JRuby
JRoR Deploying Rails on JRubyJRoR Deploying Rails on JRuby
JRoR Deploying Rails on JRuby
 
Node Security: The Good, Bad & Ugly
Node Security: The Good, Bad & UglyNode Security: The Good, Bad & Ugly
Node Security: The Good, Bad & Ugly
 
Jit complier
Jit complierJit complier
Jit complier
 
Writing Rust Command Line Applications
Writing Rust Command Line ApplicationsWriting Rust Command Line Applications
Writing Rust Command Line Applications
 
Rails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & ToolsRails Application Optimization Techniques & Tools
Rails Application Optimization Techniques & Tools
 

Recently uploaded

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Recently uploaded (20)

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

DB Replication With Rails

  • 1. Using Replicated Databases with Rails Max Schöfmann 26. Feb. 2009
  • 2. This talk is about... • Why replication? What does it look like? • Dealing with replicated DBs in a Rails app • not: Setting up replication (I‘m no DBA) (I will focus on MySQL here, but most is applicable to any DB)
  • 5. So why...? • Load balancing • Redundancy
  • 6. Load balancing Master Slave replicates writes reads App
  • 7. Load balancing Slave 1 Master replicates Slave 2 repl icates s d rea reads writes App
  • 8. Redundancy (read) Slave 1 Master Slave 2 repl icates reads writes App
  • 9. Redundancy (r + w) Master Master replicates reads + writes reads + writes App
  • 10. Redundancy (r + w) Master Master reads + writes App
  • 11. Problems • Conflicting primary keys (only multi-master) • Replication lag (Reason: asynchronous replication)
  • 12. Conflicting Primary Keys • auto_increment MODULO #Master • Only read from one master and use HA (migrating IP)
  • 13. Failover using virtual IP „Shadow“-Master 10.0.0.1 Master 10.0.0.2 +10.0.0.10 replicates heartbeat reads + writes reads (optional) to/from virtual IP: 10.0.0.10 App
  • 14. Failover using virtual IP New Master 10.0.0.1+10.0.0.10 Master 10.0.0.2 10.0.0.10 migrates from .2 to .1 reads + writes to/from virtual IP: 10.0.0.10 App
  • 15. Failover using virtual IP • How? • hartbeat & Friends • Benefits: • App doesn‘t need to implement failover • no specially crafted primary keys needed • secondary master can be used for LB • add more slaves for more LB
  • 16. Want it more Enterprisey? • MySQL Cluster • + synchronous replication • - RAM RAM RAM RAM RAM RAM RAM • - not ideal for every scenario
  • 17. So we need a read/write split... • MySQL Proxy • DBSlayer • Various Rails plugins
  • 18. MySQL Proxy Pro • Widely used (support!) • Powerful scripting • Delay adaptive balancing (helps against lag!) Con • Another moving part in your setup • Seems to have issues with UTF-8
  • 19. DBSlayer • NYTimes • JSON via HTTP • http://code.nytimes.com/projects/dbslayer (haven‘t played with it yet)
  • 20. Rails Plugins • Various plugins after the „Twitter-Incident“ • Magic Multi-Connections (Dr. Nic) • acts_as_readonlyable • MySQL replication adapter • Most need special care in your app (e.g. Foo.all :use_slave => true) (development seemed to have stalled, but check github!)
  • 21. Rails Plugins • Still actively maintained: • masochism (Rick Olson) • data_fabric (Fiveruns) • multi_db (yours truly) • (dev of both started in spring ’08, based on the ideas of masochism) • mostly transparent in your app
  • 22. How they work • „AR::B#connection“ returns a proxy • the proxy sends insert/delete/update... to the master and selects to the slave • and sends everything to the master during transactions • forcing the master is possible using: code } AR::B.connection.with_master {
  • 23. multi_db is (a bit) different • Supports multiple slaves with blacklisting and immediate failover • Sends only „safe“ things to the slave and anything else to the master (safe when APIs change...) • dynamically creates delegation methods for perfomance • distributed as a GEM (use without Rails) http://github.com/schoefmax/multi_db
  • 24. Fighting replication lag • Set certain controller actions master only • Declare certain models master only (all three plugins support this in different ways)
  • 25. Comparison masochism data_fabric multi_db nesting with_master data sharding load balancing threadsafe! compatible* database.yml (* disable plugin without having to switch hosts in your database.yml - easy to forget...)