I created https://websu.io an open source webpage speed monitoring tool and a key feature was the ability to test your webpage speed from locations across the world. This post will describe the architecture of Websu and how that allowed me to easily deploy it to 25 locations across the world for $3.73/yr
The easiest thing to do would be to spin up a server in each location across the world and when user requests a speed test you run it on the server. That however comes with a large downside, you will have to pay for this server even when users aren't submitting any requests to that specific location. If I had done this, this would have cost me 25 * ~$5/month = $1500 / year even when I didn't have any users. This cost, being purely a hobby project for me wouldn't have been feasible. Note I don't like spending money, I am Dutch after all, there is a reason there is a saying "going Dutch" when you split up your bill.
So what comes to the mind of a scrappy Dutchmen? Serverless with Cloud Run! I split up Websu into 3 components to make it possible to run from any location:
- websu-ui: React/Nextjs front-end that consumes a REST API. OSS version here: https://github.com/websu-io/websu-ui
- websu-api: The API front-end that takes in the end-user requests. Source repo: https://github.com/websu-io/websu note: this project was a result of me trying to learn code, excuse the bad code.
- lighthouse-server: GRPC server that takes in lighthouse requests and responds with the raw lighthouse json output. Source repo: https://github.com/websu-io/websu/tree/master/pkg/lighthouse
The websu-ui and websu-api are deployed to a single region in Cloud Run however the lighthouse-server is deployed to all regions available with Cloud Run. This way, cloud run will run 0 lighthouse-server instances in any of the 25 regions so I won't get billed. Only when an end-user tries to test the page speed in a specific location e.g. Japan, then it will spin up a cloud run instance automatically. The cold start delay has been very minimal for me and definitely worth the savings. You can see a screenshot of my Cloud Run and Artifact Registry costs here:
To make my life easier, I also wrote a simple python script to manage the deployment of new lighthouse-servers: https://github.com/websu-io/websu/blob/master/scripts/deploy-lighthouse-servers.py
During this time I was able to serve 3.1k users (see analytics screenshot below) across 25 locations for $3.73/yr instead of $1500/yr. I hope this posts helps people give insight to new use cases for serverless.
The big spike was due to Korben.info mentioning Websu: https://korben.info/script-mesurer-performances-site-web.html and I was happy that Cloud Run could easily sustain that.
Note/Disclaimer: I'm excluding the cost of a VM that's running Mongo, which is costing me about $10 / month. The reason for that is because with or without Cloud Run I would have incurred that cost. Opinions are my own and not the views of my employer
Top comments (1)
@samos123 How do you protect your public cloud run urls from abuse? Or from development errors where if we accidentally call the url infinitely?