Handling a shard-key lookup across two databases
I wanted to build a lightweight database proxy that supports both MySQL and PostgreSQL. I was particularly interested in sharding and Redis-backed caching, because applications often end up implementing parts of that logic themselves.
DBProxy is my way of exploring those problems. In this post, I’ll focus on one case: an IN query whose keys belong to two database shards.
A batch lookup in an application might look like this:
SELECT id, status FROM orders WHERE tenant_id IN (101, 102, 104); Assume the orders table uses integer modulo routing across two configured shards. Tenant 101 maps to shard 1, while tenants 102 and 104 map to shard 0. With the multi-key lookup enabled, DBProxy partitions the request into two queries. In this example, they look like this:
-- Shard 0 SELECT id, status FROM orders WHERE tenant_id IN (102, 104); -- Shard 1 SELECT id, status FROM orders WHERE tenant_id IN (101); The application can split the IDs and combine the results itself. It also needs to decide what to do when one database is slow or unavailable. DBProxy’s opt-in path handles the split for a supported single-table lookup.
What happens when a shard fails?
Suppose shard 0 returns orders for tenants 102 and 104, but shard 1 times out. Returning the first set of rows would leave the application unable to tell whether tenant 101 had no orders or its database never answered.
DBProxy buffers the results and checks the metadata before returning rows. If a branch fails, the client gets an error. The branches share a deadline and limits on the number of rows and bytes.
Buffering uses memory, and the caller waits for the slower branch. The row, byte, and deadline limits help bound that cost. They matter when deciding whether this is a good fit for a batch.
One detail from testing
A live MySQL test caught a problem that the planner tests had missed. A prepared query selected an integer, but the client connection closed while the proxy encoded the result. The integer width sent on the wire did not match the column metadata.
The encoding was fixed and checked again with text and prepared queries on cold and warm cache runs. Routing tests and client-level tests cover different parts of the request, including what the database driver actually receives.
Where the cache fits
DBProxy currently offers an optional Redis or Valkey read-through cache for MySQL. MySQL remains the source of truth. This is a MySQL cache feature; the PostgreSQL transaction-mode lookup also supports the shard split, but the Redis-backed result cache discussed here is for MySQL.
Caching is enabled per table and applies to eligible buffered, replica-eligible SELECT queries. Reads inside transactions, primary-only reads, and tables without a cache rule bypass it. For an eligible multi-key lookup, a warm shard branch can come from Redis or Valkey while a cold branch still queries MySQL.
Successful writes through DBProxy invalidate the affected table and shard’s cache generation. Transactional writes are handled at commit boundaries. Direct writes to MySQL bypass that invalidation path, so use a bounded TTL or an external invalidator that meets the documented contract. Redis failures and invalidation failures also follow the configured cache behavior and TTL bounds.
Trying the lookup
Start with two MySQL backends you can safely stop for a test. Provision the same orders schema and permissions on both, then configure each shard and its backend. DBProxy routes to databases you already run; it does not create shards or move their data.
The MySQL sharding section needs the opt-in setting and a rule for the table:
[sharding] enabled = true allow_multikey_scatter = true [[sharding.rules]] table = "orders" column = "tenant_id" shards = ["orders-0", "orders-1"] strategy = "hash" The backend declarations, credentials, and full example configuration are in the sharding guide linked below. For PostgreSQL and TimescaleDB, use transaction routing mode and enable postgres.sharding.allow_multikey_scatter = true instead.
Put test rows for tenant 101 on shard 1 and tenants 102 and 104 on shard 0. Connect through DBProxy and run the original lookup. With both backends healthy, check that rows from both shards are returned. Then make one test backend unavailable and run it again. The expected result is a client error before any rows are returned. The exact error and timing depend on the failure and configuration.
This row lookup accepts a direct, single-table read with one shard-key IN predicate. Joins, global ordering, LIMIT/OFFSET, and grouping are rejected on this path. Supported aggregates have a separate merge path.
Configuration and sharding examples:
https://github.com/rahulbsw/dbproxy-rs/blob/main/docs/sharding-examples.md
MySQL cache eligibility and invalidation:
https://github.com/rahulbsw/dbproxy-rs/blob/main/docs/cache.md
Implementation and test discussion:
原文:https://www.a8q9.cn/en/article/handling-a-shard-key-lookup-across-two-databases-42dc58.html