Menu

Sequential vs Parallel Fetching

Lesson 2: Sequential vs Parallel Fetching

What is a waterfall?

A waterfall happens when each request waits for the previous one to finish, even though they do not depend on each other.

Pattern Total time (requests of 300 ms, 400 ms, 500 ms)
Sequential (waterfall) 300 + 400 + 500 = 1,200 ms
Parallel max(300, 400, 500) = 500 ms

Example

Slow, sequential:

const course = await getCourse(slug);        // 300 ms
const instructor = await getInstructor(slug); // 400 ms
const reviews = await getReviews(slug);       // 500 ms

Fast, parallel:

const [course, instructor, reviews] = await Promise.all([
  getCourse(slug),
  getInstructor(slug),
  getReviews(slug),
]);

When one request does depend on another, keep that pair sequential and parallelise the rest:

const course = await getCourse(slug);
const [instructor, related] = await Promise.all([
  getInstructor(course.instructorId), // needs course
  getRelatedCourses(course.category), // needs course
]);

How it works

  • Promise.all starts every request at once and waits for all of them.
  • If any request fails, Promise.all rejects. Use Promise.allSettled when partial results are acceptable.
  • Better still for UX: put each independent section in its own component inside <Suspense>. Each one fetches its own data and streams in when ready (Module 3).

Deduplication with React **cache()**: if generateMetadata, the page, and a component all need the same course, wrap the function so it runs once per request:

// lib/courses.ts
import { cache } from 'react';

export const getCourse = cache(async (slug: string) => {
  return db.course.findUnique({ where: { slug } });
});

Common mistakes

Mistake Effect Fix
await in a loop (for (const id of ids) await get(id)) N sequential requests await Promise.all(ids.map(get))
Passing data down through many props to avoid refetching Tightly coupled components Fetch where needed; use cache() to deduplicate
Fetching in a parent layout "to share" with pages Layout waits; data goes stale Fetch in each component that needs it

Key takeaways

  • Start independent requests together with Promise.all.
  • Use cache() to fetch once per request, even when called in several places.
  • Combine parallel fetching with Suspense for the best perceived speed.

Try it: Rewrite a course page that makes three sequential requests so it uses Promise.all, and log the time saved.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details