Urgent.News

600+ sources. One page. See who else covered it.

Editions

Tech

Should your daily batch job live inside your main application?

Most Spring Boot services end up with a scheduled job in them somewhere. A nightly reconciliation, a report, an export to some partner system. It starts small, and it goes in the main app because that's where the domain code already is. One artifact, one deployment, one pipeline. That's a real advantage and it's why most teams do it. This post is about when that stops being a good trade, how to…

Spring Boot services often include scheduled jobs for tasks like nightly reconciliations or report generation. Initially, these jobs are placed within the main application due to its centralized domain code location. This approach offers benefits like a single artifact, deployment, and pipeline. However, there are drawbacks to sharing the same JVM for both the API and the batch job.

The memory consumption of the batch job is minimal throughout the day, but it spikes during execution and then drops back to minimal usage. If both the API and batch job are running in the same JVM, the application pod must be sized for the peak memory usage of the batch job. This leads to inefficient utilization of resources, as multiple replicas of the API consume unnecessary memory reserved for the batch job running once per day.

Unlike CPU limits, memory limits are non-compressible. Exceeding the memory limit triggers the kernel to immediately terminate the container process without any warning or opportunity for graceful shutdown. In the case of a batch job running within the same pod as the API, the API is at risk of being terminated prematurely, negatively impacting the user experience.

Even if the batch job stays within memory limits, its heavy memory allocations can cause longer garbage collection pauses, leading to increased request latency for the API. This degradation in performance may go unnoticed since it occurs during off-peak hours when the job runs.

To mitigate these issues, the batch job should be designed as a standalone process that starts, performs its task, and then exits. This can be achieved by creating a separate command-line runner or application runner in Spring Boot. By implementing `CommandLineRunner` or `ApplicationRunner` interfaces, the job can execute its functionality and exit cleanly.

If using Spring Batch, `ExitCodeGenerator` can be utilized to set the process exit code based on the job's success or failure status. Ensuring the JVM exits properly is crucial, as a lingering process can cause Kubernetes to misinterpret the job's state, leading to failed jobs and ineffective retries or alerting.

To prevent the batch job from coexisting with the API within the same pod, Kubernetes CronJob functionality can be employed. By defining a separate `CronJob` resource, the job can be scheduled independently, avoiding resource contention with the API. Proper configuration of concurrency policies, such as setting `concurrencyPolicy` to "Forbid," prevents multiple instances of the job from running concurrently, which could otherwise result in data corruption or other inconsistencies.

Additionally, configuring `restartPolicy` to "Never" ensures that the pod is not automatically restarted if the batch job encounters an error, further isolating the job from the API's operation. These measures collectively enhance the reliability and efficiency of batch processing within a Spring Boot application ecosystem.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.

Read the original at dev.to →

More in Tech

How to bring your software delivery workflow into GitHub with agent apps

See how four GitHub Agent Apps can help you scope, secure, roll out, and ship a feature across the SDLC–all without leaving GitHub. The post How to bring your software delivery workflow into GitHub with agent apps appeared first on The GitHub Blog .

  • GitHub agent apps streamline workflow by answering key questions within GitHub.
  • Optional step for inviting teammates deferred for solo signups based on support insights.
  • Deployment risk assessed by PagerDuty agent before shipping changes.