WhoBuilt

Using Serverless Redis with Next.js

Mar 26, 2021· 10 min· 25K views·Watch on YouTube

Lee Robinson of leerob demonstrates how to build and deploy a serverless Redis-backed product roadmap with Next.js and Upstash.

Redis as an application data store

Redis is often introduced as an in-memory cache, but it also supports data structures such as hash maps, counters, sorted lists, and queues. Lee uses it as the persistent store for a product roadmap where users can submit and upvote features.

Deploying the app and Redis

The example runs Next.js on Vercel and Redis on Upstash, with both services available on free tiers. Lee describes Upstash’s per-request pricing, spending caps, monitoring console, and GraphQL API. Vercel’s Upstash integration connects the services and adds the needed environment variables during deployment; the example deployment takes about 45 seconds.

Loading and updating roadmap data

The app connects to Redis through ioredis, configured with a connection string in an environment variable. Its homepage reads roadmap features in `getServerSideProps`, sorting them by score and then title. SWR starts with that server-fetched data and can revalidate it so updates, such as other users’ votes, appear without a full page refresh.

Creating features and subscriptions

A feature form sends a POST request to a Next.js API route, which validates the title, limits its length, creates a UUID and other fields, and stores the feature in Redis. SWR updates the page cache immediately after submission. A separate form handles email subscriptions, while native HTML attributes provide required-field, email-type, autocomplete, and maximum-length validation.

Takeaways

  • Redis can serve as a persistent application store, not only as a cache.
  • Use managed serverless Redis and request-based pricing for a small app that may have variable traffic.
  • Keep secrets in environment variables and connect services through deployment integrations where available.
  • Combine server-rendered initial data with SWR revalidation for a page that feels current.
  • Use Next.js API routes for lightweight backend operations, and native HTML validation for straightforward form checks.

Mentioned in this talk

Search inside this talk

What was claimed 11

Show all 11 facts

Related talks

Using Serverless Redis with Next.js · WhoBuilt