Shard-aware high-performance bulk insert using serde_avro_fast for encoding.
BulkInserter routes records to the correct worker based on shard key computation, enabling true multi-head ingest where each record is sent directly to the worker that owns its shard.
How Shard-Aware Routing Works
- On initialization, fetch the routing table via
admin_show_shards - Fetch the table schema via
show_tableto identify shard key columns - Build a
RecordKeyBuilderto compute routing keys - For each record, compute its shard key hash using all shard key columns
- Use the routing table to determine which worker owns that shard
- Route the record directly to that worker
Performance
- Shard-aware routing eliminates server-side record redistribution
- Each worker receives only records that belong to its shards
- Faster Avro encoding with
serde_avro_fast(~6-7x faster than apache-avro) - Parallel encoding with rayon for maximum throughput
Insert Methods
The BulkInserter provides four insertion methods:
| Method | Use Case | Queue Throughput |
|---|---|---|
insert | Single record streaming | ~3-7M rec/s |
insert_with_backpressure | Memory-bounded streaming | ~3-7M rec/s |
insert_batch | Bulk loading | ~20-30M rec/s |
insert_batch_arc | Pre-wrapped records | ~30-35M rec/s |
The batch methods use parallel shard key computation and reduce lock contention by grouping records before acquiring locks. This provides up to 8x faster client-side queue throughput for large batches.
JSON Records
BulkInserter::new_json and BulkInserter::with_json_options make a BulkInserter<String> taking JSON objects as text: each record goes to the rank of a random shard, and each batch is sent as one JSON array to /insert/records/json.
Further Features
Backpressure Control
Use insert_with_backpressure() for memory-bounded operation:
Timed Flush
Configure automatic periodic flush:
Error Queue
Non-blocking async error collection:
Batch Callbacks
Real-time monitoring via BatchInsertionListener:
Example
Re-exports
pub use crate::bulk_inserter_common::ShardKeyExtractor;pub use crate::bulk_inserter_common::ShardKeyValue;pub use crate::bulk_inserter_common::ShardKeyValues;