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
TheBatchingConfig struct controls how records are batched:
Configuration Parameters
1
Linger Duration
How long to wait for more records before flushing a batch.
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
TheProducer provides the simplest way to leverage batching:
Manual Batching
For more control, useAppendInputs to batch a stream of records:
Batching Behavior
Flush Triggers
A batch is flushed when:- Record count limit reached: Batch contains
max_batch_records - Byte limit reached: Batch size reaches
max_batch_bytes - Linger timeout:
lingerduration expires since first record - 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 thanmax_batch_bytes are rejected:
Fencing and Sequence Numbers
Batching can be combined with fencing tokens and sequence number matching:Performance Tuning
Latency vs Throughput
Low latency (minimize delay):Record Size Considerations
Small records (< 1 KiB):Best Practices
Batching adds a small amount of latency (up to
linger duration) but dramatically improves throughput for high-volume workloads.