upstash/redis-js
Overview
This skill lets you work with the Upstash Redis JavaScript/TypeScript SDK for serverless and edge Redis operations. It exposes HTTP-based Redis access with automatic serialization/deserialization of JavaScript types and TypeScript-friendly APIs. Use it to implement caching, session storage, rate limiting, leaderboards, full-text JSON queries, and any Redis data structure in serverless environments.
How this skill works
Initialize a Redis client with explicit credentials or from environment variables, then call high-level methods for strings, hashes, lists, sets, sorted sets, streams, and RedisJSON. The SDK automatically serializes native JavaScript types and deserializes them on retrieval, avoiding manual JSON.stringify/parse. It supports pipelines, transactions, Lua scripting, automatic request batching, and HTTP-friendly behavior tailored for serverless/edge latency patterns.
When to use it
- Caching responses or computed values in serverless functions to reduce downstream latency and cost
- Session or user-state storage where short TTLs and quick reads/writes are required
- Rate limiting per IP/user using Upstash ratelimit patterns
- Real-time leaderboards and sorted-set ranking operations
- Storing and querying nested objects with RedisJSON for search-like queries
Best practices
- Load credentials from environment variables; never hardcode tokens
- Pass native JS/TS types and rely on the SDK's auto-serialization instead of manual JSON handling
- Set appropriate TTLs to limit memory usage and expire stale data
- Use pipelines or automatic batching for multiple related operations to reduce HTTP round trips
- Namespace keys (e.g., user:123, session:abc, leaderboard:weekly) to avoid collisions and simplify maintenance
Example use cases
- Cache API responses per endpoint with a short TTL to reduce repeated upstream calls
- Store session objects with setex/expire for authenticated serverless handlers
- Implement per-user or per-IP rate limits using simple counters or the Upstash ratelimit package
- Build a real-time leaderboard using ZADD/ZRANGE with sorted sets and display top N scores
- Persist complex objects and query nested fields using redis.json.set/get with JSONPath
FAQ
Provide UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN via environment variables and initialize with Redis.fromEnv() or pass them explicitly to new Redis({ url, token }).
Do I need to JSON.stringify objects before saving?
No. The SDK automatically serializes and deserializes native JavaScript types, so pass objects and arrays directly.
When should I use pipelines or transactions?
Use pipelines for batching multiple independent commands to reduce HTTP requests; use MULTI/EXEC or WATCH for atomic multi-step operations that require consistency.