# Serverless SQL or Managed PostgreSQL for Laravel

Scaleway has a PostgreSQL database that sleeps and one that is always on. What wakes the first, how a Laravel app connects to each, and how to choose.

## In short

- A Serverless SQL Database bills compute only while it is awake, and a Managed Database bills its node every hour of the month.
- Which one is cheaper depends on one figure, the share of the month the database is awake.
- A Serverless SQL Database falls asleep five minutes after its last query, so anything that queries it every minute keeps it awake all month.
- A Laravel scheduler or a database queue that runs every minute is the usual reason a serverless database never sleeps.
- Serverless SQL Database is only in Paris so far, so an app in Amsterdam, Warsaw or Milan uses a Managed Database.

Scaleway offers PostgreSQL in two forms, and they are priced in opposite ways. One sleeps when nobody asks it anything and costs nothing for compute while it does. The other runs all month on a node you pay for by the hour. For a Laravel app, the right choice has less to do with how big the app is than with how often something touches the database.

This post explains the difference, what keeps a serverless database awake in a Laravel app, how the app connects to each, and how [Brewless](/) helps you see which one is cheaper. It does not quote prices: the console reads Scaleway's list prices and shows the date it read them.

## Serverless SQL Database or Managed PostgreSQL: which one should a Laravel app use?

Use a Serverless SQL Database when the database is asleep for a good part of the month, and a Managed Database when it is awake most of the time. A serverless database bills compute only while it is up, at a higher price per hour; a managed node bills every hour whether it is used or not. Somewhere in between is a share of the month above which the managed node wins, and the Brewless console shows where that is for your database.

| | Serverless SQL Database | Managed Database |
| --- | --- | --- |
| You pay for | Compute while awake, plus storage | The node every hour, plus storage |
| Sleeps | Five minutes after its last query | Never |
| Engines | PostgreSQL | PostgreSQL or MySQL |
| Regions | Paris only, so far | Not limited to Paris |
| Private Network | Cannot join one | Can have a private endpoint |
| Made by Brewless | Yes | No: you name one that exists |

## What keeps a serverless database awake

A Serverless SQL Database falls asleep five minutes after its last query. One query every few minutes, all day, and it never sleeps. In a Laravel app on serverless containers, the usual causes are these.

**Jobs waiting in the database.** With `QUEUE_CONNECTION=database`, the jobs have to be looked for, so Brewless has a trigger call the worker every minute: one tick of the schedule, then the queue. That keeps the database awake all day. With a queue at Scaleway Queues instead, the queue calls the worker as soon as a job is put on it, and crons call the scheduler only at the moments your schedule has work. Nothing calls the worker in between, so the database can sleep.

```yaml brewless.yml
environments:
  staging:
    region: fr-par
    database: shop-staging
    queue: sqs
```

**A schedule that runs every minute.** A task scheduled `everyMinute()` gets the scheduler called every minute. So does a task in a time zone or form a cron cannot copy. If the task queries the database, the database stays up. Run it less often, or switch the schedule off for an environment that does not need it with `scheduler: false`.

**Visitors and monitoring.** A real visitor every few minutes keeps it up, and should. An uptime monitor that loads a page backed by the database does the same, around the clock. Point such a monitor at a route that does not query the database. Brewless itself never sends requests to your app to measure it.

**Sessions and cache in the database.** With `SESSION_DRIVER=database` and `CACHE_STORE=database`, every request touches the database, even a page that reads nothing else from it. That only matters while there are visitors, so it rarely decides the choice. A Redis cache takes that load off the database, but it does not sleep itself.

> [!WARNING]
> A scheduler or a queue worker that runs every minute keeps a serverless database awake all month, also when there are no visitors. For such an environment a managed node is almost always the cheaper one.

## How Laravel connects to each

**Serverless SQL Database.** Brewless makes it, and the app signs in as an identity of its own, an IAM application that may use the databases of the project. The variables get `DB_CONNECTION`, `DB_HOST`, `DB_PORT`, `DB_DATABASE`, `DB_USERNAME`, `DB_PASSWORD` and `DB_SSLMODE=require`. It is reached over the internet, with that key, and cannot join a Private Network. Making the identity needs the permission set `IAMManager` on Brewless's key; without it, the result names `DB_USERNAME` and `DB_PASSWORD` for you to add.

