Skip to main content
Batching records is essential for achieving high throughput in S2. The SDK provides automatic batching with configurable parameters for linger time, batch size, and byte limits.

Why Batching Matters

Batching reduces overhead by:
  • Minimizing network round trips
  • Reducing per-request costs
  • Improving compression efficiency
  • Enabling higher throughput
S2 enforces maximum batch limits: 1000 records or 1 MiB metered bytes per batch.

BatchingConfig

The BatchingConfig struct controls how records are batched:

Configuration Parameters

1

Linger Duration

How long to wait for more records before flushing a batch.
Lower linger = lower latency, higher linger = better batching efficiency.
2

Max Batch Records

Maximum number of records per batch (1-1000).
Must be at least 1 and cannot exceed 1000.
3

Max Batch Bytes

Maximum metered bytes per batch (8 bytes - 1 MiB).
Must be at least 8 bytes and cannot exceed 1 MiB (1048576 bytes).

Automatic Batching with Producer

The Producer provides the simplest way to leverage batching:
The producer handles batching, backpressure, and retries automatically.

Manual Batching

For more control, use AppendInputs to batch a stream of records:

Batching Behavior

Flush Triggers

A batch is flushed when:
  1. Record count limit reached: Batch contains max_batch_records
  2. Byte limit reached: Batch size reaches max_batch_bytes
  3. Linger timeout: linger duration expires since first record
  4. Input stream ends: No more records available

Overflow Handling

When a record would overflow the current batch:
Records are never split across batches. If a record would overflow, it becomes the first record of the next batch.

Oversized Records

Records larger than max_batch_bytes are rejected:

Fencing and Sequence Numbers

Batching can be combined with fencing tokens and sequence number matching:
The match_seq_num is automatically incremented by the batch size, ensuring continuity across batches.

Performance Tuning

Latency vs Throughput

Low latency (minimize delay):
High throughput (maximize batching):

Record Size Considerations

Small records (< 1 KiB):
Large records (> 100 KiB):

Best Practices

Start with defaults: The default configuration (5ms linger, 1000 records, 1 MiB) works well for most use cases.
Measure metered bytes: Use record.metered_bytes() to understand your record sizes and tune max_batch_bytes accordingly.
Use Producer for simplicity: Unless you need fine-grained control, the Producer API handles batching optimally.
Batching adds a small amount of latency (up to linger duration) but dramatically improves throughput for high-volume workloads.