What is serverless ?
Serveless is a service where you pay for how much compute power actually use, 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 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 load - cloud provider will scale your infrastructure to traffic.
I decided to go all in on serverless on my pet project. TLDR on project it’s basically a CRUD, nothing special. I figured it would be a perfect fit to try migration, because it is very low traffic website. My cloud of choice is GCP, so I picked Cloud Run to host Angular SSR website.
Cloud functions to run Nestjs Code and Cloud Firestore as a serverless NoSQL database. The biggest problem at the time I thought it will be a database migration, because I was running initially MySQL. But migrating to NoSQL from SQL database is 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 principles 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 frontend, put it in docker container and called it day.
For backend code, I’ve put in Cloud Function (to 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 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 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 then 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 app utilises real time features.
- Cold start. Requires some wait time to a container to boot up on first request after a period of time.
- Bugs can cost big time. I had a bug where it caused endless loop. So without realising I had about 1000 app instances running in parallel. As I said it can get expensive pretty quick!
All in all serverless seems like a good solution for any kind of application if used correctly. Cold start times can cause poor UX, but there is a way to battle this - for example ping app every X seconds to boot it or just have one instance always running.