Scale Spring Batch, comparison with Spring Cloud Task & best practices of Spring Batch!

Disclaimer: This blog content has been taken from my latest book:

“Cloud Native Microservices with Spring and Kubernetes”

Comparison Spring Cloud Task vs Spring Batch

  • Spring Cloud Task is complimentary of Spring Batch.
  • Spring Batch can be exposed as a Cloud Cloud Task.
  • Spring Cloud Task makes life easy to run and Java/Spring microservice application that do not need the robustness of the Spring Batch APIs.
  • Spring Cloud Task has good integration with Spring Batch and Spring Cloud Data Flow (SCDF). SCDF provides feature of batch orchestration, and UI dashboard to monitor Spring Cloud Task.
  • In nutshell, all Spring Batch services can be exposed/registered as Spring Cloud Task to have better control, monitoring, and manageability.

Best practices for Spring Batch:

  1. Use an external file system (Volume Services) for persistence of large files  with PCF/PAS due to the file system limitations. Refer to this link.
  2. Always use SCDF abstraction layer with UI dashboard to manage, orchestrate, and monitor Spring Batch applications.
  3. Always use Spring Cloud Task with Spring Batch for additional batch functionality.
  4. Always register and implement vanilla Spring Batch applications as Spring Cloud Task in SCDF.
  5. Use Spring Cloud Task when you need to run a finite workload via a simple Java micro-service.
  6. For High Availability (HA), implement best suited horizontal scaling technique from the top scaling techniques based on the use cases on containers (K8s).
  7. For large PROD system, use SCDF as an orchestration layer with Spring Cloud Task to manage large number of batches for large data sets.
  8. App data and batch repo should live in the same schema for transaction synchronization.

 Spring Batch Auto-scaling (both vertically and horizontally)

  • Vertical Scaling: No issue with that. H/w or POD size can be increased any time based on the usage of CPU and RAM for better performance and reliability. As you give the process more RAM, you can typically increase the chunk size which will typically increase overall throughput, but it doesn’t happen automatically.
  • Horizontal Scaling: There are  popular techniques, watch this YouTube video for detail and refer this GitHub code –
  1. Multi-threaded Steps – Each transaction/chunk executed by its separate threads, state is not persisted, only an option if u don’t need non-restartibility. 
  2. Parallel steps – Multiple independent steps run  in parallel via threads.
  3. Single JVM Async Item Writer/Item Processor. ItemProcessor calls are executed within a Java Future. The AsyncItemWriter unwrapps the result of the Future and passes it to a configured delegate to write.
  4. Partitioning – Data is partitioned then assigned to n workers that are being executed either within the same JVM via threads or in external JVMs launched dynamically when using Spring Cloud Task’s partition extensions. A good option when restartability is needed.
  5. Remote Chunking- Mostly I/O bound, sometimes when you need more processing power beyond the single JVM. It sends actual data remotely, only useful when processing is the bottleneck. Durable middleware is required for this option.

 Spring Batch Orchestration and Composition

SCDF doesn’t watch the jobs. It just shares the same DB as the batch job does so you can view the results. Once a job is launched via SCDF, SCDF itself has no interaction with the job. You can compose and orchestrate jobs by drag and drop and set dependency between jobs, which jobs should run in parallel and which one in sequence, execution order can also be set for multiple jobs scheduling.

 Achieve Active-Active operation for High Availability(HA) between two Data Centers/AZs

There are two standard ways:

  1. Place a shard Spring Batch Job repository between two active-active DC/AZs. Parallel sync happens in the job repository database. App data and batch repo should in the same schema for better synchronization as noted above. Transaction  isolation level set by default, so that one of active DC can run the job and other job should be failed when it tries to re-run the same job with same parameter. 
  2. Spring Cloud Task has this built-in functionality to restrict Spring Cloud Task Instances-

 Alerts and Monitoring of Spring Cloud Task and Spring Batch

  • Spring Cloud Task includes Micrometer health check and metrics  API out of the box.
  • Plain Prometheus is not suitable for jobs, because it uses pull mechanism and it won’t tell when job has finished or has some issues. If you want to use Prometheus for application metrics with Grafana visualization then follow this Prometheus rsocket-proxy API-

More References:

Published by

Rajiv Srivastava

Principal Architect with Wells Fargo

One thought on “Scale Spring Batch, comparison with Spring Cloud Task & best practices of Spring Batch!”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s