269 points · by goranmoomin · 4y ago · 113 comments
The upshot is, so long as your postgres + rails app isn't doing some kind of DB write with every request, and doesn't do much - or ideally anything - with Redis, and you want 200ms time-to-first-byte globally (or at least, in multiple locations across the world) then you can easily get better performance with Fly.io than most other solutions.
It's a really clever solution, and one day I'd love to work on a project that fulfils all of those constraints. Haven't found one yet though.
And most Redis commands can be mapped to DDB, I worked on a lib to do that.
https://github.com/dbProjectRED/redimo.go
That gets you global scalability with minimal meaningful changes to your application at all. They even made a Rails specific guide for it here
https://cloud.google.com/blog/topics/developers-practitioner...
If you combined that with say Cloud Native Buildpacks (buildpacks.io) and Cloud Run you now have basically an infinitely scalable solution that happens to hook into all of Google’s infrastructure at just the right points that requires almost zero work on your behalf. Plus you get to keep the one command to deploy that a lot of the Rails community is used to from the Heroku days.
No K8s to configure, no servers to maintain, per second billing on your app server, everything autoscales, very little to learn in the way of new technology. You can complicate things at your own pace as far as the rest of “the cloud” goes but there’s no need to do so unless you want to. It’s a pretty sweet deal in terms of cost to benefit ratios.
By "full", you mean "some"? From [the documentation] on Spanner's PostgreSQL Interface:
> This means that applications written against the PostgreSQL interface can be readily ported to another PostgreSQL environment, obviously without the non-functional benefits, like scalability and availability, that are unique to Cloud Spanner. However, 100% compatibility with PostgreSQL is not the goal.
> The PostgreSQL interface provides a rich subset of the open-source PostgreSQL SQL dialect, including common query syntax, functions, and operators
It seems like existing simple Postgres applications could switch to Spanner, but it does require running a sidecar if your app wants to continue speaking the Postgres protocol on the wire, instead of integrating a Spanner API client.
When it comes to [datatypes], Spanner's PostgreSQL interface is very limited. According to the docs, only these types are supported:
> bool, bytea (byte array), float8 (64-bit IEEE-754 float), int8 (signed 64-bit int), numeric/decimal, timestampz, text.
Notably missing for [my use-case] are UUID, JSON/JSONB, array types like UUID array, and custom enum types. The indexing support is also quite sparse. Full Postgres compatibility, this is not :(
[the documentation]: https://cloud.google.com/spanner/docs/postgresql-interface
[datatypes]: https://cloud.google.com/spanner/docs/reference/postgresql/d...
[my use-case]: https://www.notion.so
Joins, no. Most of the NoSQL systems distribute data by partitioning them on the primary key, and give you scalability by not doing any work across partitions. Which means that traditional joins won’t be supported. You can either join the application, or maintain a secondary schema that has your joined data organised in the access pattern you plan to use.
Google DynamoDB Alex DeBrie or Rick Houlihan for a ton of tutorials on how to restructure data for partitioned/distributed NoSQL systems.
Yeah that's the thing I never understood about Fly.
Globally scaling a stateless web app is very nice to reduce latency but what happens when you have your web apps in US West, US East, EU, India and Australia but Postgres and Redis are sitting in US East.
Anytime you perform a DB / Redis write aren't you back to waiting 70ms-300ms depending on where in the world you are?
Background jobs that flow through Postgres or Redis are super common in a bunch of tech stacks (even Elixir / Phoenix with Oban), and with Rails specifically any type of websocket broadcast goes through Redis too which kind of defeats the purpose of trying to achieve low latency websocket connections since every broadcast goes through Redis in US East while your web servers are around the globe.
Am I missing something important here? Personally I haven't developed a single web app in the last 8 or so years that didn't heavily use Postgres and / or Redis. These are mostly Flask, Rails, Django and Phoenix apps.
I want to like Fly but every time I think about the above I talk myself out of considering it because each web app instance costs money. If you have let's say 6 instances spread across the globe with 1 dedicated CPU + 2 GB of memory each that's $31 x 6 ($186) just for your apps on Fly[0]. If you can't get around high latency for DB / Redis writes and writes are fairly common, it's a hard sell to pay $186 / month for that when you can get a 1 CPU + 2 GB VPS on DigitalOcean[1] for $10 / month and not think about globally distributing your web apps.
I get that the $186 vs $10 isn't a totally fair comparison because with Fly you're getting 6 CPUs and 12 GB of compute while with DO you're getting 1 CPU and 2 GB but with Fly even if your app can happily run with 1 CPU + 2 GB of memory you have to pay for each distributed instance if you want that global reach. With DO you have the option to use that single $10 / month VPS to run your app and forget the idea of global distribution, so if your app fits on that instance size it really is directly and fairly comparing $186 vs $10.
But that's always going to be the case surely with Postgres? If you're running your one DO box config in say US East, every request is going to have that 70-300ms response time for people not in US East region.
With Fly, I think what they're saying is if you are doing writes to DB then you are going to get no optimisation over having one box. However, if you are just doing reads, then you are going to get a lot of optimisation from having it closer to the user.
If you had a simple blog for example, reading blog articles would be fast as it could serve locally, but if you created a blog article it would be 'slower' as you would have to write to the DB. Given for most blogs are 99.99% reading articles you are going to get a significant speedup.
The problem is though that many applications do writes regardless of what the user is doing (analytics/audit logs and that kind of thing).
I think what Fly needs (maybe they have this already) is a specialised message queue which sits in the same region as your servers and doesn't require waiting for global sync. You could then put many write operations through that, and only have to wait for global sync when you need to show something in the UI immediately.
Absolutely, but in DO's case you'd be paying $10 / month instead of $186 on Fly since you're accepting the situation for what it is (you have 1 server in 1 location).
> However, if you are just doing reads, then you are going to get a lot of optimisation from having it closer to the user.
Yes but this also comes at an additional cost I think. Their shared CPU + 2 GB PG cluster is $27.40 a month. Based on their pricing page it's not clear on how this would be priced across multiple regions and read-replicas only but if it's like their app servers then it's multiplied by the number of regions, so if you had 6 of them across the world it would be $164.40, then you have your $186 for your app servers for $350.40 a month and you still have high latency writes. We haven't gotten to Redis costs either. Keep in mind this is for a 1 CPU + 2 GB set up too, the costs get multiplied if you need more compute power.
> The problem is though that many applications do writes regardless of what the user is doing (analytics/audit logs and that kind of thing).
I think there's a lot of cases where you'll have a good amount of writes independent of audit lots. For example a chat system, or leaving comments on anything. There's a lot of scenarios where writes are common and this isn't a problem related to DB scaling. Each one of those writes will cause the user to wait a long time (location dependent) since your controller action will wait for your database to return before it sends an HTTP response back to the user. But I do agree there's tons of opportunities to take advantage of read-replicas for reads.
What Fly really excels at is the dev-ex of deploying apps globally (they don't really compete with VPS providers, that's a different market, imo). There's no mucking around with deployment, orchestration, configuration (and to an extent monitoring) to distribute apps over Fly's infrastructure.
> If you have let's say 6 instances spread across the globe with 1 dedicated CPU + 2 GB of memory each that's $31 x 6 ($186) just for your apps.
Agree. They need to work on their pricing tiers. It is a steep jump from $2/mo with 256M RAM + 0.05vCPU (?) to $31/mo for 1vCPU + 2G RAM.
CDNs have gotten better, and you can write an app in JavaScript to sit in front of your Rails app to make things more dynamic if you want. I believe most devs are better off running their fullstack app where they need it and skipping the additional infrastructure layer.
CDNs are an architectural misfeature that only exist because Fly.io wasn't around 20 years ago. I'm being extreme, but I think that's fundamentally true. ;)
But if we are being honest with ourselves, HN is very much a featherweight unicorn in an ocean of lumbering behemoths. And that's part of the attraction for this particular crowd.
Would be interested in other high-profile rare cases, like HN.
For the 1% of people who do need that global distribution, what Fly.io have built is incredible.
I currently host on Heroku and plan to switch to Fly.io once they have an equivalent to Heroku Postgres's WAL point in time restore.
And by optimizing for few requests and small size, you also get the additional benefit that it will work nicer if the user has a bad internet connection!
My business doesn't "need" global distribution but it could defiantly be argued that it could "benefit from" being so.
I've said it many times but a startup whose elevator pitch is "Heroku, but cheaper at scale" (and I mean exactly the same UX) would be worth billions
They aren't there just yet, but it wasn't far off last time I tried it ~6 months ago.
Running in the right first location is good for users. And Virginia is almost never the right first location.
We move the database data and query compute closer to the end user for consistent 1ms query execution times at scale. It intelligently manages the cache for every individual SQL query so no configuration is needed, unless of course you want to set manual TTL’s.
* A POST request is redirected to the primary data source, the resource is created and the id is returned
* A GET request is immediately issued for that id but the resource that has not been replicated to the regional data source
* :boom:
We talk about this more here: https://fly.io/blog/globally-distributed-postgres/
Like Airbnb taking many seconds to load many parts of their app from the UK, or business apps like Xero which respond tens of times slower than a local app from 30 years ago.
Surely these are the sorts of companies which should be on top of global hosting, now that a hosting firm can box and sell it?
Can someone try it for a medium sized app and send through their monthly bill, that'd be great thanks :).
*Started for me at least. Heroku was how I got started as a Rails developer, and it made it so easy to get a deployment available to interact with from anywhere.
After reading the OP, the tradeoffs seems more complex than a single origin in us-east and having CDNs cache HTML pages.
The most appealing advantage I see with running rails at all these regions is you can authenticate users really quickly and redirect them to a html page in a CDN.
My general takeaway was that HTTP is a terrible interface for "programming" things. Worse, the whole stack was difficult to test and inflexible to iterate on. And the apps I've found myself wanting to build lately rarely serve the same HTML twice.
You may find differently, but I strongly believe "just run your app and DB, no other layers" is the simplest possible way to do most full stack work.
i have to try it now. After reading the free postgres and last mile redis blog post, i realized something...
I can use probably the most under-used cache stores rails provides: the file_store, which is twice as fast as trying to read from a memcache/redis process. This was not even an option on heroku b/c of the ephemeral file system.
Locally I run with docker run -p 8888:8888, so I assume the correct thing to do when deploying on fly is to build the image with EXPOSE 8888 in the docker file, and set the service internal_port = 8888 in fly.toml. Have I assumed this correctly?
If I was to speculate though I think you'd possibly find that you have long tail of potential customers spread outside of the G7 countries.
That's leaving out that there are a lot of very wealthy non-G7 countries.
But even to stick to your question, the G7 spans probably a dozen time zones. Even if you stuck there, you'd still have a pain with a multi-region app and see benefits.
It's getting less non-obvious, I hope.