Skip to main content
Streaming sessions enable high-throughput, long-lived connections for both reading and appending data to S2 streams. They provide automatic retry, backpressure control, and efficient batching.

Append Sessions

Append sessions allow you to submit multiple batches of records while preserving order and managing backpressure.

Basic Usage

1

Create an append session

2

Submit batches

3

Wait for acknowledgements

4

Close the session

Always call close() to ensure all submitted batches are acknowledged before terminating the session.

Backpressure Control

Append sessions implement backpressure by limiting unacknowledged bytes in flight:
The submit() method will block when the unacknowledged bytes limit is reached, providing automatic backpressure.

Advanced: Reserve and Submit

For more control in async contexts like select! loops, use reserve() followed by submit():
The reserve() method is cancel-safe, making it ideal for use in select! loops.

Automatic Retry

Append sessions automatically retry on transient failures:
  • Network disconnections
  • Timeout errors
  • Server unavailability
The session tracks inflight appends and resends them on reconnection:

Read Sessions

Read sessions provide a streaming interface for consuming records with automatic retry and heartbeat monitoring.

Basic Usage

1

Create a read session

2

Consume batches

Following the Stream

Use with_wait_secs() to follow a stream in real-time:
The read session automatically adjusts the wait budget on retries to avoid excessive long polling.

Heartbeat Monitoring

Read sessions monitor for heartbeats to detect stalled connections:

Error Handling

Both append and read sessions handle errors gracefully:

Retryable Errors

These errors trigger automatic retry:
  • HeartbeatTimeout (read)
  • AckTimeout (append)
  • ServerDisconnected (append)
  • Network connectivity issues

Terminal Errors

These errors terminate the session:
  • SessionClosed
  • SessionDropped
  • Permission denied
  • Invalid requests

Best Practices

Reuse sessions: Create sessions once and reuse them for multiple operations to avoid connection overhead.
Tune buffer sizes: Adjust max_unacked_bytes based on your throughput requirements and memory constraints.
Handle graceful shutdown: Always call close() on sessions during application shutdown to ensure data is flushed.
Sessions use AbortOnDropHandle internally, so dropping a session will abort the background task. Always prefer explicit close() calls.