Over a million developers have joined DZone.

How Saving a Single Byte Cost Me Over a Day (and Was Solved With a Single Byte)

DZone's Guide to

How Saving a Single Byte Cost Me Over a Day (and Was Solved With a Single Byte)

An example of a simple error, a simple solution, and a single byte.

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

I’m currently doing something that I would rather not do. I’m doing significant refactoring to the lowest level of RavenDB, preparing some functionality that I need to develop some visible features. But in the meantime, I’m mucking about in the storage layer, dealing with how Voron actually persists and manages the information we have. And I’m making drastic changes. That means that stuff breaks, but we have got the tests to cover for us, so we are good in that regard - yeah!

The case for this post is that I decided to break apart a class that had too many responsibilities. The class in question was:


As you can see, there are multiple concerns here. There are actually several types of Page in the system, but changing this would have been a major change so I just tucked more responsibility into this poor class at the time.

When I finally got around to actually handling this it was part of a much larger body of work, but I felt really great that I was able to do that.

That was until I ran the tests, and a few of them broke. In particular, only the tests that deal with large amounts of information (over four million entries) broke. And they broke in a really annoying way - it looked like utter memory corruption was happening. It was scary, and it took me a long while to figure out what was going on.

Here is the fix:


This error happens when adding a new entry to a tree. In particular, this piece of code will only run when we have a page split on a branch page (that is, we need to move from two levels in the tree to three levels).

The issue turned out to be because when we split the Fixed Size Tree from the Variable Size Tree, I was able to save a single byte in the header of the fixed size tree page. The previous header was 17 bytes in size, and the new header was 16 bytes in size.

The size of BranchEntrySize is 16 bytes… and I think you get the error now, right?

Before, with page size of 4K, we had a PageMaxSize of 4079 bytes. So on the 255 entry, we would be over the limit, and split the page. By reducing a single byte from the header, the PageMaxSize was 4080. Because we only checked from greater than, we thought that we could write to that page; but we ended up writing to the next page, corrupting it.

The fix, ironically enough, was to add another byte, to check for greater than or equal.


It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate


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

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}