Costs
Nine places a serverless Laravel app overpays
Serverless bills by the second, so waste hides in wake-ups and settings. The nine checks behind the Savings page in Brewless, and what to do about each.
In this post
A server has a price per month, and you know it before you start. A serverless app is billed for the seconds its containers run and the memory they were given. That is cheap when the app sleeps. It also means that every wake-up nobody needed, and every setting that is roomier than the app, is a small line on the bill that no one decided on.
Those lines are hard to see. The invoice says what a container cost, not why it ran. This post goes through the nine places where a serverless Laravel app on Scaleway and bunny.net pays for something it may not need. They are the nine checks behind Savings, a page that came to the Brewless console this week.
Where does a serverless Laravel app waste money?
In three kinds of places: containers that hardly sleep, sizes and minimums that are larger than the app needs, and resources that are billed whether anybody uses them or not. The first kind is about your application's behaviour, the second about a few lines in brewless.yml, and the third about things you forgot were there.
| Kind | Finding |
|---|---|
| Not sleeping | A worker that is up nearly always |
| Not sleeping | A web container that hardly sleeps |
| Not sleeping | The edge cache switched off |
| Sized too large | A container kept warm |
| Sized too large | A roomy container |
| Sized too large | A database minimum above one vCPU |
| Billed regardless | A database awake most of the time |
| Billed regardless | A standby copy outside production |
| Billed regardless | An environment nothing was released to |
Containers that do not sleep
The worker that is always up. A worker should be called when there is work. One that is up more than three quarters of the time is being asked, over and over, whether there is any. There are two usual causes. Jobs that wait in the database have to be looked for, so the worker runs between two jobs and finds nothing. With a queue at the provider (queue: sqs when you set an environment up) the queue calls the worker when a job arrives, and it sleeps in between; Laravel queues without a long-running worker explains how. The other cause is the schedule. A task that runs every minute wakes the worker every minute. Every task that may run hourly instead takes 59 wake-ups an hour away. And an application with no scheduled tasks at all can say so:
environments:
staging:
kind: staging
scheduler: false
With the schedule off and no queue to look in, nothing calls the worker at all.
The web container that hardly sleeps. Scaleway lets a container sleep only after 15 minutes without a request, so one request every quarter of an hour is enough to keep it running all day. The usual causes are a public page that starts a session or prints a CSRF token, which the edge may then not keep, a monitor that asks every minute, and a crawler on an address outside the edge. Why your Laravel container never scales to zero covers these, and php artisan brewless:sleep names the rule each page breaks.
The edge cache switched off. With edge.cache: 0 in brewless.yml, bunny.net keeps no page and every visit reaches the container itself. With the cache on, the edge answers a GET without cookies itself for an hour, and every release empties it. A page that depends on the visitor says so with a Cache-Control: private of its own, which is a better fix than switching the cache off for all of them.
Sizes and minimums nobody needed
A container kept warm. web.min: 1 keeps a replica running every hour of the month, whether or not anybody asks it anything. That can be the right choice: a cold start is something the first visitor waits for. But it is a choice for production, and it tends to get copied into staging with the rest of the file.
A roomy container. Memory is one of the two things a Serverless Container is billed for, by the second. A container with 2 GB or more that never needed a second replica may well fit in less. Only the application can say whether it does, so measure before you lower web.memory or worker.memory: the peak of a normal request and of your heaviest job, with memory_get_peak_usage(true).
A database minimum above one vCPU. A Serverless SQL Database scales between a minimum and a maximum. While it is awake it is billed for the minimum whether it uses it or not. A higher minimum is worth it only where a query may not wait for the database to scale up. For one heavy job or one busy morning, a planned boost lifts the database for as long as that lasts instead of all month.
Things that are billed whether you use them or not
A database awake most of the time. A serverless database is billed for every second it is awake, at a higher price per hour than a small managed node. When it is awake most of the month, the smallest managed node would have cost less over the same hours, and it is simply always on. Serverless SQL or Managed PostgreSQL shows where the line falls. Before you move, look at what keeps it awake: often it is the same every-minute schedule that keeps the worker up.
A standby copy outside production. A standby in another zone is a second instance, and Scaleway bills it as one. It earns its keep on production, where a zone that fails may not cost you the database. On staging a backup is usually enough.
An environment nothing was released to. A staging environment that had no release for two months still holds its resources. Its containers may sleep and cost nothing, but a database, a bucket and a pull zone do not sleep. Keep it if you still use it. If not, brewless detach takes it apart, and asks you to confirm every resource before it removes one.
Measured, or worth checking
Not every finding is equally certain, and the page says which is which.
A finding is measured when the amount follows from the hours Brewless recorded: the worker that ran, the database that would have been cheaper as a managed node, the minimum that was billed while the database was awake. It is worth checking when the amount also depends on something that cannot be seen from outside your application, such as whether it fits in less memory. What was looked at and came out fine is listed apart, under Also looked at, so you can see that it was looked at.
Note
The amounts are estimates from Scaleway's list prices, before tax, and the page names the date those prices were read. They are not lines from your invoice. The Costs page shows what Scaleway and bunny.net actually charged.
The page costs nothing to open. Brewless already records, per hour, how long every container ran and how long it was scaled to zero, and the same for a serverless database. Savings reads the last 30 days of those hours, next to the settings your last deploy applied. No provider is asked and no container is woken. A new application has to wait about a day before the checks can say anything.
Hand a finding to your coding agent
Some findings cannot be settled from outside. Why a web container is woken, which scheduled tasks really have to run every minute, what makes a job heavy: the answers are in your code.
Each of those findings has a Copy prompt button. It copies a task for a coding agent with the environment's own figures written into it: how much of the time the container slept, how many hours it ran, how much memory it has. The prompt for a container that will not sleep, for example, asks the agent to go through the public GET routes and say per route whether a visitor without a cookie gets a session, a CSRF token or a Set-Cookie header. Copy every tip as one prompt does the same for all of them at once.
The prompts ask for a report, not for changes. Each one ends with "change nothing until I say so", so you read what the agent found before anything is touched.
Tip
An application's own page in the console shows the same findings for that application alone. Savings sits behind the same permission as Costs, so a member who may not see Costs does not see it either.
Summary
- Look first at what does not sleep: the worker, the web container, the database. An hour that ran is an hour that is billed.
- Then at the settings:
min,memory,database.min_vcpu,database.standbyandedge.cache. Keep the production choices on production. - Then at what is left standing: an environment nobody releases to still has a database, a bucket and a pull zone.
- Treat a measured amount as an estimate from list prices, and check a "worth checking" one in your own application before you change a size.
Saving costs in the documentation has every check and what each prompt looks into.
Frequently asked questions
Why is my serverless Laravel bill higher than I expected?
Usually because something does not sleep. A web container woken by crawlers or session cookies, a worker called every minute, or a database that is awake all month is billed for nearly every hour, and then serverless costs what a server costs.
Does opening the Savings page wake my containers?
No. The page is drawn from the hours Brewless recorded of every container and database, and from the settings of your last deploy. No provider is asked and no container is woken.
Are the amounts on the Savings page what I will save on my invoice?
They are estimates from Scaleway's list prices, before tax, and the page names the date those prices were read. What the providers actually charged is on the Costs page.
Does Brewless change my settings when it finds a saving?
No. Nothing on the page changes an environment. Each finding names the key in brewless.yml or the page in the console where you make the change yourself.
How long before the Savings page shows anything for a new application?
About a day. Brewless counts the hours per hour and needs roughly 24 of them before a check can say anything. From then on it reads the last 30 days.