Raven's Storage: Understanding the SST File Format
Join the DZone community and get the full member experience.Join For Free
this is an example of an sst that stores:
- tests/0000 –> values/0
- tests/0001 –> values/1
- tests/0002 –> values/2
- tests/0003 –> values/3
- tests/0004 –> values/4
as well as a bloom filter for rapid checks.
if you are wondering about the binary format, that is what this post is all about. we actually start from the end. we have the last 48 bytes of the file are dedicated to the footer.
the footer format is:
last 8 bytes of the file is a magic number: 0xdb4775248b80fb57ul – this means that we can quickly identify whatever this is an sst file or not.
here is what this number looks like broken down to bytes:
- the other 40 bytes are dedicated for the metadata handle and the index handle.
those are two pair of longs, encoded using 7 bit encoding, in our case, here is what they look like:
let us see if we can parse them properly:
- 143, 1 = 143
note that in order to encode 143 properly, we needed two bytes, because it is higher than 127 (and we use the last bit to indicate if there are more items to read). the first two values are actually the metadata handle (position: 100, count: 38), the second are the index handle (position: 143, count: 14).
we will start by parsing the metadata block first:
you can see the relevant portions in the image.
the actual interesting bits here are the first three bytes. here we have:
- 0 – the number of shared bytes with the previous key (there is no, which is why it is zero).
- 25 – the number of non shared bytes (in this case ,the full value, which is 25).
- 2 – the size of the value, in this case ,the value is the handle in the file of the data for the filter.
you can read the actual key name on the left ,”filter.builtinbloomfilter”, and then we have the actual filter data:
you can probably guess that this is the filter handle (position: 82, count: 18).
the rest of the data are two 4 bytes integers. those are the restart array (position 130 –133) and the restart count (position 134 – 137). restarts are a very important concept for reducing the size of the sst, but i’ll cover them in depth when talking about the actual data, not the metadata.
next, we have the actual filter data itself, which looks like this:
this is actually a serialized bloom filter, which allows us to quickly decide if a specified key is here or not. there is a chance for errors, but errors can only be false positive, never false negative. this turn out to be quite useful down the road, when we have multiple sst files are need to work with them in tandem. even so, we can break it apart into more detailed breakdown:
- the first 8 bytes are the actual serialized bloom filter bits.
- the 9th byte is the k value in the bloom filter algorithm. the default value is 6.
- the last value (11) is the lg value (also used in the bloom filter algo).
- the rest of the data is a bit more interesting. the 4 bytes preceding the 11 (those are 9,0,0,0) are the offset of valid data inside the filter data.
- the four zeros preceding that are the position of the relevant data in the file.
honestly, you can think about this as a black box. filter data is probably enough.
now that we are done with the filter, we have to look at the actual index data. this is located on 143, but we know that the filter data is actually ended on 100 + 38, so why the gap? the answer is that after each block, we have a block type (compressed or not, basically) and the block crc, which is used to determine if the file has been corrupted.
back to the index block, is tarts at 143 and goes for 14 bytes, looking like this:
the last 4 bytes (32 bit int equal to 1) is the number of restarts that we have for this block. and the 4 bytes preceding them (32 bit int equal to 0) is the offset of the restarts in the index block.
in this case, we have just one restart, and the offset of that is 0. hold on about restarts, i’ll get there. now let us move to the beginning of the index block. we have the following three bytes: 0,1,2.
just like in the meta block case, those fall under (shared, non shared, value size) and are all 7 bit encoded ints. that means that there is no shared data with the previous key (because there isn’t previous key), the non shared data is 1 and the data size is 2. if you memorized your ascii table, 117 is lower case ‘u’. the actual value is a block handle. this time, for the actual data associated with this index. in this case, a block with position: 0 and count: 70.
let us start analyzing this data. 0,10, 8 tells us shared, non shared, value . indeed, the next 10 bytes spell out ‘tests/0000’ and the 8 after that are ‘values/0’. and what about the rest?
now we have 9,1,8. shared is 9, non shared 1 and value size is 8. we take the first 9 bytes of the previous key, giving us ‘tests/000’ and append to it the non shared data, in this case, byte with value 49 (‘1’ in ascii), giving us a full key of ‘tests/0001’. the next 8 bytes after that spell out ‘values/1’. and the rest is pretty much just like it.
now, i promised that i would talk about restarts. you see how we can use the shared/non shared data to effectively compress data. however, that has a major hidden cost. in order to figure out what the key is, we need to read all the previous keys.
in order to avoid this, we use the notion of restarts. every n keys (by default, 16), we we will have a restart point and put the full key into the file. that means that we can skip ahead based on the position specified in the restart offset, and that in turn is governed by the number of restarts that we have in the index block.
and… that is pretty much it. this is the full and unvarnished binary dump of an sst. obviously real ones are going to be more complex, but they all follow the same structure.
Opinions expressed by DZone contributors are their own.
How AI Will Change Agile Project Management
Managing Data Residency, the Demo
Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL