Optimizing Large Transactions in Backend Development

The article explains why large database transactions degrade backend API performance, outlines issues such as data inconsistency, lock contention, and log overhead, and presents practical optimization techniques including avoiding RPC inside transactions, using programmatic transactions, batch processing, splitting into smaller transactions, and asynchronous parallel execution with CompletableFuture.

Top Architect
Top Architect
Top Architect
Optimizing Large Transactions in Backend Development

In backend development, complex business logic often results in large database transactions that can significantly slow down API responses as data volume grows.

These "big transactions" cause several problems: concurrent data inconsistency, lock contention leading to time‑outs, heavy undo‑log generation that hurts log query performance, and excessive pressure on the database.

Avoid Remote RPC Calls Inside Transactions – Remote calls without a distributed‑transaction framework can cause inconsistency and make rollback impossible; using asynchronous calls mitigates latency issues.

Programmatic Transactions for Flexibility – Unlike declarative @Transactional, programmatic control lets you limit transaction scope to only update/insert operations. Example:

public Boolean transactionCommit(String userName) {
    // query user
    SysUser sysUser = userMapper.selectUserByUserName(userName, null);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            try {
                if (null != sysUser) {
                    // update status
                    userMapper.updateStatus(userName);
                }
            } catch (Exception e) {
                // rollback
                transactionStatus.setRollbackOnly();
            }
        }
    });
    // query again
    SysUser sysUser1 = userMapper.selectUserByUserName(userName, "1");
    /log/.info("Status 1 user:" + JSON.toJSONString(sysUser1));
    return true;
}

Batch Data Processing – Split large data sets into smaller batches (e.g., 50 records) to reduce transaction size and improve throughput:

List<List<ReceivableFeeSaveDTO>> partition = Lists.partition(receivableFeeSaveDTOList, 50);

Split Large Transactions into Smaller Ones – Decompose a monolithic transaction into multiple focused transactions (e.g., generate receipt, write back amount, call third‑party service) to simplify logic and improve performance.

Asynchronous Parallel Execution – When remote calls are unavoidable, execute them asynchronously and combine results, for example using CompletableFuture:

CompletableFuture<Object> task1 = CompletableFuture.supplyAsync(() -> {
    System.out.println("Check thread" + Thread.currentThread().getId());
    return "Bill info";
}, executor);
CompletableFuture<Object> task2 = CompletableFuture.supplyAsync(() -> {
    System.out.println("Generate receipt thread" + Thread.currentThread().getId());
    try {
        Thread.sleep(3000);
        return "Bill number";
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}, executor);
CompletableFuture<Boolean> future = task1.thenCombineAsync(task2, (t1, t2) -> {
    System.out.println("Write back thread" + Thread.currentThread().getId());
    return true;
}, executor);

By applying these strategies—removing RPC from transactions, using programmatic control, batching data, breaking down large transactions, and leveraging asynchronous processing—developers can significantly improve the efficiency and reliability of backend services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendtransaction
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.