I'm using Spring Batch with a Kubernetes Cron Manager to schedule and run a job. The job involves calling an external API to read, process, and write data. However, when I execute the job with a dataset of 200,000 items, it takes an excessively long time to complete, at least 5 hours.
In my configuration, I have set up a single replica set with a single pod in the Kubernetes cluster, and I have also configured Spring Batch to use 40 task executors for concurrent processing. Despite these settings, the job execution time is still significantly slow.
I would appreciate any insights or suggestions on how to improve the execution time of my job. Are there any specific optimizations or best practices I should consider when working with Spring Batch and Kubernetes Cron Manager in such scenarios?
This is my spring batch kubernetes configuration
resources:
requests:
cpu: 8
memory: 8Gi
limits:
cpu: 16
memory: 16Gi
This is my Task executor
@Bean
@StepScope
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(40);
executor.setMaxPoolSize(40);
executor.setThreadNamePrefix("spring_batch_worker-");
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
This is my writer
public class Writer<T> implements ItemWriter<String> {
private static final Logger logger = LoggerFactory.getLogger(Writer.class);
private final String paymentType;
private final String type;
public Writer(String paymentType,
String type) {
this.paymentType = paymentType;
this.type = type;
}
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private Client client;
@Override
public void write(List<? extends String> users) throws Exception {
for (String userId : users) {
taskExecutor.execute(() -> {
String currentThreadName = Thread.currentThread()
.getName();
try {
logger.info("action", "repayment_processing_item",
"threadName", currentThreadName,
"userId", userId,
"paymentType", paymentType,
"type", type);
client.makePayment(userId, paymentType, type);
} catch (Exception e) {
logger.error("action", "repayment_failed_to_process_item",
"errorMessage", GeneralUtil.getErrorMessage(e),
"threadName", currentThreadName,
"userId", userId,
"paymentType", paymentType,
"type", type);
}
});
}
}
}