When a page makes many API calls, users wait for network latency, the server handles extra overhead, and slow calls block rendering. Improve it at several levels. 1. Make independent calls in parallel If calls do not depend on each other, start them together instead of one after another: - Angular: forkJoin({ user: api.user(), orders: api.orders(), stats: api.stats() }) - React or JavaScript: await Promise.all([...]), or parallel useQuery hooks. Total wait becomes roughly the slowest call, not the sum. 2. Reduce the number of calls - Backend for Frontend (BFF) or aggregation endpoint: one endpoint returns exactly the data a page needs (GET /api/dashboard), with the server calling internal services in parallel over fast internal networks. - GraphQL lets the client request several resources in one query, when the organisation supports it. - Remove duplicate requests: several components asking for the same data should share one cached request (TanStack Query, RTK Query, shareReplay in Angular, or a shared service). 3. Cache - Client-side caching of reference data (countries, categories, user profile) for the session. - HTTP caching headers (Cache-Control, ETag) so browsers and CDNs reuse responses. - Server-side caching (Redis, output caching) for expensive aggregations. 4. Load progressively - Render the page shell and the most important data first; load secondary widgets afterwards (lazy loading, @defer in Angular, Suspense in React). - Skeleton loaders so the page feels fast, and independent error handling so one failing widget does not break the page. - Load data for below-the-fold sections only when they become visible. 5. Make each API faster - Optimise slow endpoints (database indexes, projections, avoiding N+1 queries), paginate large lists, compress responses (Brotli or gzip) and return only necessary fields. 6. Use the protocol efficiently - HTTP/2 or HTTP/3 multiplexing reduces the cost of many parallel requests; serve APIs from the same origin or a nearby region to cut latency. 7. Avoid request waterfalls - Start fetching early (route resolvers or loaders) rather than after each component mounts. - When one call depends on another, fetch the dependent data in the backend instead of chaining from the browser. 8. Measure - Use the browser's Network tab and performance traces to see the waterfall, plus server-side tracing to find slow dependencies. Example: "Our dashboard made nine sequential calls taking about 4 seconds. We added a BFF endpoint that fetched the data in parallel on the server and cached reference data, and the page loaded in under 800 ms."
Ravindra Wadile · · 0 likes