What is serverless?
Serverless is a service where you pay for how much computing power is used, not how much you reserve.
Migrate reasons can be several: from security, to cost savings and scalability. By scalability I mean you don’t need to worry about the underlying infrastructure, it will scale up and down depending on the load. If no load there will be no active containers/functions/lambdas ready to handle requests, also there will no billing for that period. If there's a load - the cloud provider will scale your infrastructure to traffic.
I decided to go all in on serverless on my pet project. I figured it would be a perfect fit to try migration because it is a very low-traffic website. My cloud of choice is Google Cloud Platform, so I picked Cloud Run to host the Angular SSR website.
Cloud functions to run NestJS Code and Cloud Firestore as a serverless NoSQL database. I thought the biggest problem at the time would be a database migration because I was initially running MySQL. But migrating to NoSQL from SQL database was straightforward. I inserted responses of the API to FireStore. I didn’t split anything, I just put the whole document with preloaded relationships to avoid additional querying. This is one of the benefits of NoSQL. If data is queried together it should belong in one document. Firestore supports 1MB of data per document, so it is fine for my use case.
I updated the frontend, put it in the docker container, and called it day.
For backend code, I’ve put in Cloud Function (too spicy it up), for this no Dockerfile is needed, everything is packaged up behind the scenes. On the other hand, CloudRun is a managed container runtime, so Dockerfile is required. Deployment from the command line straight forward for both services. It was easier than expected, but that was probably because I know how to navigate GCP. For non-GCP users, it should take a little bit more time, but UI is way better than AWS's.
At the time of writing it was a couple of months ago. I was spending around 20Eur on my GCP bill on this specific project. Now I spend less than 1Eur.
There are some caveats you should know:
- Unexpected charges if you don’t set billing monitoring right (increased load).
- Vendor lock-in could be hard to migrate out of managed service. For example, Firestore could be a challenge to replace if the app utilizes real-time features.
- Cold start. Requires some wait time for a container to boot up on the first request after a while.
- Bugs can cost big time. I had a bug where it caused an endless loop. So without realizing, I had about 1000 app instances running in parallel. As I said it can get expensive pretty quickly!
If used correctly, serverless seems like a good solution for any kind of application. Cold start times can cause poor UX, but there is a way to battle this - for example, ping the app every X seconds to boot it or just have one instance always running.