Peak load: a whole fleet sends at the same second, the trucks wait in a parking lot, and a small database receives batched writes over one or two connections.

Device data is small. A reading of a few hundred bytes per minute from a hundred devices fits on the smallest database for years. What runs out first is connections: Postgres and its poolers accept a fixed number of clients, and a website, an API and an ingest path usually share them. This article lists the setups that make a burst of device reports exhaust that limit, the errors they produce, and the fixes in the order of effort. It is the third article of the series “Production details”, after certificate expiry and sleeping devices.

Why does a burst of small reports exhaust a database?

A burst of small reports exhausts a database because the limits count connections, not bytes. Every client that writes holds a connection while it does, and in a pooler’s session mode it holds it for the whole session. When many senders write in the same second, many connections are open in the same second. Postgres refuses connections beyond max_connections (PostgreSQL, connection settings). On Supabase, the pooler in session mode admits only as many clients as its pool size, 15 by default on Nano to Medium compute (Supabase, pooling and limits).

Device traffic makes this worse because it is bursty by nature. Devices on a common reporting interval send in the same second, and after a Wi-Fi or server outage every device reconnects and flushes its backlog at once: a reconnect storm.

Which errors show that connections ran out?

These errors show that a connection limit was reached:

ErrorWhere it comes fromWhat it means
FATAL: sorry, too many clients alreadyPostgres, SQLSTATE 53300max_connections is reached (PostgreSQL error codes)
remaining connection slots are reserved for ...PostgresOnly reserved superuser slots are left
(EMAXCONNSESSION) ... max clients are limited to pool_size: 15Supabase pooler, session modeThe session-mode client limit equals the pool size
Timeouts acquiring a connectionThe app’s own poolThe app pool is exhausted and requests queue

The session-mode error is common well beyond IoT: data export tools and internal-tool builders hit the same limit of 15 (Retool community).

Which setups cause it?

Five setups cause most of these failures:

  1. Pools that add up to more than the limit. Each app instance opens its own pool. Three instances with a pool of 10 each need 30 connections, twice the default session-mode limit of 15.
  2. Serverless functions on session mode. Every function instance opens its own connection, and session mode keeps it for the whole session. Supabase directs serverless clients to transaction mode for exactly this reason (Supabase, connecting to Postgres).
  3. Idle sessions. Connections that are opened per request and never closed, or pools without an idle timeout, keep slots occupied without doing work.
  4. Devices with database credentials. A device that connects to Postgres, or calls the database API with a key in its firmware, turns every device into a database client. The number of clients then grows with the fleet.
  5. One role for everything. The website, the API and the ingest path use the same database user through the same pooler, so they share one pool. Whichever bursts first takes the connections of the others.

What are the fixes, from cheapest to most robust?

Five fixes solve it, here in the order of effort:

  1. Size every pool against the limit. Add up the pool sizes of all instances, keep the total below the database limit with room for migrations and admin tools, and set idle timeouts.
  2. Use the right connection type. For persistent backends such as VMs and long-running containers, Supabase recommends the direct connection with an application pool. Serverless functions and many short-lived clients use the pooler’s transaction mode on port 6543, and session mode stays the IPv4 fallback for persistent clients. The direct connection is IPv6 unless the project has the IPv4 add-on, and the shared pooler does not support prepared statements in transaction mode (Supabase, connecting to Postgres).
  3. Give ingest its own role. The Supabase pooler keeps a separate pool for every combination of user, database and mode (Supabase, Supavisor FAQ). An ingest role gets its own pool, so an ingest burst cannot take the website’s connections. This is the bulkhead pattern (Azure Architecture Center, bulkhead).
  4. Buy a bigger compute. It raises every limit, at a price that follows connections instead of data (Supabase, compute and disk):
ComputeMax pooler clientsMax direct connectionsDefault pool sizePrice per month
Micro2006015about USD 10
Small4009015about USD 15
Medium60012015about USD 60
Large80016020about USD 110
XL1,00024020about USD 210
  1. Put a buffer in front of the database. Devices write to an ingest endpoint, a queue absorbs the burst, and a forwarder writes to the database in batches over one or two connections. This is the queue-based load leveling pattern (Azure Architecture Center). The AWS IoT Lens of the Well-Architected Framework recommends the same for device data and queues it before it reaches compute and database services (AWS IoT Lens).

Fixes 1 to 3 are configuration, and they hold as long as the number of writers is fixed. Fix 5 is the one that keeps holding when the fleet doubles, because database connection use no longer depends on the number of devices or the size of a burst.

Why does the buffer belong in the design from the first device?

The buffer belongs in the design from the first device because the device side depends on it. Devices that post to an ingest endpoint never need database credentials, and the endpoint can change behind them. Devices that were built against the database have to be updated in the field to move. On the device, the ingest path is a plain HTTPS post to an address that is not your database:

#include <string.h>#include "esp_http_client.h"esp_err_t post_reading(const char *json){    esp_http_client_config_t cfg = {        .url = "https://ingest.example.com/v1/readings",   /* not the database */        .method = HTTP_METHOD_POST,        .timeout_ms = 10000,        /* client certificate of this device for authentication */    };    esp_http_client_handle_t c = esp_http_client_init(&cfg);    esp_http_client_set_header(c, "Content-Type", "application/json");    esp_http_client_set_post_field(c, json, strlen(json));    esp_err_t err = esp_http_client_perform(c);    esp_http_client_cleanup(c);    return err;   /* on failure: retry later with backoff and jitter */}

Behind the endpoint, the forwarder writes one statement per batch. Each reading carries a unique id from the device, so a retried batch never creates duplicates (PostgreSQL, INSERT ON CONFLICT):

insert into readings (event_id, device_id, ts, co2_ppm, fw)select event_id, device_id, ts, co2_ppm, fwfrom jsonb_to_recordset($1::jsonb)  as r(event_id uuid, device_id text, ts timestamptz, co2_ppm int, fw text)on conflict (event_id) do nothing;

Sources

Frequently asked questions

What does 'max clients are limited to pool_size' mean on Supabase?

It means the Supabase pooler in session mode is full. In session mode every client keeps one database connection for its whole session, and the pooler admits only as many clients as the pool size, 15 by default on Nano to Medium compute. The next client is queued for up to a minute and then refused.

What does 'sorry, too many clients already' mean in Postgres?

It is the Postgres error 53300 (too_many_connections): the server has reached max_connections and refuses new connections. On Supabase the direct-connection limit is 60 on Micro and 90 on Small, and Supabase’s own services use part of it.

Which Supabase connection type should a device backend use?

A long-running backend on IPv6, or with the IPv4 add-on, should use the direct connection with a pool in the application. Serverless functions should use the pooler in transaction mode on port 6543, with prepared statements turned off. Session mode on port 5432 is the fallback for persistent clients on IPv4-only networks.

Does a separate database role help?

Yes. Supabase’s pooler keeps one pool per combination of user, database and mode, so an ingest service with its own role gets its own pool and cannot take the connections of the website. This is the bulkhead pattern: separate pools for separate workloads.

Does a bigger compute fix it?

It raises the limits, from 200 pooler clients on Micro to 1,000 on XL, but the price then follows the number of connected clients instead of the data. A buffer in front of the database keeps connection use constant, so the compute can stay sized for the data.

How do I see who holds the connections?

Query pg_stat_activity grouped by usename, application_name, client_addr and state. Many rows in state idle from one application usually show an oversized pool or sessions that are never closed.