Video in progress
You need to process several million records a day. When is a plain Java Stream pipeline enough, and when should you move to something like Spring Batch or Kafka Streams?
If the data fits in one JVM's memory and time window and restarting is simple, a stream is enough. If you need to resume from a restart, record partial failures, get monitoring and split across several instances, Spring Batch. If the data arrives continuously and you need stateful aggregation, Kafka Streams or something similar.
📖 Answer
A million records is not a big volume in itself: a single JVM runs through them with a stream in a few seconds. The problem isn't volume but operational requirements.
When a plain stream is enough:
- the job finishes in 10–15 minutes;
- if it fails, restarting from the beginning is safe (idempotent writes);
- "stop everything" is enough for bad records;
- one instance is enough.
When you need Spring Batch:
- the job runs for hours, and if it fails at 80%, starting over is expensive: the
JobRepositoryremembers where it stopped; - chunk-oriented processing: every 1000 records in a separate transaction;
- a skip/retry policy: skip up to 10 bad records and report on them;
- splitting across several workers with partitioning;
- audit: who ran, when, how many records.
When you need Kafka Streams (or Flink):
- the data is a continuous flow, not a batch;
- you need time windows, joins and state (a state store);
- horizontal scaling by partitions;
- exactly-once semantics are required.
In practice I often choose an in-between option: our own code, but with batches.
long lastId = checkpointRepo.load("invoice-export");
List<Invoice> page;
while (!(page = repo.findNextPage(lastId, 1000)).isEmpty()) {
List<Line> lines = page.stream().map(mapper::toLine).toList();
writer.writeAll(lines);
lastId = page.get(page.size() - 1).id();
checkpointRepo.save("invoice-export", lastId); // we save where we stopped
}
A stream on the inside, and pagination with a checkpoint on the outside. If requirements grow and we start writing skip/retry/monitoring by hand, that's the sign it's time to move to Spring Batch. Bringing in the framework up front adds extra configuration and tables for a small task.
Sign up to mark studied questions and save your progress.