Video in progress
For processing a large volume of data, how do you choose between Spring Batch, a plain @Scheduled job and stream processing?
For a small, fast job where restart isn't required, @Scheduled + a lock. For a large bulk job that can be split into chunks, must resume where it stopped and must report, Spring Batch. When data arrives continuously and you need results fast, stream processing (Kafka Streams, Flink).
📖 Answer
@Scheduled + ShedLock, for example to cancel expired reservations every hour. Simple, but progress, restart and statistics you write all yourself.
Spring Batch is for nightly calculations, big CSV imports, monthly reports. Chunked transactions, job/step state in the database (JobRepository), retry and skip policies, resuming from where it stopped, parallel processing with partitioning: all of it comes ready-made. The price is its own schema and concepts (Job, Step, Reader, Processor, Writer), which take time to learn.
@Bean
Step importStep(JobRepository repo, PlatformTransactionManager tx) {
return new StepBuilder("import", repo)
.<CsvRow, Customer>chunk(500, tx)
.reader(csvReader()).processor(validator()).writer(jpaWriter())
.faultTolerant().skip(ValidationException.class).skipLimit(100)
.build();
}
Stream processing works as each event arrives: real-time fraud detection, aggregates, materialized views. It has state, windowing and "exactly-once" inside Kafka. But it's heavy operationally: state stores, rebalances, reprocessing.
When choosing, ask: when is the result needed, in seconds or by morning? How big is the volume, and how costly is a restart after a failure? What can the team carry?
Often the two go together: the stream gives quick numbers, and a nightly batch recomputes the exact figures to verify.
Sign up to mark studied questions and save your progress.