Serverless hosting explained
Published at Dec 8, 2024
Serverless hosting is a pay-as-you-go solution where the framework of your website has broken down functions in your websites routes into separate serverless functions. This lets you only pay for the actual usage, if noone visits certain parts of your website - then you dont pay for that either. Additionally these serverless functions can scale up, so if you have many users on certain routes then the provider supplies more serverless functions for you so you can handle all the requests.
This splintering of functions in one route can make estimating costs for a website harder. If you have a particular route, lets say “/dashboard”, then you would maybe have a function to check if the user is already logged in, maybe another to fetch the users data, a second to fetch the first part of the dashboard information, a third to get notifications, a fourth to fetch an array of some data, and so on.
This simple example would have at least 5 function calls for one route.
Additionally actions you perform on the page would also map to functions, want to sort a table on a different column? That could mean 3-4-5 functions ran. Submitting a form could have the same, first submitting the formData to the backend, checking that you are logged in and have access, maybe get some data from the database, making a notification, sending an email and so on.
Function runtimes are also a factor for estimating costs
The hosting provider doesnt only care about the number of times functions are called and ran, but also for how long each function runs for. The longer, the more expensive.
Some functions calls may be fast, sub 50 milliseconds for checking a quick status. Or longer depending on the logic it has to run through, and maybe you have to do 2-3 calls to a database or some other APIs. Suddenly some function calls can take 300 ms. And usually more if it has to call the database.
How to cut costs for serverless functions
Caching function results can have major saving potential, the cheapest work is the work you dont have to do.
If you have certain functions you know will run very often, maybe you can cache the results and for every time this function is supposed to run, you check if you already have the response saved in the cache.
This saves function calls, runtime and latency for your users, making your website faster to use!