# Best practices

Source : https://webgune.cloud/en-us/docs/bonnes-pratiques · Mis à jour le 2026-11-20

# Best practices

Ten points to check before going to production. Each one matches an incident we see regularly, with what to do to avoid it.

## 1. Lock file up to date and committed

The build installs exactly what the repository's lock file describes: `package-lock.json`, `bun.lock`, `pnpm-lock.yaml` or `yarn.lock`. If it is missing or behind `package.json`, the build fails. After every dependency you add, commit the lock file with the `package.json`.

```bash
npm install
git add package.json package-lock.json
git commit -m "Add the dependency"
```

Use a single package manager per repository: a `package-lock.json` and a `yarn.lock` side by side cause confusion ([How builds work](/docs/builds-railpack)).

## 2. Listen on `PORT` and `0.0.0.0`, once ready

Webgune considers an instance ready as soon as its port accepts a connection, and sends traffic to it then. Three rules follow:

- read the port from the `PORT` variable and declare the same one in the service;
- listen on `0.0.0.0`, not on `localhost` or `127.0.0.1`, otherwise nobody can reach you;
- connect to the database and load the configuration before calling `listen`.

```js
const port = Number(process.env.PORT) || 3000;
await db.connect();
app.listen(port, "0.0.0.0");
```

Also add a `/healthz` route that answers `200` only when the application works, database included. It serves your probes and your diagnosis ([Health check](/docs/controle-de-sante)).

## 3. Migrations compatible with the previous version

During an update, the old and the new version run at the same time for a while. After a rollback, the old version runs alone on the migrated schema. Add a column rather than rename one, and remove the old one in a later version. If the application migrates at startup, only one process must migrate at a time ([Database migrations](/docs/migrations-base-de-donnees)).

## 4. No important data on the working disk

An instance's disk is wiped on every restart and every update. Files uploaded by your users go in a [persistent volume](/docs/volumes-persistants) or object storage, data goes in a [database](/docs/bases-de-donnees).

A volume brings the service back to a single instance and runs it as a standard container rather than as a microVM. For a site that must handle load, prefer S3-compatible object storage.

## 5. Valid JSON variables, secrets out of the repository

A variable that contains invalid JSON prevents the service from starting.

```text
correct    {"webgune-core": "valeur"}
incorrect  {webgune-core: valeur}
```

No secret goes in the repository, `.env` file included. Build variables are used only for the build and do not enter the image: do not put there what the application needs while running ([Environment variables](/docs/variables-environnement)).

## 6. An application that can run twice at once

A site or an API in ultra high availability moves to several instances when the load rises, and two requests from the same visitor can reach two different instances.

- Shared sessions and caches go in [Redis](/docs/db-redis) or in the database.
- Scheduled tasks go in a [worker](/docs/workers), otherwise they run once per instance.
- WebSockets broadcast their messages between instances through Redis pub/sub.

If the application cannot be adapted, switch it to **Simple service** ([Availability and scaling](/docs/autoscaling)).

## 7. Internal addresses between services in the project

Between a service and its database, or between a frontend and its API, use the internal address shown in the **Network** panel. Traffic stays in the project and does not depend on the public entry point. Size the connection pool by multiplying by the number of instances: 4 instances with a pool of 20 open 80 connections, beyond the 50 allowed by a managed database ([Databases](/docs/bases-de-donnees)).

## 8. Graceful shutdown

On every update, the old instance receives `SIGTERM`. It must stop accepting new requests, finish the ones in progress, close its connections and exit. It has 30 seconds with the default strategy, after which it is stopped by force.

```js
process.on("SIGTERM", () => {
  server.close(async () => {
    await db.end();
    process.exit(0);
  });
});
```

```python
import signal, sys

def stop(signum, frame):
    # finish the current work, close the connections
    sys.exit(0)

signal.signal(signal.SIGTERM, stop)
```

In a Dockerfile, start the application with the form `CMD ["node", "server.js"]`. With `CMD node server.js`, a shell sits in between and the signal does not reach your program.

## 9. Logs on standard output

Write with `console.log`, `print` or the standard output of your logging library. These lines appear in the **Logs** tab. A log file on disk disappears on restart and is visible nowhere ([Logs and monitoring](/docs/logs-monitoring)).

## 10. Backups and a protected account

Schedule a backup of each database and test a restore at least once ([Backups and restore](/docs/sauvegardes)). Turn on two-factor authentication on the account and give each API key the minimum rights ([Account security](/docs/securite-compte)).

## Before going to production

| Check | Done |
|--------------|:----:|
| Lock file committed and up to date | ☐ |
| Listening on `PORT` and `0.0.0.0` | ☐ |
| Initialization finished before the port opens | ☐ |
| `/healthz` route | ☐ |
| `SIGTERM` handled | ☐ |
| Migrations compatible with the previous version | ☐ |
| No file to keep outside a volume or object storage | ☐ |
| Secrets in variables, none in the repository | ☐ |
| Sessions and scheduled tasks outside the web process | ☐ |
| Internal address and reasonable pool for the database | ☐ |
| Scheduled database backup | ☐ |
| Availability mode chosen | ☐ |
| Two-factor authentication turned on | ☐ |
