Optimizing I/O Throughput
Want to really boost performance? Consider optimizing your I/O throughput.
Join the DZone community and get the full member experience.Join For Free
we got a customer request about performance issues they were seeing on startup on a particular set of machines.
those machine run in a cloud environment, and they have… peculiar, one might say deviant, i/o characteristics. in particular, the i/o pipeline on those machines is wide, but very slow. what do i mean by that? i meant that any particular i/o operation on those is likely to be slow, but the idea is that you can get much better performance if you issue concurrent i/o. the system is supposed to be able to handle that much better, and overall you’ll see the same relative performance as elsewhere.
this is pretty big issue for us, because for many things, we really do care about serial i/o performance. for example, if we are committing a transaction, we really have no other way to handle it except to wait until the i/o is fully completed.
that said, the particular scenario where we had the problem was startup. if the database was under heavy load at the time it shut down, the recovery logs would be full, and the database would need to replay the recent actions that happened. note that shutdown performance is important, because it many cases we are running in an environment where shutdown comes with a ticking clock (in iis or as a windows service).
at startup, we usually have more time, and it is expected that we’ll take a while to get up to speed. if nothing else, just bringing enough of the database to memory is going to take time, so on large databases, startup time is expected to be non trivial.
that said, the startup time on those set of machines was utterly atrocious. to figure out what is going on, i pulled out process monitor and looked at the file i/o. we go this:
we are reading from a journal, and that is pretty serial i/o (in the image, i’m running of a remote network drive, to simulate slow responses). note that we need to read the log in a serial fashion, and the way the os reads things, we read 32kb at a time.
remember, we are reading things in a serial fashion, and that means that we have a lot of page faults, and we have a slow i/o system, and we execute them serially.
yes, that is a killer for perf. by the way, when i’m talking about slow i/o system, i’m talking about > 0.5 ms per disk read for most requests (ideally, we would have latency of 0.05 – 0.15). and we have quite a few of those, as you can imagine.
since i know that we are going to be reading the whole journal, i used the prefetchvirtualmemory() method and passed it the entire file (it is a maximum of 64mb, and we are going to need to read it all anyway). this let the os have the maximum amount of freedom when reading the data, and it generate big, concurrent i/o. here is how this looks like:
this also give the wide i/o bandwidth a chance to play. we load the i/o subsystem with a lot of stuff that it can try to do in an optimized fashion.
the next part that was expensive was that we need to apply the data from the journal files to the data file, and sync it.
the performance of syncing a file is related to the size of the file, unfortunately. and the file in question was large, over 45gb. especially on such a system, we saw a lot of latency here, as in multiple minutes . one obvious optimization was to not sync per journal file, but sync once per the whole recovery process. that helped, but it was still too expensive.
next, we tried pretty much everything we could think about.
- switching to writefile (from using mmap and then calling flushviewoffile)
- using async i/o (writefileex)
- using scatter / gather i/o with no buffering (saves the need to do sync in the end)
- completion ports
- asking a 4 months old baby girl what she think about it (she threw up on the keyboard, which is what i wanted to do at the time, then she cried, and i joined her)
nothing seems to have worked. the major issue was that in this workload, we have a large file (45gb, as i said) and we are writing 4kb pages into it in effectively random places. in the workload we were trying to work with, there were roughly 256,000 individual 4kb writes (most of them weren’t consecutive, so we couldn’t get the benefit of that). that is about 1 gb of writing to do.
and nothing we could do would get us beyond 3mb/sec or so. saturating the i/o subsystem with hundreds of thousands of small writes wouldn’t work, and we were at a loss. note that a small test we made, just copying data around manually has resulted in roughly 10ms/sec peek performance on those machines. this is a very lame number, so there isn’t much that we can do.
then i thought to ask, why are we seeing this only during startup? surely this happens also on a regular basis. why didn’t we notice?
the reason for that is pretty simple, we didn’t notice because we amortize the cost. only on startup did we had to actually sit and wait for it to complete. so we dropped that requirement. we used to read all the journals, apply them to the data file, sync the data files and then delete the journals. now we read the journals, apply them (via a memory map) to the data file, and only remember what is the last journal file we applied in memory.
there is a background process running that will take care of syncing the data file (and deleting the old journals). if we crash again, we’ll just have to replay the logs that we aren’t sure were synced before. this saves even more time.
but we still have another issue. writing to memory mapped file require the os to page the relevant pages into memory. and again, we are on slow i/o, and the os will only page the stuff that we touch, so this is again a serial process that this time require us to load to memory about 1gb of data at 3mb/sec. that is… not a good place to be at. so the next step was to figure out all the addresses we’ll be writing to, and letting the os know that we’ll be fetching them. we do some work to make sure that we load those values (and neighboring pages) to memory, then we can write to them without paging for each page individually.
a nice side effect of this is that because this is running on the latest changes in the system, this has the effect of preloading to memory the pages that are likely to be in used after the database has started.
that is a lot of work, but to be perfectly frank, this is mostly optimizing in a bad environment. the customer can’t walk away form their current machine easily, but the i/o rates those machines have would make any database sit in a corner and cry.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.