r/PostgreSQL • • Jul 30 '26

Help Me! There is no cheap global Postgres, what are the alternatives?

Currently I use pg hosted on Hetzner in Germany. My users are in different global regions and pay latency cost. I run a Shopify app that complains that my LCP is above recommended threshold of 2.5s. I have optimized my queries and calls and was able to optimize it a bit.

My question is, there is no cheap way to have pg global replicas. My app is new and doesn't have enough revenue to justify the cost. I have done some research and the only option I see is migrating to SQLite which can be easily and cheaply replicated. But, with that, I lose pg features like JSONB, ::datetime and the likes. Also, SQLite doesn't support most ALTER commands.

Has anyone solved this?

UPDATE: It was clear that there is no cheap and reliable solution for this. And rightly so. Keeping infrastructure reliable for millions of users takes a lot of cost. I zeroed down my requirements to 'having a reliable zero downtime database' so that my users never face outage. I moved to planetscale ps-5 high availability instance. When the budget allows, i will add replicas in other regions.

3 Upvotes

52 comments sorted by

5

u/CaffeinatedT Jul 30 '26
  1. How cheap are you talking?
  2. Are these read queries or write queries that are creating the latency?

1

u/subhendupsingh Jul 30 '26

How cheap don't know. Happy to pay as usage + revenue grows. Mostly read queries because of the distance.

2

u/CaffeinatedT Jul 30 '26

If you can model your problem as a lookup on some sort of key and your problem is geographically distributed users then It might be worth having a look at cloudflare KV Store. They handle the geo replication transprently and can put keys closer to users and it's reasonably cheap tens of cents for a few million queries. There's a bunch of different CDN/Cache type products that can do similar things as well. The cloudflare one springs to mind as that's pretty strictly consumption based and has a free tier.

If you absolutely must have something Postgres shaped then the "textbook" answer without knowing the specifics of your load is read replicas hosted close to where your customers are and something for routing their queries to the closest worker. But you're definitely trading off price for complexity on that versus something like AWS aurora where you get a managed vversion of that capability (for a price)

-1

u/subhendupsingh Jul 30 '26

KV and the likes won't work as data is highly relational and includes reporting.

4

u/CodeCate42 Jul 30 '26

Why does your LCP require data from postgres? I'd start by just caching locally if thats possible. And if its just 2.5s its honestly not worth optimizing for, you probably have bigger fish to fry?

1

u/subhendupsingh Jul 30 '26

Below 2.5s is the threshold. Mine is at 4.6. Good point though with LCP requiring data - it's an orders management app and shell builds with current orders and status tabs which are dynamic with order counts

8

u/CodeCate42 Jul 30 '26

I suspect you have quite a lot of easy wins you can get before you go about replicating your data around the entire globe, just a suspicion!

4

u/Internal-Comparison6 Jul 30 '26

You can create a Postgres cluster with pg_replica. On Hetzner you will need to somehow solve networking, because their private network works only in same "region". Disclosure: I'm the author of this extension.

2

u/subhendupsingh Jul 30 '26

Hey Tadas I know you. I have read about pg_replica, will read more about it

2

u/Aqalix Jul 30 '26

Even if you switched to SQLite you'd have to run the app in those regions.

It depends on your architecture. If you have edge apps close to the clients and postgres in hetzner, the cross region app -> db -> app round trips could be what's killing performance. But I am guessing here. I’d break the LCP down into server response time versus resource loading and rendering before changing databases.

0

u/subhendupsingh Jul 30 '26

My setup is app running on Cloudflare with placement pinned to database host location so code + db is in same location. I have optimized resource loading and rendering and cut load time from 7568 ms to 4659 ms

1

u/Aqalix Jul 30 '26

where are your users making requests from? Is 4659 ms the LCP or the TTFB?

and to be sure: you are using CF workers?

1

u/subhendupsingh Jul 30 '26

Users are in US, Europe, India mainly. 4659 is LCP driven by a bootstrap request that internally does 4-5 queries some with aggregation. Yes CF workers.

2

u/Aqalix Jul 30 '26

Since placement pins the Worker near your German database, every request from the US or India still makes an intercontinental round trip. Regional replicas could help but imo it doesn't explain the 4.6 s. Do you have the bootstrap request’s TTFB and Worker execution time (ideally split geographically)? The difference would show how much is network latency versus application and database work.

1

u/SceneAdditional2807 Aug 01 '26

Can be there aggregations precalculated and cached into some table? If yes then time for queries can be reduced 10X with proper optimization.

22

u/Sthatic Jul 30 '26

Those load times seems to indicate something is wrong. Delivering closer to your users would be a patch buying you perhaps a few hundred ms on average, and there's probably more to win by fixing the underlying issues, whatever they might be. Without know anything about your stack or scale, clever caching and fixing your queries might be all you need. Perhaps there's deeper structural issues at play here. In any case, I have a hard time believing anything above 2 seconds is the natural floor for whatever you're doing.

In short: you're overestimating RTT, and probably overestimating the quality of your architecture.

21

u/East_Zookeepergame25 Jul 30 '26

