Youtube Cache

Cache YouTube video metadata with built-in rate limiting, TTL management, and batch lookups to prevent API quota exhaustion in Convex apps.

Installation

npm install convex-youtube-cache

About Youtube Cache

**Cache and serve YouTube metadata without hitting API limits.**

Drop-in component that handles the full caching lifecycle so you never worry about quota again.

- TTL-based cache with automatic expiration and refresh
- Built-in rate limiting to stay within YouTube API quotas
- Reservation system to prevent duplicate in-flight requests
- Batch lookups for efficient multi-video queries
- Real-time subscriptions for cache updates

Benefits

Use cases

how to cache YouTube video metadata in Convex without hitting API limits

The convex-youtube-cache component provides automatic caching with configurable TTL and built-in rate limiting to stay within YouTube API quotas. It includes a reservation system that queues requests when quota is exhausted and implements stale-while-revalidate for fast responses.

batch fetch YouTube video data in Convex application

Use the getVideos method to fetch metadata for up to 50 videos in a single API call, optimizing quota usage. The component automatically handles caching, rate limiting, and returns an array of VideoMetadata objects with title, description, thumbnails, and channel information.

implement YouTube API rate limiting in Convex

The convex-youtube-cache component includes built-in rate limiting with configurable daily quotas (default 10,000 units). When quota is exhausted, it creates reservations that wait up to 10 seconds for capacity, then gracefully degrades by returning stale data or null.

prevent duplicate YouTube API requests in Convex

The reservation system prevents duplicate in-flight requests by queuing identical video ID requests. Multiple concurrent requests for the same video will wait for a single API call to complete, reducing quota usage and preventing race conditions.

Frequently asked questions

How does convex-youtube-cache prevent API quota exhaustion?

The convex-youtube-cache component includes built-in rate limiting with configurable daily quotas (default 10,000 units per day). When quota is exhausted, it creates reservations that queue requests and wait up to a configurable timeout (default 10 seconds) before gracefully degrading to return stale cached data or null.

Can I fetch multiple YouTube videos efficiently with this component?

Yes, the convex-youtube-cache component provides a getVideos method that supports batch lookups for up to 50 video IDs in a single YouTube API call. This significantly reduces quota usage compared to individual requests and includes the same caching and rate limiting features.

What happens when cached YouTube data becomes stale?

The convex-youtube-cache component implements a stale-while-revalidate strategy. When cached data exceeds its TTL, it returns the stale data immediately for fast response times, then fetches fresh data from the YouTube API in the background if quota is available.

How do I monitor YouTube API usage with this component?

The convex-youtube-cache component provides a getCacheStats method that returns hit rate, quota usage, and active reservations. You can call this from a Convex query to monitor performance and track how efficiently your app is using the YouTube API quota.

Does convex-youtube-cache handle YouTube API errors and retries?

Yes, the convex-youtube-cache component includes retry logic with exponential backoff for transient HTTP errors. It supports configurable retry attempts (default 3) and base retry delays (default 1 second) to handle temporary YouTube API failures gracefully.

Links