Low-Level Voron Optimizations: Recyclers Do It Over and Over

DZone 's Guide to

Low-Level Voron Optimizations: Recyclers Do It Over and Over

This is an optimal data access pattern, preserving the most juice from the drive and giving us the best possible performance.

· Performance Zone ·
Free Resource

One of the key rules in optimization work is that you want to avoid work as much as possible. In fact, any time that you can avoid doing work, that is a great help to the entire system. You can do that with caching, buffering, pooling, and many other such common patterns.

With Voron, one of our most common costs is related to writing to files. We are doing quite a lot of work around optimizing that, but in the end, this is file I/O and it is costly.

A big reduction in the cost of doing such I/O is to preallocate the journal files. That means that instead of each write extending the file, we ask the operation system to allocate it to its full expected size up front. This saves time and also ensures that the OS has a chance to allocate the entire file in as few fragments as it possibly can.

However, every dog has its day, and eventually, a journal has outlived its usefulness, which means that it is time to make a hotdog — or, as the case may be, delete the now useless journal file.

Of course, eventually, the current journal file will be full, and we’ll need a new journal file — in which case we’ll ask the OS to allocate us a new one and pay the cost of doing all of this I/O and the cost of file allocations.

Hmm. That seems pretty stupid, doesn’t it, when you think about the whole system like that?

Instead, we now reuse those journals. We rely on the fact that file rename is atomic in both Windows and Posix so we can avoid expensive allocation calls and reuse the buffers.

Here is what heavy writes benchmarks look like:


It is important to note that we not only have to do some management here (to only keep pending journals for a period of time if they aren’t being used) but also need to handle a very strange case. Because we are now reusing a valid journal file, we have a case where we might read valid transactions — but ones that are obsolete. This means that we need to be aware that beyond just garbage, we might have to encounter some valid data that is actually invalid. That made us tighten our journal validation routine by quite a bit. 

There is also another advantage of this approach: it also plays very well with the underlying hardware. The reuse of the already-allocated files means that the disk has to do a lot less work, it reduces fragmentation, and it allows much faster responses overall. According to research papers, the difference can be a factor of four difference on modern SSD drives. This is a really good thing since this means that this approach has wide applicability across mass storage devices (SSD, HDD, etc). I actually had a meeting with a storage company to better understand the low-level details of how a disk manages the bits and how some of this behavior is influenced by those discussions.

I’m ignoring a lot of previous work that we have done around that (aligned writes, fixed sizes, pre-allocation, etc.), of course, and just focusing on the new stuff.

Some of that only applies to that particular manufacturer disks, but a lot of that has broader applicability. In short, the idea is that if we can keep the number of writes we do to a few hot spots, the disk can recognize that and organize things so this would be optimized. You can read a bit more about this here, where it discusses the notion of multiple internal storage tiers inside a disk.

The idea is that we provide the disk with an easily recognizable pattern of work that it can optimize. We looked at using the disk low-level options to tell it directly what we expect from it, but that is both hard to do and will only work with specific brands of disks. In particular, with cloud storage, it is very common to just lose all such notions of being able to pass hints to the disk itself, even while the underlying storage could handle it. (In the previous presentation, this is called I/O tagging and latency and priority hints.)

Instead, by intentionally formatting our I/O in easily recognizable pattern, we have much higher applicability and ensure that the Right Thing will happen. Sequential writes, in particular (the exact case for journals) will typically hit a non volatile buffer and stay there for a while, letting the disk optimize its I/O behavior even further.

Another good read on this talks about StableBuffer (you can ignore all the other stuff about decomposing and reordering I/O). Just the metrics about how much a focused write like that can help is very good.

This resource also indicates that this is an optimal data access pattern, preserving the most juice from the drive and giving us the best possible performance.

optimization ,performance ,recyclers ,voron

Published at DZone with permission of Oren Eini , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}