Boost API Performance: 12 Proven Techniques to Slash Latency

This article shares a comprehensive, step‑by‑step guide to optimizing API performance, covering batch processing, asynchronous execution, caching, pre‑processing, pooling, parallelism, indexing, transaction management, deep pagination, SQL tuning, lock granularity, and code structure refactoring, all illustrated with Java examples.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Boost API Performance: 12 Proven Techniques to Slash Latency

1. Batch Processing

Batch processing means performing database operations in bulk. In loops that insert records one by one, you can replace the per‑iteration inserts with a single batch insert to reduce I/O overhead.

// for loop single insert
list.stream().forEach(msg -> {
    insert();
});

// batch insert
batchInsert();

2. Asynchronous Processing

Asynchronous processing moves time‑consuming, non‑essential logic to background execution, lowering request latency. For example, in a financial purchase API, accounting and file‑writing can be handled asynchronously using thread pools, message queues, or scheduling frameworks.

3. Space‑for‑Time (Caching)

Using caches for frequently accessed, rarely changed data avoids repeated database queries or calculations. Choose appropriate cache types such as Redis, Memcached, local maps, or R2M, while being aware of consistency trade‑offs.

4. Pre‑Processing

Pre‑processing (pre‑fetch) calculates or stores results ahead of time, so the API can retrieve ready‑made data, dramatically improving response speed. For instance, pre‑compute annualized returns for financial products and store them for instant access.

5. Pooling

Pooling reuses expensive resources like database connections or threads, eliminating the cost of repeated creation and destruction. The core principle is pre‑allocation and cyclic reuse.

6. Serial to Parallel

Convert sequential operations into parallel ones when there are no result dependencies. Parallel execution reduces total latency, as illustrated by fetching user account, product, and banner data concurrently for a portfolio page.

7. Indexing

Adding appropriate indexes dramatically speeds up data retrieval. The article notes common scenarios where indexes may not be effective and suggests reviewing them during iteration.

8. Avoid Large Transactions

Long‑running transactions hold database connections, causing contention. Example code shows a purchase request that performs multiple inserts within a single transaction; adding non‑DB operations like RPC calls inside the transaction can exacerbate the problem.

@Transactional(value = "taskTransactionManager", propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = {RuntimeException.class, Exception.class})
public BasicResult purchaseRequest(PurchaseRecord record) {
    BasicResult result = new BasicResult();
    // insert account task
    taskMapper.insert(ManagerParamUtil.buildTask(record, TaskEnum.Task_type.pension_account.type(), TaskEnum.Account_bizType.purchase_request.type()));
    // insert sync task
    taskMapper.insert(ManagerParamUtil.buildTask(record, TaskEnum.Task_type.pension_sync.type(), TaskEnum.Sync_bizType.purchase.type()));
    // insert file upload task
    taskMapper.insert(ManagerParamUtil.buildTask(record, TaskEnum.Task_type.pension_sync.type(), TaskEnum.Sync_bizType.cert.type()));
    result.setInfo(ResultInfoEnum.SUCCESS);
    return result;
}

To mitigate large‑transaction issues:

Do not place RPC calls inside the transaction.

Keep read‑only queries outside the transaction.

Avoid processing excessive data within a single transaction.

9. Optimize Program Structure

Repeated iterations can lead to tangled code with redundant queries and object creations. Refactor the API codebase, evaluate each block’s purpose, and reorder execution to improve efficiency.

10. Deep Pagination

Using LIMIT offset, count scans unnecessary rows. Replace it with keyset pagination (e.g., WHERE id > last_id LIMIT count) to leverage primary‑key indexes.

select * from purchase_record where productCode = 'PA9044' and status = 4 limit 100000,200;
select * from purchase_record where productCode = 'PA9044' and status = 4 and id > 100000 limit 200;

11. SQL Optimization

SQL tuning—using proper indexes, efficient pagination, and selective column retrieval—significantly boosts query performance.

12. Lock Granularity

Coarse‑grained locks (e.g., synchronizing entire services) degrade performance. Lock only the minimal critical section, similar to locking just a bathroom door instead of the whole house.

// Incorrect locking
synchronized(this) {
    share();
    notShare();
}

// Correct locking
notShare();
synchronized(this) {
    share();
}

Conclusion

API performance issues often accumulate over multiple iterations when quick fixes lead to code bloat. By adopting a higher‑level design mindset—considering batching, async execution, caching, indexing, transaction size, pagination, and lock granularity—developers can achieve substantial cost and efficiency gains.

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.

Backendjavaperformance optimizationAPI
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.