Skip to content

Commit 7bb7e7a

Browse files
authored
When sharding, use the where clause in the shard key to distribute requests more evenly (#2229)
1 parent e90235b commit 7bb7e7a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

apps/webapp/app/services/realtimeClient.server.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class RealtimeClient {
292292
): URL {
293293
const $url = new URL(url.toString());
294294

295-
const electricOrigin = this.#resolveElectricOrigin(environment.id);
295+
const electricOrigin = this.#resolveElectricOrigin($url, whereClause, environment.id);
296296
const electricUrl = new URL(`${electricOrigin}/v1/shape`);
297297

298298
// Copy over all the url search params to the electric url
@@ -483,14 +483,30 @@ export class RealtimeClient {
483483
return `${this.options.keyPrefix}:${environmentId}`;
484484
}
485485

486-
#resolveElectricOrigin(environmentId: string) {
486+
#resolveElectricOrigin(url: URL, whereClause: string, environmentId: string) {
487487
if (typeof this.options.electricOrigin === "string") {
488488
return this.options.electricOrigin;
489489
}
490490

491-
const index = jumpHash(environmentId, this.options.electricOrigin.length);
491+
const shardKey = this.#getShardKey(whereClause, environmentId);
492492

493-
return this.options.electricOrigin[index] ?? this.options.electricOrigin[0];
493+
const index = jumpHash(shardKey, this.options.electricOrigin.length);
494+
495+
const origin = this.options.electricOrigin[index] ?? this.options.electricOrigin[0];
496+
497+
logger.debug("[realtimeClient] resolveElectricOrigin", {
498+
whereClause,
499+
environmentId,
500+
shardKey,
501+
index,
502+
electricOrigin: origin,
503+
});
504+
505+
return origin;
506+
}
507+
508+
#getShardKey(whereClause: string, environmentId: string) {
509+
return [environmentId, whereClause].join(":");
494510
}
495511

496512
#registerCommands() {

docker/docker-compose.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ services:
4848

4949
electric:
5050
container_name: electric
51-
image: electricsql/electric:1.0.13@sha256:4e69c4a6ec3e976efbdd8b7e6de427e771aeacdbc0c8c7ca22eb0ca6ab1611ff
51+
image: electricsql/electric:1.0.22@sha256:1b151dc8a6fd5292fba70cbc744e25bd3a1e42db5fc89b7e479b7b939980a0e2
5252
restart: always
5353
environment:
5454
DATABASE_URL: postgresql://postgres:postgres@database:5432/postgres?sslmode=disable
@@ -60,6 +60,21 @@ services:
6060
depends_on:
6161
- database
6262

63+
electric-shard-1:
64+
container_name: electric-shard-1
65+
image: electricsql/electric:1.0.22@sha256:1b151dc8a6fd5292fba70cbc744e25bd3a1e42db5fc89b7e479b7b939980a0e2
66+
restart: always
67+
environment:
68+
DATABASE_URL: postgresql://postgres:postgres@database:5432/postgres?sslmode=disable
69+
ELECTRIC_INSECURE: true
70+
ELECTRIC_REPLICATION_STREAM_ID: "triggershard1"
71+
networks:
72+
- app_network
73+
ports:
74+
- "3061:3000"
75+
depends_on:
76+
- database
77+
6378
clickhouse:
6479
image: bitnami/clickhouse:latest
6580
container_name: clickhouse

0 commit comments

Comments
 (0)