Boost API Performance: 11 Proven Backend Optimization Techniques
This article presents a comprehensive set of backend API optimization strategies—including batch processing, asynchronous execution, caching, pre‑processing, pooling, parallelization, indexing, transaction management, code restructuring, deep pagination, SQL tuning, and lock granularity—to dramatically reduce response times and improve system efficiency.
1. Background
Legacy projects often suffer from excessive API latency, prompting a focused effort on performance optimization.
2. Summary of API Optimization Solutions
1. Batch Processing
Group database operations to reduce I/O by inserting or updating records in bulk after processing.
<code>//for loop single insert
list.stream().forEach(msg->{
insert();
});
//batch insert
batchInsert();
</code>2. Asynchronous Processing
Move non‑critical, time‑consuming logic to asynchronous execution to lower API latency.
For example, in a financial purchase API, accounting and file‑writing can be handled asynchronously because they are not required for the immediate response.
Implementation can use thread pools, message queues, or scheduling frameworks.
3. Space‑for‑Time (Caching)
Cache frequently accessed, rarely changed data to avoid repeated database queries or calculations, while being mindful of consistency issues.
Caching can be implemented with Redis, local caches, Memcached, or simple maps.
4. Pre‑Processing
Pre‑compute data and store it in cache or dedicated fields so that API calls can retrieve ready‑made results, such as pre‑calculating annualized returns for financial products.
5. Pooling
Reuse resources like database connections and threads through pools to avoid the overhead of repeated creation and destruction.
6. Serial to Parallel
Execute independent tasks concurrently when there are no result dependencies, e.g., fetching user account, product, and banner data in parallel for a portfolio page.
7. Indexing
Proper indexing dramatically improves query efficiency; the article notes common scenarios where indexes may not be effective.
8. Avoid Large Transactions
Long‑running transactions hold database connections, causing contention. Example code shows a transactional method that performs multiple inserts and a push RPC call, which can lead to deadlocks and timeouts.
<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();
// insert tasks
taskMapper.insert(...);
// ... other inserts
result.setInfo(ResultInfoEnum.SUCCESS);
return result;
}
</code>Mitigation strategies: keep RPC calls out of transactions, place queries outside transactions, and limit data processed within a transaction.
9. Optimize Program Structure
Refactor accumulated code after multiple iterations to eliminate redundant queries and object creations.
10. Deep Pagination
Using
LIMIT offset, countscans many rows; replace with keyset pagination (e.g.,
WHERE id > last_id LIMIT count) to leverage primary‑key indexes.
<code>SELECT * FROM purchase_record WHERE productCode='PA9044' AND status=4 AND id > 100000 LIMIT 200;
</code>11. SQL Optimization
Combine indexing, pagination, and other SQL tuning techniques to improve query performance.
12. Lock Granularity
Apply locks only to truly shared resources; avoid coarse‑grained locks that block unrelated operations.
<code>// Incorrect locking
private void wrong() {
synchronized(this) {
share();
notShare();
}
}
// Correct locking
private void right() {
notShare();
synchronized(this) {
share();
}
}
</code>3. Conclusion
API performance issues often accumulate over iterative development; adopting higher‑level design thinking and these optimization techniques can significantly reduce latency and improve cost efficiency.
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.