Skip to main content
This guide covers performance optimization techniques for the S2 SDK, from batching strategies to connection pooling and memory management.

Throughput Optimization

Producer Configuration

For maximum throughput, tune the producer’s batching and backpressure settings:
High throughput trade-off: Larger batches and higher linger times improve throughput but add latency.

Append Session Configuration

For direct session control:
Higher max_unacked_bytes allows more data in flight but uses more memory. Balance this with your available resources.

Parallel Submissions

Submit multiple batches concurrently:

Latency Optimization

Low-Latency Producer

Minimize batching delays for low-latency scenarios:
Low latency trade-off: Lower linger and smaller batches reduce latency but decrease throughput.

Direct Append

For single-record appends with minimal overhead:
Direct append() calls have higher per-record overhead. Use only when latency is critical and throughput is low.

Connection Management

Connection Pooling

The SDK automatically pools HTTP connections:
  • Up to 90 concurrent requests per client
  • Connections idle for 90 seconds are reaped
  • Automatic reconnection on connection failure

Reuse Clients

Share S2 instances across threads:
Reusing clients amortizes connection overhead and reduces resource usage.

Memory Management

Backpressure Control

Prevent unbounded memory growth with backpressure:

Streaming Large Reads

Process read batches incrementally to avoid loading entire streams into memory:
Use with_limit_records() or with_limit_bytes() to bound memory usage during reads.

Compression

Enable Compression

Reduce network bandwidth with compression:
Compression options:
  • Compression::None - No compression (default)
  • Compression::Gzip - Good balance, widely supported
  • Compression::Zstd - Better compression ratio, faster
Zstd typically offers the best performance for S2 workloads.

When to Use Compression

Enable compression when:
  • Network bandwidth is limited
  • Records are highly compressible (text, JSON, logs)
  • Network cost is a concern
Disable compression when:
  • Records are already compressed (images, video)
  • CPU is constrained
  • Records are very small (< 100 bytes)

Batching Strategies

Adaptive Batching

Adjust batching based on load:

Size-Based Batching

For variable-sized records:

Read Performance

Batch Size Tuning

Control read batch sizes for optimal memory/latency balance:

Parallel Reads

Read from multiple streams concurrently:

Following the Tail

Optimize for real-time consumption:

Retry Configuration

Aggressive Retries

For high-availability scenarios:

Fast Failure

For latency-sensitive scenarios:

Monitoring and Metrics

Track Batch Sizes

Monitor Backpressure

Best Practices Summary

Profile first: Measure your actual bottlenecks before optimizing. Use tools like tokio-console or tracing.
Start with defaults: The default configurations work well for most use cases. Only tune when you have specific performance requirements.
Monitor memory: Track heap usage and set appropriate backpressure limits to prevent OOM.
Reuse sessions: Create sessions once and reuse them for multiple operations to amortize connection overhead.
Test at scale: Performance characteristics change at different scales. Test with production-like data volumes.
The S2 SDK uses tokio for async runtime. Ensure your application uses an appropriately sized thread pool for your workload.

Performance Checklist

  • Batching configured appropriately for workload
  • Backpressure limits set based on available memory
  • Compression enabled for compressible data
  • Clients reused across operations
  • Sessions reused for multiple appends/reads
  • Retry configuration matches availability requirements
  • Monitoring in place for batch sizes and latency
  • Testing completed at production scale