Database Best Practices
Performance tips, optimization strategies, and troubleshooting guidance for database integration with Expanso Edge.
Performance Optimization
1. Always Use Batching for Writes
Batch inserts provide 10-100x better performance than individual inserts.
Do:
output:
sql_insert:
driver: postgres
dsn: postgres://user:pass@localhost:5432/db?sslmode=disable
table: events
columns: ['data']
args_mapping: 'root = [ this ]'
batching:
count: 500 # Batch 500 records
period: 10s # Or every 10 seconds
Don't:
output:
sql_insert:
# No batching - slow!
driver: postgres
table: events
columns: ['data']
args_mapping: 'root = [ this ]'
Performance impact:
| Batch Size | Inserts/sec | Improvement |
|---|---|---|
| 1 (no batching) | 100 | Baseline |
| 100 | 5,000 | 50x faster |
| 1,000 | 20,000 | 200x faster |
2. Use Local Databases for Edge Storage
Use SQLite for edge-local data instead of remote cloud databases.
Do:
output:
sql_insert:
driver: sqlite
dsn: 'file:///var/lib/expanso/cache.db?_journal_mode=WAL'
table: events
columns: ['data']
args_mapping: 'root = [ this ]'
Benefits:
- Sub-millisecond queries (vs 50-200ms for cloud)
- Works offline
- No network costs
- Scales to millions of rows per database
3. Configure Connection Pooling
Properly configure connection pools to balance performance and resource usage.
Do:
output:
sql_insert:
driver: postgres
dsn: postgres://user:pass@localhost:5432/db?sslmode=disable
table: events
columns: ['data']
args_mapping: 'root = [ this ]'
conn_max_open: 10 # Max 10 concurrent connections
conn_max_idle: 2 # Keep 2 idle connections
conn_max_idle_time: 5m # Close idle after 5 min
conn_max_life_time: 30m # Recycle after 30 min
For SQLite:
conn_max_open: 1 # SQLite works best with single writer
For cloud databases:
conn_max_open: 20 # Can handle more concurrent connections
conn_max_idle: 5