Skip to content

Commit c0d0265

Browse files
committed
Docs: README, add section on AWS Lambda use
1 parent 6190c9a commit c0d0265

1 file changed

Lines changed: 59 additions & 8 deletions

File tree

README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ but also robust.
6969

7070
### Kubernetes / Container Deployment
7171

72-
When deploying to Kubernetes or other container orchestration platforms, configure `initialConnections`
72+
When deploying to Kubernetes or other container orchestration platforms, configure `initialConnections`
7373
in addition to `minConnections` and `maxConnections`:
7474

7575
```java
@@ -86,15 +86,15 @@ DataSourcePool pool = DataSourcePool.builder()
8686

8787
**Why this matters:**
8888

89-
- **Rapid production readiness:** When a new pod is deployed, it immediately serves production traffic.
90-
Setting `initialConnections` higher than `minConnections` (typically between min and max) ensures
89+
- **Rapid production readiness:** When a new pod is deployed, it immediately serves production traffic.
90+
Setting `initialConnections` higher than `minConnections` (typically between min and max) ensures
9191
the pod can handle incoming requests without the cold-start overhead of creating many connections.
9292

93-
- **Automatic scaling down:** The pool continuously trims unused connections in the background
94-
(default trim frequency is 59 seconds). Over time, the pool naturally shrinks back to a
93+
- **Automatic scaling down:** The pool continuously trims unused connections in the background
94+
(default trim frequency is 59 seconds). Over time, the pool naturally shrinks back to a
9595
sustainable size as demand normalizes, so you don't waste resources.
9696

97-
- **Prevent connection storms:** Without adequate initial connections, new deployments spike
97+
- **Prevent connection storms:** Without adequate initial connections, new deployments spike
9898
database load by creating many new connections simultaneously to service incoming requests.
9999

100100
**Configuration strategies:**
@@ -120,11 +120,62 @@ DataSourcePool pool = DataSourcePool.builder()
120120
.maxConnections(100)
121121
```
122122

123-
Start with these values as a baseline and adjust based on your application's observed connection usage
124-
and deployment patterns. The pool will automatically trim idle connections over time, so starting with
123+
Start with these values as a baseline and adjust based on your application's observed connection usage
124+
and deployment patterns. The pool will automatically trim idle connections over time, so starting with
125125
more connections during deployment doesn't permanently increase resource consumption.
126126

127127

128+
### AWS Lambda
129+
130+
The connection pool has built-in support for AWS Lambda. When running in Lambda, the pool
131+
**automatically disables background thread validation** to optimize for cost and
132+
resource efficiency.
133+
134+
**Automatic Detection:**
135+
136+
The pool detects Lambda environments by checking for the `LAMBDA_TASK_ROOT` environment variable (set by
137+
AWS Lambda runtime). When detected, `validateOnHeartbeat` is automatically set to `false`.
138+
139+
**What Changes:**
140+
141+
- **Background heartbeat thread is disabled:** Normally the pool validates connection health every 30 seconds
142+
in the background. This thread is skipped in Lambda to avoid unnecessary CPU costs.
143+
144+
- **Connection validation still works:** Dead or stale connections are still detected eagerly when they are
145+
returned to the pool or when you attempt to use them. This ensures the pool remains robust.
146+
147+
- **Why this matters:** Lambda functions are charged per millisecond of execution. Background threads consume
148+
CPU time even when the function is idle, directly increasing your Lambda costs. Serverless functions are
149+
ephemeral and short-lived, making background threads less useful anyway.
150+
151+
**Configuration for Lambda:**
152+
153+
Keep connection pools lean in Lambda since functions are short-lived and scale horizontally:
154+
155+
```java
156+
DataSourcePool pool = DataSourcePool.builder()
157+
.name("mypool")
158+
.url("jdbc:postgresql://db.example.com:5432/myapp")
159+
.username("user")
160+
.password("pass")
161+
.minConnections(1) // Minimal baseline
162+
.initialConnections(2) // Only what's needed for one invocation
163+
.maxConnections(10) // Rarely needed; horizontal scaling handles load
164+
.build();
165+
```
166+
167+
168+
169+
**Important notes:**
170+
171+
- Connection pooling in Lambda is **per-Lambda instance**, not per-invocation. A connection pool
172+
persists across warm invocations of the same Lambda container.
173+
174+
- Always minimize `minConnections` to reduce startup time and resource usage during cold starts.
175+
176+
- The pool is still fully robust and reliable in Lambda despite the disabled background heartbeat.
177+
178+
128179
### Mature
129180

130181
This pool has been is heavy use for more that 10 years and stable since April 2010 (the last major refactor to use `java.util.concurrent.locks`).

0 commit comments

Comments
 (0)