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

  • uses
    core_product
    “the front end on Next.js on Vercel”
    YouTube
    Verified5 sources· Talk, Job posting· archived
  • feature
    Upstash: Per-request pricing with maximum caps
    “per-request pricing with the serverless model”
    YouTube
    Unverified1 source· Talk
  • feature
    Upstash: Free tier
    “they have a pretty generous free tier”
    YouTube
    Unverified1 source· Talk
  • feature
    Upstash: Serverless Redis deployment and management
    “the easiest way I've found to deploy and manage a Redis instance”
    YouTube
    Unverified1 source· Talk
  • feature
    Upstash: GraphQL API
    “There's also a GraphQL API I can use on top of that”
    YouTube
    Unverified1 source· Talk
  • feature
    Upstash: Durable Redis storage
    “it allows you to use Redis for durable storage”
    YouTube
    Unverified1 source· Talk
  • integrates with
    Vercel integrates with Upstash
    “You'll notice it has an Upstash integration included”
    YouTube
    Unverified1 source· Talk
  • integrates with
    Next.js integrates with Redis
    “connecting to our Redis instance”
    YouTube
    Unverified1 source· Talk
  • feature
    swr: Client-side cache updates and revalidation for real-time-feeling data
    “when you refocus or revalidate the page”
    YouTube
    Unverified1 source· Talk
  • uses
    “we're using this handy library called SWR”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: API routes provide back-end code without a separate Express server
    “API routes allow you to add some back-end code to your Next.js application without having to spin up an entire Express server”
    YouTube
    Unverified1 source· Talk

Related talks