August 1, 2014

Binary log file size matters (sometimes)

I used to think one should never look at max_binlog_size, however last year I had a couple of interesting cases which showed that sometimes it may be very important variable to tune properly. I meant to write about it earlier but never really had a chance to do it. I have it now!

One of our customers was complaining that the database would lock up at random times and then it would go back to normal in just a few seconds. This was MySQL 5.0 running MyISAM/InnoDB mix, not heavily loaded. We used pt-stalk (at that time it was aspersa stalk) trying to figure out what is happening, however all we found was a spike in writes, many queries piled up and looking at the system process list it was quite obvious that page flush daemon was acting out. I/O Pattern was rather awkward – here is an output from pt-diskstats (again, aspersa diskstats at that time):

You can see that (a) reads are affected – response time dropped significantly and (b) the reason appears to be enormous amount of writes. For a while I was really puzzled, looking for answers in MySQL status counters, innodb status, memory info and what not. It seemed that the problem was gone before pt-stalk would kick-off and all I could see was symptoms. Indeed the case it was. I was not allowed to run pmp or oprofile on that system due to potential performance impact. However, somehow I have noticed that pt-stalk kick-off time matched that of binary logs modification time:

What is happening here?

Apparently, every time current binary log gets close to max_binlog_size in size, new binary log is created and, if expire_logs_days is not zeroish, log files older than expire_logs_days are removed. &LOCK_log mutex is held for the duration of the binary log removal and if your file system is slow at deleting files (both customers systems ran on ext3, which can be very slow deleting large files), then write operations in MySQL will suffer since that very same mutex is used inside MYSQL_LOG::write() call. Even though it only lasts 1-2s, it still gives users unstable response time – here’s how that looks on one of the customer system – this one was doing ~10k qps at a time:

A small blip, yet over 100 users are backlogged for a couple of seconds – not great user experience, considering this happens every 10-15 minutes.

Solution?

Here’s what we did: we have reduced the size of binary log file from default 1GB (some systems have it set to 100MB in their default my.cnf) down to 50MB and we never saw this problem ever again. Now the files were removed much more often, they were smaller and didn’t take that long to remove.

Note that on ext4 and xfs you should not have this problem as they would remove such files much faster.

About Aurimas Mikalauskas

Aurimas joined Percona in 2006, a few months after Peter and Vadim founded the company. His primary focus is on high performance, but he also specializes in full text search, high availability, content caching techniques and MySQL data recovery.

Comments

  1. Aurimas,

    Running purge of binary logs under the lock looks like a very bad idea. Unlike creating new log file it is not something what really needs to be serialized. I wonder if it is fixed in later MySQL version or bug report is due ?

  2. martin says:

    but is faster rotating few huge (1G) files OR many small (50M) files?
    for example, when max_binlog_size = 1G and expire_logs_days=14 we have 200 files. With 100M it’s 2000 files and 50M => 4000 files. It could be slow to. it’s question of benchmark.

  3. Peter,

    It seems this was fixed in MySQL 5.1.43 (as well as 5.5.1) as part of bug #49536 fix. I didn’t have a chance to verify, but looking at the source code it should not be holding LOCK_log for purging. So it seems I just have too many customers who are still on 5.0 or early 5.1 versions :)

  4. Martin, if you care more about how long the whole process will take as opposed to what the impact is going to be, then, assuming that you purge many binary log files at once, indeed it’s not clear what is going to take longer and fewer large files could be faster.

    However, in this case I am referring to purging that happens automatically every time a new binary log is created. Smaller files means purging will happen more often. Also, if you do happen to purge many binary log files, small binary logs is also an advantage because the lock is held separately for each file, ergo smaller files means purging has lesser impact on threads that are writing at the same time.

  5. martin says:

    And another Q, have you /var/lib/mysqllogs/ at dedicated disk array / filesystem, independent from disk for mysql data? It can make a difference.

  6. Martin, I _think_ in the first case it was a separate disk, though the other customer had both data and binlogs in the same disk. I think the only difference there is, is the speed at which the underlying disk will complete the task, lock is held regardless.

Speak Your Mind

*