@@ -65,22 +65,20 @@ Reduces payload size by 60-80% on average, improving:
6565
6666### Compression Methods
6767
68- 1 . ** Brotli** (preferred): 20-30% smaller than gzip
69- - Quality level: 5 (balanced speed/compression)
70- - Mode: Text optimization
68+ AgenticPay uses the maintained Express ` compression ` middleware with streaming
69+ backpressure support, negotiated encodings, and route-level filters.
7170
72- 2 . ** Gzip ** (fallback): Universal support
73- - Compression level: 6
74- - Minimum size threshold: 1KB
71+ - Compression level: 6
72+ - Minimum size threshold: 1KB by default
73+ - Skips images, audio, video, archives, and already-compressed content
7574
7675### Implementation
7776
7877Located in: ` backend/src/middleware/compression.ts `
7978
8079``` typescript
8180app .use (compressionMiddleware ({
82- brotliLevel: 5 ,
83- gzipLevel: 6 ,
81+ level: 6 ,
8482 minSizeBytes: 1024 ,
8583}));
8684```
@@ -90,7 +88,7 @@ app.use(compressionMiddleware({
9088Access compression metrics via:
9189
9290```
93- GET /api/v1/monitoring/pool/ compression
91+ GET /api/v1/monitoring/compression
9492```
9593
9694Returns:
@@ -108,6 +106,35 @@ Returns:
108106
109107---
110108
109+ ## API Response Streaming
110+
111+ Large exports are streamed with chunked transfer encoding instead of being buffered in memory.
112+
113+ Endpoints:
114+
115+ ``` bash
116+ GET /api/v1/exports/audit/stream? format=csv& limit=100000
117+ GET /api/v1/exports/audit/stream? format=jsonl& batchSize=1000
118+ GET /api/v1/exports/payments/stream? format=csv
119+ ```
120+
121+ Reusable helpers live in ` backend/src/middleware/streaming.ts ` :
122+
123+ ``` typescript
124+ const query = parseStreamingQuery (req .query );
125+ await streamDataset ({
126+ req ,
127+ res ,
128+ items: takeStreamItems (fetchRows (), query .limit ),
129+ format: query .format ,
130+ });
131+ ```
132+
133+ The streaming helpers set ` Transfer-Encoding: chunked ` , disable proxy buffering with
134+ ` X-Accel-Buffering: no ` , honor HTTP backpressure, and track completed, aborted, and failed streams.
135+
136+ ---
137+
111138## Cursor-Based Pagination
112139
113140### Overview
@@ -198,6 +225,40 @@ GET /api/v1/payments -H "If-None-Match: abc123def456"
198225
199226Optimized connection pooling with PgBouncer for efficient resource utilization:
200227
228+ ### Read Replicas and Failover
229+
230+ Read replica routing is configured with:
231+
232+ ``` bash
233+ DB_READ_REPLICA_URLS=postgresql://user:pass@replica-a:5432/agenticpay,postgresql://user:pass@replica-b:5432/agenticpay
234+ DB_REPLICA_MAX_LAG_MS=5000
235+ DB_REPLICA_HEALTH_CHECK_INTERVAL_MS=30000
236+ DB_REPLICA_FAILOVER_COOLDOWN_MS=15000
237+ ```
238+
239+ ` backend/src/config/database.ts ` exposes ` ReadReplicaRouter ` , which routes ` SELECT ` and ` WITH `
240+ queries across healthy replicas and falls back to ` DATABASE_URL ` when no replica is available or
241+ replica lag exceeds the configured threshold. Terraform can provision replicas with
242+ ` db_read_replica_count ` and wires ` DB_READ_REPLICA_URLS ` into the backend service.
243+
244+ ### WebSocket Pooling
245+
246+ WebSocket connections are managed by ` backend/src/websocket/pool.ts ` . The pool enforces capacity,
247+ tracks active and queued connections, batches outbound messages through ` ManagedConnection ` , and
248+ supports clean shutdown. Tune batching with:
249+
250+ ``` typescript
251+ attachWebSocketServer ({
252+ server ,
253+ options: {
254+ maxConnections: 250 ,
255+ maxQueueSizePerConnection: 500 ,
256+ flushIntervalMs: 25 ,
257+ maxBatchSize: 50 ,
258+ },
259+ });
260+ ```
261+
201262** Benefits:**
202263- Prevents connection exhaustion
203264- Detects and prevents connection leaks
@@ -239,7 +300,7 @@ Located in: `backend/src/config/database.ts`
239300Access pool health via:
240301
241302```
242- GET /api/v1/monitoring/pool/ health
303+ GET /api/v1/monitoring/health
243304```
244305
245306Returns:
@@ -261,7 +322,7 @@ Returns:
261322Automatic detection of connection leaks:
262323
263324```
264- GET /api/v1/monitoring/pool/ leaks
325+ GET /api/v1/monitoring/leaks
265326```
266327
267328- Monitors connection acquisition/release
@@ -271,7 +332,7 @@ GET /api/v1/monitoring/pool/leaks
271332### Metrics Endpoint
272333
273334```
274- GET /api/v1/monitoring/pool/ metrics
335+ GET /api/v1/monitoring/metrics
275336```
276337
277338Returns comprehensive pool statistics including:
@@ -358,7 +419,7 @@ cache.registerWarmer('dashboard:overview',
358419### Metrics Endpoint
359420
360421```
361- GET /api/v1/monitoring/pool/ cache
422+ GET /api/v1/monitoring/cache
362423```
363424
364425Returns:
@@ -383,7 +444,7 @@ Returns:
383444Comprehensive view of all performance metrics:
384445
385446```
386- GET /api/v1/monitoring/pool/ performance
447+ GET /api/v1/monitoring/performance
387448```
388449
389450Returns combined metrics:
0 commit comments