Menu

1 / 2

Fixing Serverless Timeout Spikes in Next.js When Fetching YouTube Creator Metrics

DEV Community: typescript·Aditya Ekka·3 days ago
#ccCEFK7Z
#devtotypescript#devto#dev#const#await
Reading 0:00
15s threshold

The Problem

When pulling batch metrics for hundreds of YouTube creators inside a Next.js API route running on Vercel, requests frequently hit the 300-second Fluid Compute ceiling—or worse, time out on standard serverless invocations—because sequential HTTP requests to the YouTube Data API block the event loop, causing connection pools to saturate before Neon Postgres can commit the payload.

What I Tried First (and Why It Failed)

My initial approach was straightforward: loop through the array of creator channel IDs using a standard JavaScript for...of loop, awaiting each fetch call sequentially.

// The naive approach that caused 504 Gateway Timeouts for ( const channelId of creatorChannelIds ) { const response = await fetch ( `https://www.googleapis.com/youtube/v3/channels?part=statistics&id= ${ channelId } &key= ${ process . env . YOUTUBE_API_KEY } ` ); const data = await response . json (); await db . query ( `UPDATE creators SET stats = $1 WHERE channel_id = $2` , [ JSON .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.