Overview
TheSideEffect trait defines asynchronous actions that run after candidate selection without affecting the pipeline result. Side effects are used for logging, caching, analytics, and other non-blocking operations.
Side effects run asynchronously and do not block the pipeline response. Failures in side effects do not affect the returned results.
Trait Definition
Type Parameters
The query type that contains request context and parametersConstraints:
Clone + Send + Sync + 'staticThe candidate type representing selected resultsConstraints:
Clone + Send + Sync + 'staticSideEffectInput
Shared reference to the original query that was processed
The final candidates that were selected and will be returned to the user
Methods
enable
run
Performs the side effect operation asynchronously
Shared reference to the side effect input containing query and selected candidates
Returns
Ok(()) on success, or an error message on failure. Errors are logged but do not affect the pipeline result.name
Returns a stable name for logging and metrics
A short type name derived from the implementing struct
Example Implementation
Here’s a real example that caches request information for offline analysis:Usage Notes
- Side effects run asynchronously after the pipeline completes
- They do not block the response being returned to the client
- Failures are logged but do not affect the pipeline result
- Multiple side effects can be configured and will run concurrently
- Use
Arcfor sharing data efficiently between side effects - Side effects receive the final selected candidates, not intermediate results
Common Side Effect Types
Analytics & Logging
- Log selected candidates for offline analysis
- Track candidate source distributions
- Record feature values for ML training
Caching
- Cache selected results for fast re-serving
- Pre-warm caches for related requests
- Store impressions for deduplication
External Updates
- Notify downstream systems of selections
- Update user profiles based on served content
- Trigger recommendation model updates
Monitoring
- Emit metrics about selected candidates
- Track content diversity
- Monitor score distributions
Best Practices
- Non-Blocking: Keep operations fast; avoid expensive synchronous work
- Error Handling: Handle errors gracefully; don’t let side effects affect the response
- Idempotent: Design side effects to be safely retried if needed
- Conditional Execution: Use
enable()to control when side effects run - Resource Sharing: Use
Arcto share clients and avoid cloning heavy objects - Monitoring: Log side effect failures for debugging
- Timeouts: Implement timeouts for external service calls
Performance Considerations
- Side effects run asynchronously and don’t add latency to the response
- However, they still consume resources (CPU, memory, network)
- Monitor side effect execution time and failure rates
- Consider rate limiting side effects if they call external services
- Use batching for efficiency when possible
Error Handling
Testing Side Effects
- Test that side effects are enabled/disabled correctly
- Verify data is formatted correctly for external systems
- Mock external clients to test error handling
- Ensure side effect failures don’t crash the pipeline
See Also
- Selector - Selects candidates before side effects run
- Candidate Pipeline Architecture - Pipeline execution flow