FAQ
Frequently asked questions about deploying and running applications on Phemeral.
How should I handle database connections on an app deployed to Phemeral?
Database connections should not be expected to persist across invocations. Phemeral environments may recycle the underlying process between requests, which means idle connections in a pool can become stale or closed.
Use your provider's connection pooler if available
If your database provider already offers a connection pooler, you don't need to manage connection pooling in your application. For example, Phemeral's managed Postgres offering provides a pooled connection URI by default, so your app code can connect directly without configuring its own pool. A direct connection URI is also available when you need to bypass the pooler.
Check your provider's documentation to see if a managed pooler is available. If it is, prefer that over application-level pooling.
Application-level pooling
If you do manage your own connection pool, configure it to validate or recycle connections proactively.
SQLAlchemy
Set pool_pre_ping to validate and re-establish connections before executing a query:
from sqlalchemy import create_engine
engine = create_engine(
"postgresql+psycopg2://user:pass@host/db",
pool_pre_ping=True,
)This issues a lightweight check (e.g. SELECT 1) on each connection checkout, automatically replacing connections that the server has closed.
asyncpg
Set max_inactive_connection_lifetime to a low value (such as 1 second) so that idle connections in the pool are recycled frequently:
import asyncpg;
pool = await asyncpg.create_pool(
"postgresql://user:pass@host/db",
max_inactive_connection_lifetime=1,
)This ensures connections are not held long enough to go stale across invocations.