Skip to content

Performance Optimization

Caching Strategies

Reduce redundant operations with intelligent caching.

Refer to the main Parallel Processing page for more details and considerations.

export default defineWorkflow('batch-processing', async (flow) => {
const { items } = flow.params;
// Process items in parallel with concurrency control
const results = await Promise.all(
items.map(item =>
flow.do(`process item ${item.id}`, async () => {
return processItem(item);
})
)
);
return results;
});

Efficient resource management is crucial for performance and stability. See also the Resource Management deep dive and the Integrations page for client configurations.

Connection Pooling

Optimize database and API connections with smart pooling strategies.

Resource Limits

Set appropriate limits to prevent resource exhaustion.

See Integrations for full client configuration details.

const DbClient = () => createDbClient({
// Connection pool settings
poolSize: 10,
minConnections: 2,
maxIdleTime: '5 minutes',
// Query timeout
queryTimeout: '30 seconds',
// Circuit breaker
circuitBreaker: {
failureThreshold: 5,
resetTimeout: '1 minute'
}
});
export default defineWorkflow('data-processing', async (flow) => {
const db = flow.use(DbClient);
await flow.do('process data', async () => {
return db.query('SELECT * FROM data');
});
});

See Integrations for full client configuration details. Note the retry and circuit breaker settings, detailed further in Retry Policies.

const HttpClient = () => createHttpClient({
// Connection settings
maxConnections: 100,
keepAlive: true,
timeout: '30 seconds',
// Compression
compression: true,
// Connection pooling
agent: {
keepAlive: true,
maxSockets: 100,
maxFreeSockets: 10,
timeout: 60000
},
// Retry configuration
retries: {
limit: 3,
backoff: 'exponential'
},
// Circuit breaker
circuitBreaker: {
failureThreshold: 5,
resetTimeout: '1 minute'
}
});
export default defineWorkflow('data-processing', async (flow) => {
// Cache results based on input parameters
const data = await flow.do('fetch data', {
cache: {
ttl: '1 hour',
key: flow.params.id,
tags: ['user-data'],
staleWhileRevalidate: '5 minutes'
}
}, async () => {
return fetchExpensiveData(flow.params);
});
return data;
});