← Back to docs
Recipe

Vercel KV Primer

Serverless Redis, zero cold starts, and how Meridian uses it for session state, rate limiting, and feature flags.

What is Vercel KV?

Vercel KV is a durable Redis-compatible database built on Upstash. It operates over HTTP, which means no persistent TCP connections and no connection pooling headaches inside serverless functions. Reads and writes complete in single-digit milliseconds from edge regions.

Why Meridian chose it

  • @upstash/redis client initializes in ~2ms with an HTTP REST token — no handshake delay.
  • Atomic INCR andEXPIRE give us leaky-bucket rate limiters without external state.
  • Per-route feature flags stored as simple string keys let us ship dark without redeploying.

Pattern: session cache

Meridian validates license keys on every cold start. We cache the result in KV with a 60-second TTL so repeated requests from the same edge region hit memory instead of the licensing backend.

// pages/api/verify.ts
import { Redis } from '@upstash/redis';

const kv = Redis.fromEnv();

export default async function handler(req) {
  const key = req.headers['x-license-key'];
  const cached = await kv.get(`license:${key}`);
  if (cached) return res.status(200).json(cached);

  const result = await verifyAgainstKeyAuth(key);
  await kv.set(`license:${key}`, result, { ex: 60 });
  return res.status(200).json(result);
}

Pitfalls

  • KV is eventually consistent across regions — do not use it for strongly-ordered counters.
  • Maximum value size is 1 MB. Store large blobs in Vercel Blob and keep the URL in KV.
  • Every command is a round-trip. Batch withpipeline() when you need multiple reads.