Skip to content

Commit eafccce

Browse files
authored
feat(db): split EU read traffic across two replicas (#1718)
Randomly select one of POSTGRES_REPLICA_EU_URL or POSTGRES_REPLICA_EU_URL_2 at module init so ~2,200 concurrent Vercel instances distribute ~50/50 across two EU read replicas, halving per-replica connection pressure.
1 parent b447254 commit eafccce

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/lib/drizzle.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,22 @@ export function isUSRegion(): boolean {
4747
/**
4848
* Get the read replica URL based on deployment region.
4949
* - US deployments use the US replica (POSTGRES_REPLICA_US_URL) for lower latency
50-
* - EU deployments use the EU replica (POSTGRES_REPLICA_EU_URL) to split read
51-
* traffic off the primary's connection budget (98.8% of traffic is fra1)
50+
* - EU deployments randomly select one of two EU replicas to split read traffic
51+
* across ~2,200 concurrent Vercel instances (~50/50 statistical distribution)
5252
* - Falls back to primary if no replica URL is configured for the region
5353
*/
5454
function getReplicaUrl(): string {
5555
if (isUSRegion()) {
5656
const usReplica = getEnvVariable('POSTGRES_REPLICA_US_URL');
5757
if (usReplica) return usReplica;
5858
} else {
59-
const euReplica = getEnvVariable('POSTGRES_REPLICA_EU_URL');
60-
if (euReplica) return euReplica;
59+
const euReplicas = [
60+
getEnvVariable('POSTGRES_REPLICA_EU_URL'),
61+
getEnvVariable('POSTGRES_REPLICA_EU_URL_2'),
62+
].filter(Boolean) as string[];
63+
if (euReplicas.length > 0) {
64+
return euReplicas[Math.floor(Math.random() * euReplicas.length)];
65+
}
6166
}
6267

6368
return postgresUrl;

0 commit comments

Comments
 (0)