Common Interface Performance Optimization Techniques
This article outlines a series of backend interface performance optimization strategies—including batch processing, asynchronous execution, caching, preprocessing, pooling, parallelization, indexing, transaction management, program structure refactoring, deep pagination, SQL tuning, and lock granularity—to help developers reduce latency and improve system efficiency.
Background: While improving an older project, the most frequent issue encountered was excessive interface latency, prompting a dedicated performance optimization effort. This article shares a comprehensive set of backend interface optimization techniques.
1. Batch Processing
Group database operations to reduce I/O by inserting or updating records in bulk after processing completes.
// 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 request response time.
Example: In a financial purchase API, account posting and file generation can be handled asynchronously because the transaction settles on T+1 and real‑time results are not required.
3. Space‑Time Tradeoff (Caching)
Cache frequently accessed, rarely changed data to avoid repeated database queries or calculations. Cache implementations may include Redis, local memory, Memcached, or simple in‑process maps.
Example: Cache weekly‑updated stock rotation data and pre‑computed back‑test results to accelerate queries.
4. Preprocessing
Pre‑compute data (e.g., annualized returns from net values) and store the results in a cache or dedicated table column, allowing instant retrieval during API calls.
5. Pooling
Reuse expensive resources such as database connections or threads via connection pools and thread pools, reducing the overhead of repeated creation and destruction.
6. Serial to Parallel
Execute independent tasks concurrently when there is no data dependency, significantly cutting total execution time. For instance, fetching user account info, product details, and banner data in parallel for a portfolio page.
7. Indexing
Add appropriate indexes to improve query performance; be aware of scenarios where indexes may become ineffective.
8. Avoid Large Transactions
Long‑running transactions hold database connections and degrade overall performance. Mitigation strategies include keeping RPC calls outside transactions, performing reads outside transactions, and limiting 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. Optimize Program Structure
Refactor code after multiple iterations to eliminate redundant queries and object creations, ensuring a clean execution flow.
10. Deep Pagination
Replace offset‑based pagination with primary‑key‑based pagination (e.g., WHERE id > lastId LIMIT 200 ) to maintain performance even on deep pages.
select * from purchase_record where productCode = 'PA9044' and status = 4 and id > 100000 limit 20011. SQL Optimization
Combine indexing, proper pagination, and other techniques to enhance query efficiency.
12. Lock Granularity
Avoid coarse‑grained locks; only lock the minimal critical section needed, whether using synchronized blocks or distributed locks, to prevent unnecessary contention.
// Non‑shared resource
private void notShare() {}
// Shared resource
private void share() {}
private int right() {
notShare();
synchronized (this) {
share();
}
}Final Thoughts
Interface performance problems usually accumulate over successive iterations when quick, additive code changes are made. By adopting a higher‑level design mindset and applying the above techniques, developers can achieve significant cost reductions and efficiency gains.
Bonus: Scan the QR code in the original article to receive a free book on library management systems.
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.