Boost API Performance: 12 Proven Backend Optimization Techniques
This article presents a comprehensive set of backend optimization strategies—including batch processing, asynchronous execution, caching, pre‑processing, pooling, parallelism, indexing, transaction management, program refactoring, pagination, SQL tuning, and fine‑grained locking—to dramatically reduce API latency and improve system efficiency.
Background
In legacy projects we often encounter long API response times; this article shares a generic set of optimization strategies.
Summary of API Optimization Techniques
1. Batch Processing
Batch database operations to reduce I/O, e.g., replace per‑record inserts with a single
batchInsertcall.
<code>list.stream().forEach(msg -> { insert(); }); // single insert
batchInsert(); // batch insert</code>2. Asynchronous Execution
Move non‑critical, time‑consuming logic (e.g., accounting and file writing) to asynchronous tasks using thread pools, message queues, or scheduling frameworks.
3. Space‑for‑Time (Caching)
Cache frequently accessed, rarely changed data to avoid repeated DB queries or calculations; be aware of consistency issues.
4. Pre‑processing (Pre‑fetch)
Compute results in advance and store them, so the API can return the ready value directly.
5. Pooling Concept
Reuse expensive resources such as DB connections or threads instead of creating them repeatedly.
6. Serial to Parallel
Execute independent calls concurrently to reduce overall latency.
7. Indexing
Proper indexes dramatically improve query speed; be aware of scenarios where indexes may not be used.
8. Avoid Large Transactions
Long‑running transactions hold DB connections; keep RPC calls and heavy queries outside the transaction.
<code>@Transactional(value="taskTransactionManager", propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, rollbackFor={RuntimeException.class, Exception.class})
public BasicResult purchaseRequest(PurchaseRecord record) {
BasicResult result = new BasicResult();
// ... other DB operations
pushRpc.doPush(record); // move RPC out of transaction
result.setInfo(ResultInfoEnum.SUCCESS);
return result;
}</code>9. Refactor Program Structure
Iterative development can lead to tangled code; restructure to eliminate redundant queries and object creation.
10. Deep Pagination
Using large OFFSET values scans many rows; replace with keyset pagination (e.g., "id > lastId").
<code>SELECT * FROM purchase_record WHERE productCode='PA9044' AND status=4 AND id>100000 LIMIT 200;</code>11. SQL Optimization
Combine indexing, pagination, and selective columns to speed up queries.
12. Fine‑grained Locking
Lock only the minimal critical section; avoid locking non‑shared resources.
<code>// Wrong: lock whole method
synchronized(this){
share();
notShare();
}
// Correct: lock only shared part
synchronized(this){
share();
}</code>Conclusion
Performance problems accumulate over iterations; adopting these mindsets—batching, async, caching, pooling, parallelism, proper indexing, transaction hygiene, and fine‑grained locking—helps reduce latency and cost.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.