Intro to MySQL Master Slave Replication

14
Intro to MySQL Master Slave Replication Satej Kumar Sahu Mindfire Solutions

Transcript of Intro to MySQL Master Slave Replication

Page 1: Intro to MySQL Master Slave Replication

Intro to MySQL Master Slave Replication

Satej Kumar SahuMindfire Solutions

Page 2: Intro to MySQL Master Slave Replication

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

Page 3: Intro to MySQL Master Slave Replication

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.

Page 4: Intro to MySQL Master Slave Replication

Concept of Master and Slave

Page 5: Intro to MySQL Master Slave Replication

Advantages of Replication

• Scale-out solutions

• Data security

• Analytics

• Long-distance data distribution

Page 6: Intro to MySQL Master Slave Replication

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.

Page 7: Intro to MySQL Master Slave Replication

How event based Replication works?

Page 8: Intro to MySQL Master Slave Replication

Demo setup of master slave replication in

MySQL

• Two AWS instances with port 3306 open for mysql on master instance.

Page 9: Intro to MySQL Master Slave Replication

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.

Page 10: Intro to MySQL Master Slave Replication

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.

Page 11: Intro to MySQL Master Slave Replication

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 :)

Page 12: Intro to MySQL Master Slave Replication

References• http://dev.mysql.com/doc/refman/5.7/en/replicatio

n.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

Page 13: Intro to MySQL Master Slave Replication

any?

Page 14: Intro to MySQL Master Slave Replication

Thank You :)