14

MySQL Slave Lag (Delay) Explained And 7 Ways To Battle It


Posted by Artem Russakovskii on September 5th, 2008 in Databases, Programming
Share

Updated: May 10th, 2009

14 Responses to “MySQL Slave Lag (Delay) Explained And 7 Ways To Battle It”

    11 Comments:
  1. CaptTofu says:

    One more thing to get rid of slave lag:

    Get rid of FULLTEXT and use Sphinx.

  2. This is assuming that your slave lag occurs in the slave's SQL thread. It's a good idea to check your IO thread and make sure you don't have any lag there. Otherwise you can be misled by the fact that Seconds_Behind_Master will sometimes show as 0 in some instances where the SQL thread has caught up to the IO thread and the IO thread is behind (SQL thread only knows about statements that are in the relay log, so if relay log is fully processed and waiting for I/O, as far as replication knows, it's all caught up).

    On the master issue: SHOW MASTER STATUS
    On the slave issue: SHOW SLAVE STATUS

    Compare "File" on Master Status to "Master_Log_File" on Slave Status; "Position" on Master Status to "Read_Master_Log_Pos" on Slave Status.

  3. @Gregory
    good point, though such cases are mostly rare unless the network is really, really congested.

    @CaptTofu
    FULLTEXT will slow down indexing, excellent point as well if you're using it.

  4. Be cautious using MySQL Proxy. I evaluated the current release as a load balancing solution here at work, and it would randomly lockup after a few days of running. I tried it on multiple machines and experienced the same issues. It's a neat piece of software, but be aware that it's not even beta quality yet. Nice article though.

  5. Sean says:

    Gregory – that is a necessary check to have as I've experienced that problem when network related issues happen (iptables restart, bugs in load balancer failovers, etc). A Nagios script exists to handle what you're talking about:
    http://openquery.com.au/resources/tools/nagios_scripts

  6. These are all very good points….to make it simpler, there are only a few things you can do to write scale your system:

    1) beef up the system(s) so it can accept more writes — your #3.

    2) accept the limits of your systems, and don't try to overload them with more writes than they can handle — your #2, 4, 5 and 7

    3) make reads and writes faster, so your system has better throughput — your #1 and 6.

    Your "bonus" case is sadly an example of people not thinking, and being lazy in a bad way. How many times does an application do bulk data inserts with a large percentage of duplicate data? Those queries should be written smarter to begin with…

  7. @Sheeri
    I've seen a lot of cases where people preferred to have the database handle insert duplication (due to unique key or other constraints), rather than checking for existing data and then inserting. It is simpler (for them) to do 1 query rather than 2.

  8. @Travis, @Sean
    thanks, I just dug your comments out of Spam Karma spam harvest. I'm going to look into the settings because it's the 4th recent comment wrongly marked as spam.

  9. nomadhacker says:

    I've been in the process of moving my site over to a vps and administering it myself. In so doing, I've been scouring the internet for info to help improve mysql performance and stumbled on your blog. I've still got to go through all your db posts but I've found another bookmark. Thanks.

  10. @nomadhacker
    you're welcome.

  11. donny says:

    I'm new to PHP and recently setup my local machine with PHP and MySQL for doing development. I was sort of stuck when I needed to post my work for the user to test and review. After looking around a bit I found a site that hosts PHP and MySQL apps. I was surprised that it was free – it seems they're offering the service at no cost until 2012. At that point they'll change over to a fee-based service. However, in the meantime, it's a great place to do anything from demo and sandbox right up to posting sites for real.

    Their pitch is as follows:

    "This is absolutely free, there is no catch. You get 350 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is amazing and easy to use website builder. Moreover, there is no any kind of advertising on your pages."

    Check it out using this link:

    http://www.000webhost.com/83188.html

    Important: There's one catch in that you must make sure you visit the account every 14 days – otherwise the account is marked 'Inactive' and the files are deleted!!!

    Thanks and good luck!

  12. 3 Pings:
  13. [...]  MySQL Slave Delay Explained And 7 Ways To Battle It (0 visite) [...]

  14. [...] MySQL Slave Delay Explained And 7 Ways To Battle It [...]

  15. [...] lock up the tables from writes and severely aggravate slave lag because, as I mentioned in the MySQL Slave Lag (Delay) Explained And 7 Ways To Battle It post, replication is [...]

Leave a Reply

Connect with Facebook