Common API Performance Optimization Strategies
This article outlines a comprehensive set of backend API performance optimization techniques—including batch processing, asynchronous execution, caching, pre‑processing, pooling, parallelization, indexing, transaction handling, SQL tuning, and lock granularity—to reduce latency and improve system efficiency.
Background : In legacy projects, long‑running API calls often cause performance bottlenecks. The article shares a systematic approach to optimizing these interfaces.
API Optimization Summary
1. Batch Processing
Group database operations to reduce I/O, e.g., insert or update records in bulk after processing.
batchInsert();2. Asynchronous Processing
Offload non‑critical, time‑consuming tasks to asynchronous execution (thread pools, message queues, or scheduling frameworks) to lower request latency.
3. Space‑for‑Time (Caching)
Cache frequently accessed, rarely changed data (R2M, local cache, Memcached, or in‑memory maps) to avoid repeated DB queries or calculations, while being aware of consistency trade‑offs.
4. Pre‑Processing
Pre‑compute results and store them in cache or a dedicated column so that API calls can retrieve ready‑made values instantly.
5. Pooling
Reuse expensive resources such as database connections or threads via connection pools and thread pools to avoid creation overhead.
6. Serial‑to‑Parallel Conversion
Execute independent tasks concurrently when there are no result dependencies, dramatically reducing overall response time.
7. Indexing
Apply appropriate indexes to speed up data retrieval; be aware of scenarios where indexes may not be effective.
8. Avoid Large Transactions
Large, long‑running transactions hold DB connections and degrade performance. Recommendations:
Do not place RPC calls inside transactions.
Keep read‑only queries outside transactions.
Limit the amount of data processed within a transaction.
@Transactional(value = "taskTransactionManager", propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = {RuntimeException.class, Exception.class})
public BasicResult purchaseRequest(PurchaseRecord record) {
BasicResult result = new BasicResult();
// ...
pushRpc.doPush(record);
result.setInfo(ResultInfoEnum.SUCCESS);
return result;
}9. Program Structure Optimization
Refactor code after multiple iterations to eliminate redundant queries and object creations, improving execution order and clarity.
10. Deep Pagination
Use keyset pagination (e.g., based on an auto‑increment ID) instead of offset‑based LIMIT to maintain performance on large page numbers.
select * from purchase_record where productCode = 'PA9044' and status = 4 and id > 100000 limit 20011. SQL Optimization
Combine indexing, proper pagination, and query rewriting to boost query speed.
12. Lock Granularity
Apply locks only to truly shared resources; avoid coarse‑grained locks that block unrelated operations.
// Example of fine‑grained locking
private void notShare() { }
private void share() { }
private int right() {
notShare();
synchronized (this) {
share();
}
}Conclusion
Performance issues usually stem from incremental, unplanned changes during rapid development. By adopting a design‑first mindset and applying the above optimization techniques, developers can significantly reduce latency and improve overall system efficiency.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.