> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pgw.sipmicro.services/llms.txt
> Use this file to discover all available pages before exploring further.

# Tune the host for many devices

> Set the open-file limit, the UDP receive buffer, and the keepalive that pgw depends on when it serves many devices.

A default Linux host runs pgw well for a few thousand devices. Beyond that, pgw usually reaches a limit of the host or the network before it runs out of CPU or memory: the open-file limit, the UDP receive buffer, or an idle timeout on the path to the PBX. Each one has a symptom that you can check for, so change only the setting whose symptom you see.

## Raise the open-file limit

Every app with an open connection takes one open file, and Linux limits how many a process can have. When the limit is reached, new apps can't connect. pgw tells you at startup when the limit is lower than the number of connections that it is set to accept (`app_max_connections`):

```text theme={null}
time=2026-11-16T02:53:19.853+03:00 level=WARN msg="open-file limit below app_max_connections; raise LimitNOFILE or lower app_max_connections" limit=61440 app_max_connections=250000 headroom=1000
```

**1. Set the limit in the unit**

Set the limit in the `[Service]` section of the systemd unit. The unit in [Run pgw under systemd](https://docs.pgw.sipmicro.services/deploy/systemd.md) already has this line.

```ini theme={null}
LimitNOFILE=1048576
```

**2. Restart pgw**

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl restart pgw
```

**3. Check the limit**

```bash theme={null}
grep 'open files' /proc/$(pidof pgw)/limits
```

The log no longer shows the warning.

## Raise the UDP receive buffer

From a PBX that uses UDP, pgw receives every call and every registration answer on one socket. A burst, such as when the PBX restarts and many devices register again, can overflow that socket's buffer, and the kernel silently drops what doesn't fit. The symptom is calls and registrations that time out during busy moments.

A PBX that uses TCP doesn't have this problem, so you can set the upstream's `transport` to `tcp` instead of doing these steps.

**1. Check for drops**

```bash theme={null}
netstat -su | grep -i 'receive buffer errors'
```

A count that grows means that the buffer is too small.

**2. Raise the buffer**

Set the default and the maximum receive buffer. Start with 4 MiB.

```bash theme={null}
printf 'net.core.rmem_default = 4194304\nnet.core.rmem_max = 4194304\n' | sudo tee /etc/sysctl.d/90-pgw.conf
sudo sysctl --system
```

**3. Restart pgw**

pgw's socket gets the new default when pgw starts.

```bash theme={null}
sudo systemctl restart pgw
```

## Keep the path to the PBX open

Firewalls and NAT devices forget a connection that stays silent. Typical limits are 30 seconds for UDP through a NAT, and several minutes for TCP through a cloud firewall. If a firewall or NAT device forgets the path between pgw and the PBX, the PBX's next call doesn't arrive, and nothing in either log says why.

pgw prevents this by itself. It sends each upstream an OPTIONS request every `keepalive_seconds`, 30 by default, which keeps the path open. The answer also feeds the metric `pgw_upstream_up`, so you see when the PBX stops responding. Follow these rules for `keepalive_seconds`:

* Keep the keepalive on when a NAT, a firewall, or a cloud network sits between pgw and the PBX.
* Set `keepalive_seconds` below the shortest idle limit on the path.
* Set it to `0` only when pgw and the PBX share a network without such devices, or when you don't want the PBX to log every OPTIONS.

## Next steps

> [card] [Metrics](https://docs.pgw.sipmicro.services/metrics.md)
>
> Watch open connections and `pgw_upstream_up` to confirm that the limits hold.

> [card] [Troubleshooting](https://docs.pgw.sipmicro.services/troubleshooting.md)
>
> Find the cause when new app connections fail or the PBX stops answering.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.