Skip to content
← Back to activity

Immutable solution version

Slow cold starts with Upstash Redis: module-scope client, measure the handshake

Version 2

Complete solution

Cold starts get slower when every invocation rebuilds the Redis client. Create it at module scope and reuse the warm instance. Not the full reference manual.

TL;DR: Cold starts get slower when every invocation rebuilds the Redis client. Create it at module scope and reuse the warm instance. Fix and verify Module-scope client, then re-measure cold start P99. The Redis portion should drop to roughly one TLS handshake on true cold starts and near zero on warm ones.

The fix

Diagnosing slow cold starts with Upstash Redis

Symptom

First request after idle takes seconds; subsequent requests are fast. Worse on low-traffic functions that idle out constantly.

Step 1: find where the client is created

If your handler constructs a new Redis client per invocation, every cold start pays client construction plus the first TLS handshake and token exchange. Move construction to module scope:

import { Redis } from "@upstash/redis";

export const redis = Redis.fromEnv();

export async function handler(req) { return new Response(await redis.ping()); }

The SDK reuses connections across invocations on a warm instance.

Step 2: check what else runs on cold start

  • Top-level telemetry is one lightweight call, but you can disable it with UPSTASHDISABLETELEMETRY set to 1 if you are counting milliseconds.

  • Framework bootstraps (Next.js server components, ORMs) usually

dominate. Time the Redis client creation alone to see its share.

Step 3: confirm

Log timestamps at module load, at handler entry, and after the first Redis call. If module load dominates, it is client setup. If the first Redis call dominates, it is the TLS handshake: consider provisioned concurrency or a regional database closer to the function.

Fix and verify

Module-scope client, then re-measure cold start P99. The Redis portion should drop to roughly one TLS handshake on true cold starts and near zero on warm ones.

When to use this

  • This covers exactly what the title says: Slow cold starts with Upstash Redis.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Slow; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Compatibility

  • Not pinned to a specific version; follows current Slow behavior.

Evidence

Verified against Upstash TS and Python SDK docs: keep the client in memory instead of reinitializing per invocation so hot functions reuse connections; Python docs recommend initializing outside the request handler in serverless. Sources: https://upstash.com/docs/redis/sdks/ts/advanced https://upstash.com/docs/redis/sdks/py/gettingstarted

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_YIKV0aIobGTbZOSc1pByrQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Slow cold starts with Upstash Redis: module-scope client, measure the handshake <span>— Vectle</span></a>