Write Optimizations in Voron
Join the DZone community and get the full member experience.Join For Free
as i mentioned, there is one scenario that produces worse and worse performance for lmdb: random writes. the longer the data set, the more expensive the cost. this is due to the nature of lmdb as a copy on write database. here is a simple explanation of the cost:
we want to update the value in the red square.
because the tree is copy on write, we need to modify the following pages:
and as the tree goes deeper, it becomes worse. naturally, one consequence of doing random writes is tree fragmentation. in my tests, on a small 1 million records dataset we would get a cost of over a hundred pages (roughly 4 mb) to update a hundred 16 + 100 bytes values. just to clarify, those tests were done on the lmdb code, not on voron. so i am pretty sure that this isn’t my problem. indeed, i confirmed that this is a general problem with cow approach.
i accepted that, and the benefits that we get are significant enough to accept that. but… i never really was complete in my acceptance. i really wanted a better way.
and after adding write ahead log to voron, i started thinking. is there a reason to continue this? we are already dealing with writing to the log. so why modify the entire tree?
here is how it looks like:
now, when you are looking at the tree, and you ask for the b page, you’ll get the one in the log. however, older transactions would get to see the b page in the data file, not the one in the log. so we maintain mvcc.
this complicates matters somewhat because we need to sync the data file flushing with existing transactions (we can’t flush the file until all transactions have progressed to the point where they look at the new pages in the log), but that is something that we have to do anyway. and that would cut down the amount of writes that we have to do by a factor of ten, according to my early estimation. hell, this should improve write performance on sequential writes .
for that matter, look what happens when we need to split page d:
here we need to touch the tree, but only need to touch the the immediate root. which again, gives us a lot of benefits in the size we need to touch.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.