Skip to content

Performance Optimization

Orchestrix is designed to be lightweight, but there are several ways you can optimize its performance for high-load applications.

1. Use Parallel Execution

As mentioned in Parallel Execution, running independent steps concurrently is the easiest way to reduce total flow duration.

2. In-Memory vs. Distributed Stores

While distributed stores like Redis are necessary for consistency in distributed systems, they add network latency (usually 1-5ms per operation).

  • Use In-Memory: For single-instance apps or when high-speed is more important than cross-node consistency.
  • Optimize Redis: Use a local Redis instance or a high-performance cluster to minimize latency.

3. Keep the Context Small

The FlowContext is passed around and stored in idempotency records.

  • Avoid large blobs.
  • Store IDs instead of full objects.
  • Clear data you no longer need.

4. Hook Overhead

Hooks are powerful but they run on every step.

  • Keep hook functions synchronous if possible.
  • If doing heavy work in a hook (like sending a metric over the network), do it asynchronously without awaiting it in the hook, or use a background buffer.
ts
onStepSuccess: (result) => {
  // Don't await this if it's slow
  metricsBuffer.push(result); 
}

5. Plugin Efficiency

If you write custom plugins, ensure they don't block the execution flow unnecessarily. Orchestrix runs plugins in the order they are registered.

6. Cold Start Awareness

If running in AWS Lambda or Google Cloud Functions:

  • Initialize your flows and stores outside the handler function to reuse connections and cached instances.
ts
// Outside the handler
const store = createRedisIdempotencyStore(redis);
const flow = create("my-flow", { idempotency: store });

export const handler = async (event) => {
  return await flow.run(event);
};

Summary

Orchestrix's overhead is minimal (typically < 1ms per flow execution). Most "slowness" in workflows comes from the work being done in the steps or from network calls to external stores.

Released under the MIT License.