Fly.ioProblem & solution
Globally Distributed Postgres
Summary
Globally distributed Postgres describes a way to serve a conventional CRUD application across regions while keeping Postgres writes directed to one primary and reads near users. Fly’s proxy sends each request to the local application instance, where most requests succeed; when a write reaches a read-only replica, Rails catches PG::ReadOnlySqlTransaction and returns a fly-replay header naming PRIMARY_REGION. The proxy then retries the complete HTTP request inside its network, avoiding per-query cross-region database routing and allowing the application to retain its ordinary connection behavior. The approach can also support session-based read-your-own-writes and replica-freshness checks, but it is not suitable for every workload; the text contrasts it with geographically distributed databases such as CockroachDB.
Context
Geographically distributed applications can use read replicas for fast local reads, but writes landing on multiple database instances are difficult to manage. Rails connection switching can address this, but the text describes it as painful to configure and deliberately simplified.
Approach / What changed
Each application instance attempts to handle every request. If a write reaches a read-only Postgres replica, Rails catches PG::ReadOnlySqlTransaction and sets a fly-replay header for PRIMARY_REGION; Fly’s proxy retries the entire HTTP request in the writer’s region.
Takeaways
Fly Postgres can be configured with a writer in one region and read replicas in other application regions; the text presents flyctl postgres create as the command to create such a cluster.
The Rails implementation uses eight lines to detect PG::ReadOnlySqlTransaction and direct Fly’s proxy to replay the request in the primary region.
The pattern is intended for read-heavy applications and is not a cure-all; distributed-by-design databases such as CockroachDB are presented as an alternative for write-heavy scenarios.
Source and freshness
- Source
- fly.io
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.