Innodb Transaction History Often Hides Dangerous ‘Debt’
Join the DZone community and get the full member experience.Join For Free
in many write-intensive workloads innodb/xtradb storage engines you may see hidden and dangerous “debt” being accumulated – unpurged transaction “history” which if not kept in check over time will cause serve performance regression or will take all free space and cause an outage. let’s talk about where it comes from and what can you do to avoid running into the trouble.
technical background: innodb is an mvcc engine which means it keeps multiple versions of the rows in the database, and when rows are deleted or updated they are not immediately removed from the database but kept for some time – until they can be removed. for a majority of oltp workloads they can be removed seconds after the change actually took place. in some cases though they might need to be kept for a long period of time – if there are some old transactions running in the system that might still need to look at an old database state. as of mysql 5.6 innodb has one or several “purge threads” which remove the old data that can be removed, though they might not be doing it fast enough for workloads with very intensive writes.
does it really happen? i started looking into this problem based on some customer concerns and to my surprise i could very easily get the history to grow rapidly using basic sysbench “update” workload. it is especially easy with default innodb_purge_threads=1 setting but even with innodb_purge_threads=8 it grows rather rapidly.
if we take a look at the purging speed (which comes from innodb-metrics table) we can see what purge is being very much starved by the active concurrent sysbench process and it speeds up greatly when it is finished:
now to be frank this is not an easy situation to get in the majority of workloads with short transactions when the undo space is kept in memory purge and is able to keep up. if undo space however happens to be gone from buffer pool the purge speed can slow down drastically and the system might not be able to keep up anymore. how it could happen? there are 2 common variants….
long running transaction: if you’re having some long running transaction, for example mysqldump, on the larger table the purging has to pause while that transaction is running and a lot of history will be accumulated. if there is enough io pressure a portion of undo space will be removed from the buffer pool.
mysql restart: even with modest history length restarting mysql will wash away from memory and will cause purge to be io bound. this is of course if you’re not using innodb buffer pool save and reload.
how do you check if your undo space is well cached? in percona server i can use those commands:
mysql>select sum(curr_size)*16/1024undo_space_mb from xtradb_rseg; +---------------+ |undo_space_mb| +---------------+ | 1688.4531| +---------------+ 1row inset(0.00sec) mysql>select count(*)cnt,count(*)*16/1024size_mb,page_type from innodb_buffer_page group by page_type; +--------+-----------+-------------------+ |cnt|size_mb |page_type | +--------+-----------+-------------------+ | 55|0.8594|extent_descriptor| |2|0.0313|file_space_header| |108|1.6875|ibuf_bitmap | |17186|268.5313|ibuf_index| |352671|5510.4844|index | | 69|1.0781|inode | |128|2.0000|system| |1|0.0156|trx_system| | 6029| 94.2031|undo_log| |16959|264.9844|unknown | +--------+-----------+-------------------+ 10rows inset(1.65sec)
this shows what the total undo space size is now, 1.7gb, with less than 100mb cached in the buffer pool size….
are a few graphs from running heavy concurrent query during lighter
workload where purging could keep up. in this case i used the
“injection” benchmark in sysbench setting
to 50% of what the system shown as peak.
mysql>select count(distinctk+length(pad))from sbtest1; +--------------------------------+ |count(distinctk+length(pad))| +--------------------------------+ | 30916851| +--------------------------------+ 1row inset(28min32.38sec)
we can see from those graphs is that innodb purging initially is
progressing at a speed fast enough to keep up with inflow of
however as we kick up the complicated query, purging is stopped and when the query is done the purge speed settles on the new much lower level where it is not able to keep up with the workload anymore.
now, there is recognition of this problem and there are options with innodb_max_purge_lag and innodb_max_purge_lag_delay to set the maximum length of the history after reaching which delay will be injected for dml statements up to a specified amount of microseconds.
unfortunately it is not designed very well to use with real applications. the problems i see with its design are two fold….
looking at total history: if you think about it there are 2 kinds of records within the history – there are records that can be purged and there are ones which can’t be purged because they are needed by some active transaction. it is perfectly fine to have a lot of records in history if some long transaction is running – it is not the cause of the problem or overload, while we expect what “purgable history” should be low most of the time.
looking at the size rather than rate of change: even worse, the history blowout prevention is looking at the current value to inject a delay and not at whenever it is that’s growing or already shrinking.
these together means that cases of long running transactions concurrently with oltp workloads is handled very poorly – as long as history reaches the specified maximum amount the system will kick into overdrive, delaying all statements to the maximum extent possible, until the history falls back below the threshold. here is how it looks on graphs:
as you see on the last graph, we got the purge_dml_delay_usec spiking to 10000us (the max i set) even as no purging can be done (see the blue line is at zero). it only actually starts to work on the history when the heavy query completes and really releases the breaks when the purge is complete. in this case the throughput of the system reduced more than 5 times when the delay was active – which would not work for most real-world systems.
design thoughts: so what would i change in the purging design of the configuration? i would like to see a better default configuration that should include multiple purge threads and purge delay (improved). i would find some way to measure not only history size but purgable history size and base purge delay on it. also make it based on the change rather than threshold – do just enough delay so the history is gradually shrinking. also basing it on the undo space size instead of the number of transactions (which can vary in size) might be more practical and easier to auto-tune. we also can probably do better in terms of undo space caching – similar to insert buffer, i’d like to keep it in memory say until 10% of the buffer pool size as removing from the cache something you know you will need very soon is bad business, as well as consider whether there is some form of read-ahead which can work to pre-read undo space which is needed. right now i’ve tested and neither linear nor random read-ahead seems to help picking it up from disk with less random io.
practical thoughts: whatever improvements we’ll get from purging we have mysql and percona server 5.6 systems to run for some years to come. so what are the practical steps we can do to manage purge history better?
monitor: make sure you are monitoring and graphing innodb_history_list_length. if you use large transactions, set alerts pretty high but do not leave it unchecked.
configure set innodb_purge_threads=8 or some other value if you have write intensive workload. consider playing with innodb_max_purge_lag and innodb_max_purge_lag_delay but be careful – as currently designed it can really bring the server to its knees. you may consider using it interactively instead, changing them as run-time options if you spot history list growths unchecked, balancing current workload demands with resources allocated to purging.
let it purge before shutdown: in many cases i find purge performance much worse after i restart mysql server because of caching. so the good approach might be just to remove the workload from mysql server before shutting it down to let the purge of outstanding history complete – and only after that shut it down. if the server has crashed you might consider letting it complete purging before getting traffic routed back to it.
use innodb buffer pool preload use innodb_buffer_pool_dump_at_shutdown=on and innodb_buffer_pool_load_at_startup=on to ensure undo space is preloaded back to the buffer pool on startup.
p.s if you wonder where the graphs i have used came from – it is our percona cloud tools – a very convenient way for analyses like these allowing access to all mysql status variables, innodb metrics, tons of os metrics and more.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Top 10 Engineering KPIs Technical Leaders Should Know
SRE vs. DevOps
Database Integration Tests With Spring Boot and Testcontainers