Sophia database and its architecture was born as a result of research and reconsideration of primary alghorithmic constraints that relate to growing popular Log-file based data structures, such as LSM-tree, B-tree, etc.
Most Log-based databases tend to organize own file storage as a collection of sorted files which are periodically merged. Thus, without applying some key filtering scheme (like Bloom-filter) in order to find a single key, database has to traverse all files that can take up to O(files_count * log(file_key_count)) in the worst case, and it's getting even worse for range scans, because Bloom-filter is incapable to operate with key order.
Sophia was designed to improve this situation by providing faster read while still getting benefit from append-only design.
Following sections show Sophia design and evolution from version 1.1 to 1.2 (latest).
Original Sophia's architecture combines a region in-memory index with an in-memory key index.
A region index is represented by an ordered range of regions with their min and max keys and latest on-disk reference. Regions never overlap.
These regions have the same semantical meaning as the B-Tree pages, but designed differently. They do not have a tree structure or any internal page-to-page relationships, thus no meta-data overhead (specifically to append-only B-Tree).
A single region on-disk holds keys with values. As a B-tree page, region has its maximum key count. Regions are uniquely identified by region id number that makes them trackable in future.
Key index is very similar to LSM zero-level (memtable), but has a different key lifecycle. All modifications first get inserted into the index and then hold up until they are explicitly removed by merger.
The database update lifecycle is organized in terms of epochs. Epoch lifetime is set in terms of key updates. When the update counter reaches an epoch's watermark number then the Rotation event happen.
Each epoch, depending on its state, is associated with a single log file or database file. Before getting added to the in-memory index, modifications are first written to the epoch's write-ahead log.
On each rotation event:
The merger thread is the core part that is responsible for region merging and garbage collecting of the old regions and older epochs. On the wakeup, the merger thread iterates through list of epochs marked as 'transfer' and starts the merge procedure.
The merge procedure has the following steps:
The garbage collector has a simple design.
All that you need is to track an epoch's total region count and the count of transfered regions during merge procedure. Thus, if some older epoch database has fewer than 70% (or any other changeable factor) live regions, they just get copied to the current epoch database file while the old one is being deleted.
On database recovery, Sophia tracks and builds an index of pages from the earliest epochs (biggest numbers) down to the oldest. Log files then are being replayed and epochs become marked as 'transfer'.
Sophia has been evaluated as having the following complexity (in terms of disk accesses):
set worst case is O(1) append-only key write + in-memory index insert
delete worst case is O(1) (delete operation is equal to set)
get worst case is O(1) random region read, which itself does amortized O(log region_key_count) key compares + in-memory key index search + in-memory region search
range range queries are very fast due to the fact that each iteration needs to compare no more than two keys without a search, and access through mmaped database. Roughly complexity can be equally evaluated as sequential reading of the mmaped file.
There have been major changes in storage architecture since version 1.1.
Version 1.1 defines strict 2-level storage model between in-memory and disk. It gives worst-case guarantee O(1) for any ordered key read in terms of disk access.
This approach had its limitations, since it was unable to efficiently maintain memory limit with required performance. Additionally there was a need for multi-threaded compaction.
Sophia has evolved in a way that expands original ideas. Architecture has been designed to efficiently work with memory and large amount of keys.
In fact, it became even simplier.
Sophia's main storage unit is a Node.
Each Node represents a single file with associated in-memory region index and two in-memory key indexes. Node file consists of Branches.
Each Branch consists of a set of sorted Regions and Region Index.
A single Region holds keys with values. It has the same semantical meaning as a B-Tree page, but organized in a different way. It does not have a tree structure or any internal page-to-page relationships and thus no meta-data overhead.
A Region Index is represented by an ordered range of regions with their min and max keys and on-disk reference. Regions never overlap.
A Key Index is very similar to LSM zero-level (memtable), but has a different key lifecycle. All modifications first get into the index and hold up until they are explicitly removed by the merger.
Before getting added to the in-memory Key Index, modifications are first written to the Write-Ahead Log.
Database lifecycle is organized in terms of two major operations: Branch and Compaction.
When a Node's in-memory Key Index size reaches a special watermark value or global memory limit, Branch operation is scheduled.
When some Node branch counter reaches a special watermark value, Compaction operation is scheduled.
All background operations are planned by special scheduler.
There is a game between available memory, a number of Branches and Search times.
Each additional branch says that there is a possible additional disk access during the search. During the search, only branch regions that have min >= key <= max are examined. In the same time, it is unable to maintain memory limits without branching, because compaction times are greater than possible rate of incoming data.
Sophia is designed to be read optimized. There is a high possibility that latest created Branches (hot data) are stored in the file system cache. Scheduler is aware about nodes which have largest in-memory Key Index and biggest number of Branches. These are processed first.
Additionally all operations are planned taking current system state in account, like memory usage statistics, current load profiler (redzone), operations priorities, checkpoint, backup, etc.
Sophia compaction is multi-threaded. Each worker (thread) requests scheduler for a new task. Basic unit of a background task is an operation on a single Node.
Sophia is designed to efficiently utilize available memory. If there is more memory available, then branch/compaction operations become more infrequent and system becomes more disk-efficient. Best performance can be obtained with no memory limit set. Sophia is Hard-Drive (and Flash) friendly, since all operations are delayed and executed in large sequential reads and writes, without overwrite.
Garbage collection (MVCC related) is executed automatically by Compaction task.
Also, scheduler periodically checks if there are any nodes which have large percentage of transactional versions (duplicates) stored per node.
Sophia has following algorithmic complexity (in terms of disk access):
set worst case is O(1) write-ahead-log append-only key write + in-memory node index search + in-memory index insert
delete worst case is O(1) (delete operation is equal to set)
get worst case is amortized O(max_branch_count_per_node) random region read from a single node file, which itself does in-memory key index search + in-memory region search
range worst case of full database traversal is amortized O(total_region_count) + in-memory key-index searches for each node