Skip to main content

Overview

The SideEffect 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

generic
The query type that contains request context and parametersConstraints: Clone + Send + Sync + 'static
generic
The candidate type representing selected resultsConstraints: Clone + Send + Sync + 'static

SideEffectInput

Arc<Q>
Shared reference to the original query that was processed
Vec<C>
The final candidates that were selected and will be returned to the user

Methods

enable

fn
Determines whether this side effect should run for the given query
Arc<Q>
Shared reference to the query object
bool
Returns true if this side effect should run, false to skip. Default implementation returns true.

run

async fn
Performs the side effect operation asynchronously
Arc<SideEffectInput<Q, C>>
Shared reference to the side effect input containing query and selected candidates
Result<(), String>
Returns Ok(()) on success, or an error message on failure. Errors are logged but do not affect the pipeline result.

name

fn
Returns a stable name for logging and metrics
&'static str
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 Arc for 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

  1. Non-Blocking: Keep operations fast; avoid expensive synchronous work
  2. Error Handling: Handle errors gracefully; don’t let side effects affect the response
  3. Idempotent: Design side effects to be safely retried if needed
  4. Conditional Execution: Use enable() to control when side effects run
  5. Resource Sharing: Use Arc to share clients and avoid cloning heavy objects
  6. Monitoring: Log side effect failures for debugging
  7. 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