SSTable is a data structure that stores key- value pairs in an ordered format optimized…
- User
SSTable is a data structure that stores key- value pairs in an ordered format optimized for efficient reads and disk storage.
It introduces a new requirement to segment files: it requires that the content of the segments was sorted.
Each key only appears once within each merged segment file (compaction already ensures that).
We need a new kind of compact operation: merge and sort. Merge and sort complexity: O(n * log n).
Using SSTables we don't need to keep a complete key index in memory (sparse index).
We can use an in-memory sparse index with one key per segment to locate other keys by scanning from nearby offsets.
Example: a key like handiwork, when you know the offsets for the keys handback and handsome, you know handiwork must appear between those two. You can jump to the offset for handback and scan from there until you find handiwork, if not, the key is not present.
With this we solve the range query.
Maintaining a sorted structure on disk is possible, but it's much easier to manage in memory using tree data structures like red-black or AVL trees.
Records can be grouped in segments and compressed before being written to disk, which enhances storage efficiency during read requests.
Compaction and merging process is carried out time to time, or when the system detects two identical keys in two different segments.
A memtable is an in-memory (usually a tree), sorted, mutable data structure used to keep a segments of an SSTable.
When a write comes in, add it to an in-memory balanced tree structure (memtable) When the memtable gets bigger than some threshold (megabytes), write it out to disk as an SSTable file. Writes can continue to a new memtable instance. On a read request, try to find the key in the memtable, then in the most recent on-disk segment, then in the next-older segment, etc.
What Happens if a database using a Memcached structure crashes?
o The most recent writes are lost...
To avoid losing information, we can Keep a Write Ahead Log (WAL) file in disk with same info than memtable. That log is not in sorted order, but that doesn't matter, because its only purpose is to restore the memtable after crash. Every time the memtable is written out to an SSTable, the log can be discarded. When system fails: Rebuild memtable from log file (slow but safe). Must add all keys and values. Expected to be used only in emergencies (not often).
LSM Trees are a data structure used in NoSQL databases to efficiently manage large data volumes. They are designed to optimize performance in systems that handle high rates of data insertion and updates. It organizes the data into layers of sorted structures, each with more and more compressed data. The top layer is the fastest to access but the least compressed. As the data piles up, it’s compressed and written into a new SSTable. We can decide how many layers we need and how much compression to apply.
- Assistant