**Managed Database.** Brewless does not make one. You make it in your Scaleway project, and name it under `database` in `brewless.yml` or choose it in **Set up**. Brewless reads it and uses it as it is. Its databases and users are of your own making, so you add `DB_DATABASE`, `DB_USERNAME` and `DB_PASSWORD` before the first deploy. When the instance has a public endpoint, that one is used. With `network: private`, the environment joins the project's Private Network, the database gets a private endpoint there, and `DB_HOST` and `DB_PORT` move to it unless you set them yourself.

```yaml brewless.yml
environments:
  production:
    region: nl-ams
    database: shop-main
    network: private
```

Two environments can share one database. Linking another environment gives it a login of its own: on a Managed Database a logical database and user such as `blog_staging`; on a Serverless SQL Database, which holds one database, an identity of its own that shares the tables. See [Databases](https://docs.brewless.eu/running/databases#environments-and-sharing).

## Regions

Scaleway does not offer every product in every region. Serverless SQL Database is only in Paris so far. In Amsterdam, Warsaw and Milan, setting an environment up offers a database that exists already, or none, and a choice that cannot be made in the region is refused before anything is made.

| Region | Code | Serverless SQL Database |
| --- | --- | --- |
| Paris | `fr-par` | Yes |
| Amsterdam | `nl-ams` | No |
| Warsaw | `pl-waw` | No |
| Milan | `it-mil` | No |

## See what your database actually does

You do not have to guess the awake share. The **Databases** page of the console lists every database of your organisation with how long it was up, how long it slept, how often it woke, how far it scaled, its size and its cost over a period you choose.

The figures come from Scaleway. Scaleway reports, minute by minute, how many vCPUs it gives a serverless database, and nothing for a minute in which it is scaled to zero. Once an hour Brewless counts that into totals per hour and keeps those, never the samples. Nothing is asked of the database, so looking never wakes it.

When a serverless database was up 90% or more of the last 24 hours, the page says so, with what it costs a month at that pace and what the smallest Managed PostgreSQL node would cost with the same storage.

For each database of an environment, the console also shows what it costs as it is and what the other kind would cost, which is cheaper and by how much, and the share of the month above which the managed node wins. You can move that share to see what a change would do, such as running the scheduler less often. Until hours are recorded, the share is estimated from the minutes a container of the environment was up, plus five minutes after each. See [Database costs](https://docs.brewless.eu/running/database-costs).

> [!NOTE]
> Brewless only advises. It never moves a database from one kind to the other, and a deploy that names another database for an environment that has one is refused, because a database holds data.

## How to choose

- **Staging, demos, preview environments, internal tools used a few hours a day:** Serverless SQL Database, with jobs at Scaleway Queues and no every-minute tasks.
- **A production app with steady traffic around the clock:** a Managed Database. It is awake anyway, and the node is cheaper per hour.
- **An app with a scheduler or worker that must run every minute:** a Managed Database.
- **An app outside Paris:** a Managed Database, since Serverless SQL is not offered there yet.
- **An app with a database per customer:** Serverless SQL, where each customer's database sleeps on its own. See [A database per tenant for Laravel on serverless](/blog/laravel-database-per-tenant-serverless).

Start with what the app does at night. If nothing should happen, a serverless database lets it be quiet; if something always happens, pay for a node and stop counting.

## Frequently asked questions

### When does a Scaleway Serverless SQL Database fall asleep?

Five minutes after its last query. While it sleeps you pay for its storage only. The next query wakes it again.

### What keeps a serverless database awake in a Laravel app?

Anything that queries it at least every five minutes. With Brewless that is usually jobs waiting in the database, because the worker is then called every minute to look for them. Visitors, monitoring that loads a page, and a scheduler that is called every minute do the same.

### In which Scaleway regions is Serverless SQL Database available?

Only in Paris (fr-par) so far. In Amsterdam, Warsaw and Milan an environment uses a Managed Database that already exists in your Scaleway project, or none.

### Does Brewless move my database to the cheaper kind?

No. The console shows what each kind would cost and which is cheaper, but Brewless never moves a database from one kind to the other. A deploy that names another database for an environment that has one is refused.

### Does Brewless ping my database to measure it?

No. For a serverless database it reads the vCPU figures Scaleway reports minute by minute, once an hour. Nothing is asked of the database, so looking at the figures never wakes it.

Source: https://brewless.eu/blog/serverless-sql-or-managed-postgresql
