Intro to MySQL Master
Slave Replication
Satej Kumar Sahu
Mindfire Solutions
Contents
• What is Replication?
• Concept of Master and Slave
• Advantages of Replication
• Methods of Replication
• How event based replication works?
• Demo setup of master slave replication in MySQL
• Code/config setup from the start for allowing app level support for master-slave setups later
• Scenarios best for this setup/strategy
• Conclusion
• References
What is Replication?
• Replication enables data from one MySQL
database server (the master) to be copied to one or
more MySQL database servers (the slaves).
• By default asynchronous.
Concept of Master and Slave
Advantages of Replication
• Scale-out solutions
• Data security
• Analytics
• Long-distance data distribution
Methods of Replication
• Traditional method based on replicating events
from the master's binary log, and requires the log
files and positions in them to be synchronized
between master and slave.
- Types of events: Row based (default) or statement
based or mixed type.
• Newer method based on global transaction
identifiers (GTIDs) which is transactional.
How event based
Replication works?
Demo setup of master slave
replication in MySQL
• Two AWS instances with port 3306 open for mysql
on master instance.
Code/config setup from the start for
allowing app level support for master-slave
setups later
• Database access layer code should have been properly abstracted/modularized
• safe_writer_connect()
safe_reader_connect()
safe_reader_statement()
safe_writer_statement()
• Important thing is to have a unified interface for connecting for reads, connecting for
writes, doing a read, and doing a write.
• May be a painful and scary process at first. The code is much easier to maintain, and
adding troubleshooting options is trivial. You need modify only one or two functions;
for example, to log how long each statement took, or which statement among those
issued gave you an error.
• Also, you can create a user for write only and read only privileges to connect to master
and slave for added separation of concern layer.
Scenarios best for this
setup/strategy
• Most of the cases you would not need it. A good designed, optimised, normalized
database would not require it normally.
• MySQL replication is most beneficial for a system that processes frequent reads and
infrequent or low number of writes/updates.
• Answering the following questions should help you decide whether and by how much
replication will improve the performance of your system
- read/write ratio on your system?
- How much more write load can one server handle if you reduce the reads?
- For how many slaves do you have bandwidth available on your network?
• Examples:
- Ecommerce sites
- Newspaper sites
- Online library sites
- Blogging sites
• Browsing happens much in comparison to writes.
Conclusion
• In most of the case, given good database design
and optimisations and use of indexes, you would
not need replication in most of the cases.
• But if you need to scale out due to large number of
reads, you should go for it :)
References
• http://dev.mysql.com/doc/refman/5.7/en/replication.html
• https://www.percona.com/blog/2013/01/09/how-does-
mysql-replication-really-work/
• https://dev.mysql.com/doc/refman/5.7/en/faqs-
replication.html
• https://dev.mysql.com/doc/refman/5.7/en/faqs-
replication.html#faq-replication-replication-how-benefits-
me
any?
Thank You :)

Intro to MySQL Master Slave Replication

  • 1.
    Intro to MySQLMaster Slave Replication Satej Kumar Sahu Mindfire Solutions
  • 2.
    Contents • What isReplication? • Concept of Master and Slave • Advantages of Replication • Methods of Replication • How event based replication works? • Demo setup of master slave replication in MySQL • Code/config setup from the start for allowing app level support for master-slave setups later • Scenarios best for this setup/strategy • Conclusion • References
  • 3.
    What is Replication? •Replication enables data from one MySQL database server (the master) to be copied to one or more MySQL database servers (the slaves). • By default asynchronous.
  • 4.
  • 5.
    Advantages of Replication •Scale-out solutions • Data security • Analytics • Long-distance data distribution
  • 6.
    Methods of Replication •Traditional method based on replicating events from the master's binary log, and requires the log files and positions in them to be synchronized between master and slave. - Types of events: Row based (default) or statement based or mixed type. • Newer method based on global transaction identifiers (GTIDs) which is transactional.
  • 7.
  • 8.
    Demo setup ofmaster slave replication in MySQL • Two AWS instances with port 3306 open for mysql on master instance.
  • 9.
    Code/config setup fromthe start for allowing app level support for master-slave setups later • Database access layer code should have been properly abstracted/modularized • safe_writer_connect() safe_reader_connect() safe_reader_statement() safe_writer_statement() • Important thing is to have a unified interface for connecting for reads, connecting for writes, doing a read, and doing a write. • May be a painful and scary process at first. The code is much easier to maintain, and adding troubleshooting options is trivial. You need modify only one or two functions; for example, to log how long each statement took, or which statement among those issued gave you an error. • Also, you can create a user for write only and read only privileges to connect to master and slave for added separation of concern layer.
  • 10.
    Scenarios best forthis setup/strategy • Most of the cases you would not need it. A good designed, optimised, normalized database would not require it normally. • MySQL replication is most beneficial for a system that processes frequent reads and infrequent or low number of writes/updates. • Answering the following questions should help you decide whether and by how much replication will improve the performance of your system - read/write ratio on your system? - How much more write load can one server handle if you reduce the reads? - For how many slaves do you have bandwidth available on your network? • Examples: - Ecommerce sites - Newspaper sites - Online library sites - Blogging sites • Browsing happens much in comparison to writes.
  • 11.
    Conclusion • In mostof the case, given good database design and optimisations and use of indexes, you would not need replication in most of the cases. • But if you need to scale out due to large number of reads, you should go for it :)
  • 12.
    References • http://dev.mysql.com/doc/refman/5.7/en/replication.html • https://www.percona.com/blog/2013/01/09/how-does- mysql-replication-really-work/ •https://dev.mysql.com/doc/refman/5.7/en/faqs- replication.html • https://dev.mysql.com/doc/refman/5.7/en/faqs- replication.html#faq-replication-replication-how-benefits- me
  • 13.
  • 14.