Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does master-slave replication ensure that writes show up in later reads?

Here's a very basic question about replication.

If you set up your database with master-slave replication where writes go to the master and reads go to the slaves, doesn't that mean that newly-written data won't necessarily show up in a read until some time later?

For example, suppose a user posts a comment on your website (a write: INSERT INTO comment ...) and then refreshes the page (a read: SELECT * FROM comment ...). The read goes to a slave where the INSERT statement hasn't been replicated yet, so the list of comments comes back without the new one in it. That would effectively violate the "Durability" part of ACID, at least temporarily.

Is that how it actually works? Or is there some way to ensure that users will always be able to read what they just wrote?

like image 536
gesgsklw Avatar asked Oct 30 '22 05:10

gesgsklw


1 Answers

Master->Slave replication databases work where the master database gets written to and the Slave is read. When an INSERT is made into the master database, the slave will contain that information given that the master->slave are configured and in synch. So for example, if I wrote:

INSERT into TEST_TABLE (col1, col2) values ("test", 123);

If I went to the slave database and queried:

SELECT col1, col2 FROM TEST_TABLE;

I will see the values I inserted into the master database. I would recommend looking at the documentation for the flavor of SQL you are using for more details on master->slave replication. As mentioned in the comments, it does matter what flavor of SQL you are using as well as the configuration you have to enable to frequency of replication to occur.

like image 183
ryekayo Avatar answered Nov 15 '22 06:11

ryekayo