Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Distributed Databases

Key-Value Stores

Single Key GET

As a proof-of concept, we plan to hybridize our magpie KVS implementation with off-the-shelf single-node KVSs like Redis or Valkey. We do the partitioning and the query orchestration (e.g. multi-key operations over distinct storage nodes); instead of the StorageBucket objects we modeled our KVS implementation with, we have pointers to the objects that correspond to the spawned KVS instance. Since all accesses to the underlying KVS are mediated by an access to the object that "fronts" the instance, our cost model can keep track of accesses and perform access-based reorganization. Application-level tracking of co-accessed keys could enable application-driven key repartitioning, but the mechanism for this is still TBD so we won't consider it in the rest of this document.

Cross-shard requests

For multi-key GET queries that span partitions, we can leverage the same pattern as in our Magpie KVS and perform a multi-get operation that incorporates a fold() to aggregate the query results. Effectful multi-shard operations are still an open question, especially because we are coordinating effects that we are externalizing through an external subcomponent.

The potential problem here arises in the case where we are writing to two keys, "key-a" and "key-b" that belong to shards S1 and S2, respectively. If we model this as two distinct nanotransactions under one epic, as shown in the image below, we are exposed to two potential hazards:

Multi-Key PUT

  1. Partial Failure: If one of the shards is unreachable (e.g. S2), rolling back means that we need to issue a compensating transaction for the put_internal() nanotransaction that succeeded. This is not a big problem because we always had some plan to support compensating transactions. Unfortunately, this still leaves us exposed to
  2. Race Conditions: If a multi-key PUT operation is issued concurrently to a multi-key GET operation for the PUT operation's keys, clients could potentially observe one key set to the new value and the other set to its old value (or not even set, depending on the initial conditions). If the multi-key GET concerns only one of the keys, for example ["key-a", "key-c"], then a client could potentially observe the value written for "key-a" by the PUT epic between the moment when the request was submitted to the external key/value store and the time when magpie ran the compensating nanotransaction.

Magpie does not really offer a solution to this, since state is managed externally.

Relational Databases

For a proof-of-concept, we plan to use single-node databases like SQLite or DuckDB. The "wrapping" Magpie library needs to maintain a list of tables that exist in the database, the set of keys to use for per-instance partitioning, and per-table statistics to support high-level query planning (mostly for cross-partition joins).

How

Queries come in as text or as nanotransactions that end up emitting SQL queries to be submitted to the DB. For text queries, we use the datafusion Rust libraries for query parsing and extraction of relevant details. In our proof-of-concept we will support

  • INSERT queries
  • UPDATE
  • SELECT queries without JOIN clauses

The plan is to inspect the target tables for the query, and either i) forward the query to all instances if there is no information to narrow down the target instances (as could be the case for SELECT queries without filters) or ii) extract information (e.g. the columns participating in a partition key for INSERT) queries to direct the query to the responsible shard.

To send the actual query to the underlying database, we can leverage substrait.

Open Questions

  • If we're wrapping OLTP databases (e.g. SQLite) and we are partitioning, what do we do about cross-partition transactions? If we have an update that would ordinarily acquire a lock on two rows and these two rows end up on different hosts, how do we write an epic that has the same effect as a single-host database at the user's chosen isolation level?
    • This will be a problem even for non-OLTP applications. Say we want the equivalent of a redis transaction over keys in separate shards -- how do we make them atomic?
  • Are there cases where we need to perform a query rewrite before forwarding it to an external database?