10 Proven Strategies to Supercharge API Performance
This article presents a comprehensive guide to optimizing API latency by covering batch processing, asynchronous execution, caching, pre‑processing, pooling, parallelism, indexing, transaction management, code refactoring, pagination, SQL tuning, and lock granularity, each illustrated with practical code examples and diagrams.
Background: Over the past year many cost‑saving and efficiency projects revealed that excessive API latency is a common bottleneck, prompting a focused effort on interface performance optimization.
1. Batch Processing
Batch database operations reduce repeated I/O by inserting or updating records in bulk after processing completes.
//for loop single insert
list.stream().forEach(msg -> {
insert();
}); //batch insert
batchInsert();2. Asynchronous Processing
Move non‑critical, time‑consuming logic to asynchronous execution using thread pools, message queues, or scheduling frameworks, thereby lowering API response time.
Example: In a finance subscription API, account settlement and file writing can be performed asynchronously because they are not required for the immediate response.
3. Space‑Time Tradeoff (Caching)
Cache frequently accessed, rarely changed data (e.g., using R2M, local cache, Memcached, or a simple Map) to avoid repeated database queries or calculations.
Example: Stock strategy rotation data updates weekly; caching the data and computation results dramatically reduces execution time.
4. Pre‑processing
Pre‑calculate and store results (e.g., annualized return rates derived from net asset values) so that API calls can retrieve ready‑made values without recomputation.
5. Pooling Concept
Reuse expensive resources such as database connections or threads via connection pools and thread pools, reducing creation overhead.
6. Serial to Parallel
Convert sequential dependent calls into parallel independent calls when there is no result dependency, significantly cutting total latency.
7. Indexing
Adding appropriate indexes dramatically improves query efficiency; however, misuse can lead to ineffective indexes, which should be reviewed during iteration.
8. Avoid Large Transactions
Large, long‑running transactions hold database connections and degrade performance. Recommendations:
Do not place RPC calls inside a transaction.
Keep query operations outside the transaction.
Avoid processing excessive data within a single 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();
// 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 image upload task
taskMapper.insert(ManagerParamUtil.buildTask(record, TaskEnum.Task_type.pension_sync.type(), TaskEnum.Sync_bizType.cert.type()));
result.setInfo(ResultInfoEnum.SUCCESS);
return result;
} @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. Optimize Program Structure
Repeated iterations can cause code bloat, redundant queries, and unnecessary object creation. Refactor the entire API, evaluate each code block, and reorder execution to improve efficiency.
10. Deep Pagination Issue
Using LIMIT 100000,200 forces the database to scan 100,200 rows, which is slow. Adopt keyset pagination (e.g., using a continuously increasing primary key) to achieve consistent performance.
select * from purchase_record where productCode = 'PA9044' and status=4 order by orderTime desc limit 100000,200Keyset pagination leverages the primary key index, avoiding large offsets.
11. SQL Optimization
General SQL tuning (indexes, pagination, selective fields) should be applied based on the specific performance hotspots identified.
12. Lock Granularity
Coarse locks degrade concurrency. Use fine‑grained locking only around truly shared resources.
// Incorrect lock (locks non‑shared and shared together)
private void wrong() {
synchronized(this) {
share();
notShare();
}
} // Correct lock (locks only shared resource)
private void right() {
notShare();
synchronized(this) {
share();
}
}3. Causes of Performance Issues
Rapid feature delivery often leads to accumulated technical debt, such as duplicated queries and excessive object creation, which manifest as latency problems.
Adopting a higher‑level design mindset and considering performance from the API designer’s perspective can prevent many of these issues and contribute to cost reduction and efficiency gains.
— End of article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
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.
