Dimitri Kravtchuk (aka DimK) has just published a very thorough 3 part series comparing MySQL 8.4 LTS and MariaDB 12.3 LTS.
While the performance results are of course the thing of interest, these articles also provide a very useful insight into the experience of configuring and running MariaDB Server.
One thing that struck me was that Dimitri enabled automatic binary log expiration during his benchmarks, yet the binary logs kept piling up until his storage ran out of free space.
Why did it happen? And more importantly, how do you prevent it?
When five minutes are not five minutes
During his benchmarks, Dimitri configured the following variable:
binlog_expire_logs_seconds=300
The intent was very simple really: just auto-remove the binary logs older than 5 minutes.
However, the binary logs continued accumulating. Even an attempt to explicitly purge them resulted in the following error:
ERROR 1378 (HY000): A purgeable log is in use, will not purge
The explanation was another configuration variable, introduced in MariaDB 11.4:
slave_connections_needed_for_purge
Understanding slave_connections_needed_for_purge
This variable defines the minimum number of connected replicas required for automatic binary log purging.
It was introduced as a safeguard for replication environments to ensure binary logs aren’t removed before replicas have a chance to read them.
MariaDB can keep binary logs around even after their configured expire time has passed (i.e. they have a value of 1) if no replica is connected.
This setting affects the automatic purging mechanisms controlled by:
- binlog_expire_logs_seconds
- expire_logs_days
- max_binlog_total_size
In other words, setting an expiry period or a binary log storage limit doesn’t necessarily mean that the binary logs will be removed.
What if I’ve got none at all?
Binary logging is useful even without replication.
For example, you might turn on binary logging on a standalone MariaDB Server to support point-in-time recovery (PITR).
In that environment, it makes absolutely no sense to require a replica connection before allowing automatic binary log purging.
You can check your current configuration with:
SELECT @@global.log_bin, @@global.binlog_expire_logs_seconds,
@@global.slave_connections_needed_for_purge,
@@global.max_binlog_total_size;
If your server has no replicas at all and doesn’t depend on any external binary log consumers, then you can simply remove the minimum replica connection requirement dynamically:
SET GLOBAL slave_connections_needed_for_purge = 0;
To make the configuration persistent across server restarts, add the following to your MariaDB configuration file:
[mariadb]
slave_connections_needed_for_purge=0
For example, a standalone MariaDB Server retaining binary logs for seven days could use the following configuration:
[mariadb]
log_binbinlog_expire_logs_seconds=604800
slave_connections_needed_for_purge=0
Of course, your retention period should be driven by your backup and recovery strategy.
What if I just use replication?
If you’re running a replication topology, don’t just go and set this variable to zero.
The safeguard is there for a reason. If you remove the binary logs that a disconnected replica is required to have, you can effectively prevent that replica from ever getting back into replication.
The same applies to external binary log consumers (e.g. a Change Data Capture solution).
Make sure your binary log retention policy takes into account:
- Potential replica downtime.
- Replication lag.
- External binary log consumers.
- Backup frequency.
- Point-in-time recovery requirements.
- Available storage capacity.
Binary log expiration isn’t necessarily an immediate thing either. With traditional binary logging, expiration checks happen at events like server startup and binary log rotation.
You can inspect the currently available binary logs using:
SHOW BINARY LOGS;
For traditional binary logging, you can also trigger a log rotation using:
FLUSH BINARY LOGS;
This gives MariaDB the chance to assess the configured expiry policy.
By default, many FLUSH statements are
written to the binary log and replicated to downstream
replicas.
This can cause replication issues when replicating to a Database-as-a-Service (DBaaS) instance, where certain administrative operations may be restricted.
Although, it seems that FLUSH BINARY LOGS is an
exception and is not itself replicated by MariaDB like its in MySQL. This is not (yet) documented but seems obvious in the code.
So explicitly using the LOCAL keyword
makes your intention clear:
FLUSH LOCAL BINARY LOGS;
LOCAL is an alias for
NO_WRITE_TO_BINLOG.
As a general best practice, consider using
FLUSH LOCAL whenever an administrative
operation is intended to affect only the local server.
What about the new InnoDB based binary log?
MariaDB 12.3 introduces an InnoDB based binary log implementation (one of the improvements Dimitri points to as being a positive in his benchmarking series).
Whilst this new architecture does improve performance in terms of binary logging and transaction commit, binary log retention is still an important operational issue.
Regardless of which binary log implementation you choose, you need to understand the retention settings and how they interrelate.
Beyond performance benchmarks
Performance benchmarks are not just about queries per second, transactions per second or which database engine is the fastest.
They also provide useful feedback on configuration, usability, documentation and the general user experience.
Dimitri’s observation is the textbook example. A config dependency that would be perfectly obvious to someone who’d actually spent any time with MariaDB replication internals is going to be a complete surprise to someone who’s just configuring a standalone database server.
We are collecting the actionable findings from his benchmark series in an umbrella task: MDEV-42435
Improving visibility into binary log expiration dependencies is one of those opportunities.
Conclusion
If your MariaDB binary logs aren’t being auto purged even though you’ve set an expiry period then check slave_connections_needed_for_purge. It might be the missing bit of your config.
And thanks very much Dimitri for taking the time to benchmark MariaDB Server and, just as importantly, for sharing your experience. This is how open source works at its best: testing, sharing, learning and improving together.
Happy purging!
Further reading
- Dimitri’s benchmark series — Part 1
- Dimitri’s benchmark series — Part 2
- Dimitri’s benchmark series — Part 3
- MariaDB Replication and Binary Log System Variables
- Using and Maintaining the Binary Log
- InnoDB-based Binary Log