Pergunta

Say I have MySQL master DB(used for updates) and corresponding Slave DB(used for reads). Now user inserts the data into MASTER through application(java), just after insertion he tries to retrieve the data which will go slave. I am sure there be some time lag in replication from master to slave(may be of few Millis) and it won't be instantaneous.

My question is how to deal this scenario in master slave model where user will definitely expect the latest data inserted by him down the line.

Does it need to be handled at application level i.e. wherever transaction reads/fetches the data with any CUD, it should be done from Master so that latest data inserted by same transaction is retrieved or are there some other better ways too?

For example:- User first created the account . Insert goes to Master but when he fires the query to retrieve the user details like user_id, name call will go to slave and he may not be getting any data ?

Foi útil?

Solução

This is often handled by the application actively choosing to read from the master based on context.

A common example I've seen mentioned is a database of blog posts. *Viewing, a blog post would obviously be done from a replica, but previewing an edit to a blog post -- which may be the same code, since it's rendering the post -- might be something you do by reading from the master.

Depending on the application, you might also read from the replica and then fall back to reading from the master if you don't find what you expect.

There are also solutions, like Maxscale -- designed by MariaDB but works with MySQL too -- that are essentially a proxy between your app and both MySQL servers. Whenever you are in a transaction, you're talking to the master, whenever you're not in a transaction and issue a SELECT, the queries go to the replica... so you can easily force a read from the master just by doing it in a transaction. There are other rules in play as well that send certain statements to both servers, such as USE and SET statements. If the replica lag goes beyond a certain threshold, Maxscale sends everything to the master.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a dba.stackexchange
scroll top