π§ What is Micro-Batching?
Micro-batching is a technique where instead of processing each record individually, the system collects a small batch of records and processes them together in one operation.
- Smaller than a full batch, but bigger than single events β hence βmicroβ.
- Common in databases, stream processing, and analytics pipelines.
β‘ Why Micro-Batching for DB?
Databases have overhead per operation:
-
Every insert/update/delete has:
- Transaction overhead
- Network round-trip
- Logging & indexing cost
Processing each record individually is slow and inefficient.
Micro-batching solves this by:
- Collecting multiple records (say 50β500)
- Sending them in one DB query or transaction
- Reducing per-record overhead
β Result: higher throughput, lower latency per record, and less load on the DB.
π§© Example: Single vs Micro-Batch Inserts
Without Micro-Batching
INSERT INTO orders (id, user_id, amount) VALUES (1, 101, 500);
INSERT INTO orders (id, user_id, amount) VALUES (2, 102, 200);
INSERT INTO orders (id, user_id, amount) VALUES (3, 103, 100);
- 3 separate transactions β 3 round-trips β 3x overhead
With Micro-Batching
INSERT INTO orders (id, user_id, amount)
VALUES
(1, 101, 500),
(2, 102, 200),
(3, 103, 100);
- 1 transaction β 1 round-trip β much faster
βοΈ When Micro-Batching is Useful
| Scenario | Benefit |
|---|---|
| High-volume inserts | Reduce DB overhead |
| Stream processing (Kafka, Flink) | Aggregate events before writing |
| Analytics pipelines | Efficient bulk writes |
| Logging or metrics storage | Minimize per-insert cost |
π§ Implementation Considerations
-
Batch Size
- Too small β low efficiency
- Too large β higher memory usage, potential latency spikes
-
Time vs Size Trigger
- Batch after N records or after T milliseconds (whichever comes first)
-
Transactional Guarantees
- Micro-batching usually wrapped in one transaction
- If it fails β retry the batch
-
Idempotency
- Ensure retries donβt duplicate records
π» Java Example (JDBC Batch Insert)
Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
String sql = "INSERT INTO orders (id, user_id, amount) VALUES (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
for (Order order : orders) {
ps.setInt(1, order.getId());
ps.setInt(2, order.getUserId());
ps.setInt(3, order.getAmount());
ps.addBatch();
}
int[] result = ps.executeBatch();
conn.commit();
ps.close();
conn.close();
addBatch()collects multiple insertsexecuteBatch()sends them together in one round-trip
π Micro-Batching vs Bulk Processing vs Single Event
| Type | Description | Pros | Cons |
|---|---|---|---|
| Single-event | Process 1 record at a time | Simple | High overhead |
| Micro-batch | Small batch (tens-hundreds of records) | High throughput, moderate latency | Slight delay for batch accumulation |
| Full batch | Very large batch (thousands+) | Max efficiency | High latency, memory usage |
π Analogy
Think of mailing letters:
- Single insert = Sending 1 letter per trip πΆ
- Micro-batch = Sending 50 letters in one envelope π
- Full batch = Sending 5000 letters in a truck π
Micro-batch = sweet spot for speed and efficiency.