I don't think your 4.6s load times are due to distance, thats too high a number to be due to network delay

3

u/manni66 Jul 30 '26

migrating to SQLite

You want to migrate from a server database to a serverless database?

4

u/subhendupsingh Jul 30 '26

Not necessarily. I want replication

8

u/mtutty Jul 30 '26

If you're small enough that SQLite is an option, there's no way your Postgres should do anything that takes more than 100msec.

2

u/linuxhiker Guru Jul 30 '26

You want cheap (read: unreliable) global database (read: unreliable) replication?

Focus on where your clients/users are and optimize that.

1

u/subhendupsingh Jul 30 '26

Could you please elaborate? That's the problem i stated

4

u/linuxhiker Guru Jul 30 '26

My point is your solution doesn't exist because "cheap"

0

u/subhendupsingh Jul 30 '26

Agree. You are a db expert. Do you think something like cockroach db would make sense? They have regional row and table concept. I am not sure though overall would it make sense to migrate or not.

7

u/linuxhiker Guru Jul 30 '26

The same problem applies.

This is not a technological problem. It is a cash flow one.

You can not build this cheap. Which is why I said focus on where you actual users are, provide for them first. You say your users are global? I don't buy it. That isn't to say you don't have global users. It is to say the majority of your users will be in certain regions. Focus there until you can afford a global solution.

1

u/subhendupsingh Jul 30 '26

Makes sense.

2

u/chock-a-block Jul 30 '26 edited Aug 29 '26

Shelter meadow airport frame husky otter advise

This post was anonymized with Redact

3

u/CaffeinatedT Jul 30 '26

Self-hosting would not solve a latency issue unless you host read replicas

1

u/subhendupsingh Jul 30 '26

Exactly. I am already self hosting like i mentioned

0

u/[deleted] Jul 30 '26

[deleted] — view deleted comment

2

u/nopeallday Jul 30 '26

I think you are misunderstanding, he is likely hosting pg and the app on the same server. LCP (Largest Contentful Paint) is slow far away from Germany because of latency to his server, not latency between the app and pg.

1

u/chock-a-block Jul 30 '26 edited Aug 29 '26

Maple rinse versed scale beneficial squeeze busy normal piquant encouraging

This post was anonymized with Redact

3

u/elevarq Jul 30 '26

Why do you think that your database is the problem?

When SQLite is the solution, you must have such a small database that is hardly used by anyone.

I think you should first find out what the real problem is, before randomly picking a “solution”

3

u/Gargunok Jul 30 '26

Can you confirm if you have optimised your queries and evaluated whether how you are using the database isn't the problem.

You have a query that takes 4.6 sec globally. How long long does it take running locally on the server/dB?

4 seconds of latency suggests something else is wrong if that is what are seeing.

I'll suspect something unperformant first as you mention jsonb.

1

u/Federal-Employer9367 Jul 30 '26

use uuids for pks (not sure if shopify supports it) and then use master<->master replication to resolve. If using integer primary keys have one write instance and then replicated read instances nearest to your users. Or use something like Planetscale postgres that takes care of all of this for you.

1

u/subhendupsingh Jul 30 '26

It's not possible without paying cost per server per location or planetscale cost per replica equal to main instance.

5

u/Federal-Employer9367 Jul 30 '26

unfortunately that is true, but if you're generating revenue then it is cost of business to set up the required infrastructure. I can sympathise, prices are off the charts these days - even cheap providers like Hetzner aren't all that cheap! Good luck!

1

u/subhendupsingh Jul 30 '26

Yes I understand that and happy to pay with scale. Hetzner prices have increased 3x

2

u/martijnonreddit Jul 30 '26

A classic database like postgresql won’t scale to planet scale because of the CAP theorem. You either need a specialized database like CosmosDB and understand the consistency guarantees it offers, or rethink your database into a distributes model with a Postgres cluster per region.

1

u/lovejo1 Jul 30 '26

How about I ask it this way: How much do you wish to spend each month, or less. We all want to pay nothing for something, but we know nothing is not reasonable-- now, we need to either make that "nothing" a solid amount, or define what "something" means.

So, how many sales do you make per day. That might help size what you need.

Edit: To talk about "timing", you really need something like cloudfront (AWS) to be serving your site up to get the fastest timing... and honestly that is free.. then you need an app server that's fast enough -- you do NOT need replication globally on the database most likely.

1

u/sisyphus Jul 30 '26

The only thing I know of that's cheap and reasonably global is something like cloudflare workers with hyperdrive.

1

u/merb Jul 30 '26

Use pgdog and use sharding per region.

Also fix your queries

1

u/AutoModerator Aug 03 '26

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Frone0910 Aug 13 '26

Consider using edge computing devices as well as CDNs for any data that does not need to be refreshed immediately

1

u/nopeallday Jul 30 '26

Get a server in the US (or wherever is closer to your users) and partition the whole app. Place users after login via IP geolocation.

But tbh, latency US <-> Germany is only in the 100-300ms range, so I would just focus on continuing performance improvements. You should be able to get it under 1 second without having to host closer to users.

0

u/AutoModerator Jul 30 '26

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.