Skip to the content.

🧠 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.


⚑ Why Micro-Batching for DB?

Databases have overhead per operation:

Processing each record individually is slow and inefficient.

Micro-batching solves this by:

  1. Collecting multiple records (say 50–500)
  2. Sending them in one DB query or transaction
  3. 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);

With Micro-Batching

INSERT INTO orders (id, user_id, amount) 
VALUES 
  (1, 101, 500),
  (2, 102, 200),
  (3, 103, 100);

βš™οΈ 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

  1. Batch Size

    • Too small β†’ low efficiency
    • Too large β†’ higher memory usage, potential latency spikes
  2. Time vs Size Trigger

    • Batch after N records or after T milliseconds (whichever comes first)
  3. Transactional Guarantees

    • Micro-batching usually wrapped in one transaction
    • If it fails β†’ retry the batch
  4. 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();

πŸ”„ 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:

Micro-batch = sweet spot for speed and efficiency.