How to Slash API Response Times with Pfinder, JSF Async, and Set Refactoring
This article walks through diagnosing slow API responses, using Pfinder for tracing, refactoring Java collection handling with Sets, and applying JSF asynchronous calls to dramatically cut latency, offering a step‑by‑step guide to backend performance optimization.
Introduction
During project development we often encounter slow API responses, which hurt user experience and system throughput. Improving performance requires comprehensive optimization across code, database, cache, and asynchronous processing. This article shares an optimization journey using Pfinder, JSF async calls, and code refactoring.
Current Diagnosis
UMP diagnostics show a maximum response time of 10 seconds, T99 of 1000 ms, and frequent availability drops. Pfinder diagnostics reveal three main issues:
Looping RPC 120 times = 1441 ms
Database query = 286 ms
Unknown operation > 2000 ms
Problem Location and Performance Optimization
Complete Pfinder Tracing
Integrate the Pfinder SDK:
<!-- 引用 PFinder SDK 库 -->
<dependency>
<groupId>com.jd.pfinder</groupId>
<artifactId>pfinder-profiler-sdk</artifactId>
<version>1.2.2-FINAL</version>
</dependency>Use the @PFTracing annotation to report tracing data.
A code snippet filters duplicate elements from the waveInfos list against sendDPackageCodes, building repeatResult and showPackages. Large collections (waveInfos > 3000, sendDPackageCodes > 7000) cause high latency.
Code Optimization with Set
Replacing list iteration with a Set reduces processing time from 2000 ms to 6 ms.
Resolving RPC Batch Calls with JSF Async
Compare synchronous and asynchronous approaches. Inject an asynchronous bean and configure a JSF consumer with the async="true" attribute.
// Sync bean
@Autowired
private XxxxxApi xxxxApi;
// Async bean
@Autowired
private XxxxxApi xxxxAsyncApi;
// JSF consumer configuration
<jsf:consumer id="xxx" interface="xxx" protocol="jsf" alias="xx" timeout="xxx" retries="0" check="false">
<jsf:method name="methodName" async="true"/>
</jsf:consumer>Use RpcContext to obtain a CompletableFuture:
public CompletableFuture<CommonDto<PageDto>> queryWaybillDetailByBusinessIdByAsync() {
return RpcContext.getContext().asyncCall(() -> xxxxAsyncApi.method());
}Retrieve the result with a timeout:
public <T> T getResultDefaultTimeOut(CompletableFuture<T> future) {
try {
return future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new RuntimeException(e);
}
}This optimization reduces RPC latency from 1400 ms to 200 ms.
Final Results and Unresolved Issues
The before‑and‑after performance improvements are illustrated below.
Conclusion
API performance optimization is a multi‑dimensional process that requires continuous diagnosis and targeted improvements across code, database, cache, and asynchronous processing. By applying the techniques described—Pfinder tracing, Set‑based collection handling, and JSF asynchronous calls—developers can significantly improve response times and enhance user experience.
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.
