Upstash Redis LevelDB Implementation
upstash-redis-level is an `abstract-level` database implementation that utilizes @upstash/redis as its underlying data store. The `abstract-level` interface provides a common API for lexicographically sorted key-value databases, featuring support for encodings, sublevels, events, and hooks, enabling developers to swap out storage backends easily. This package, currently at version 1.1.1, integrates with Upstash's serverless, low-latency, HTTP/REST-based Redis offering, making it particularly well-suited for edge and serverless environments like Vercel, Cloudflare Workers, and Next.js where traditional TCP-based Redis connections are often suboptimal or unavailable. It differentiates itself by leveraging the connectionless nature of @upstash/redis to avoid common issues like connection limits in ephemeral environments.
Common errors
-
ReferenceError: fetch is not defined
cause Running on Node.js versions older than 18 without a `fetch` polyfill, as `@upstash/redis` uses the Web Fetch API.fixInstall `isomorphic-fetch` (`npm i isomorphic-fetch`) and add `import 'isomorphic-fetch';` at the top of your application's entry point. -
ERR max concurrent connections exceeded
cause While less likely with `@upstash/redis` due to its HTTP nature, if an underlying non-Upstash Redis client or an improperly configured `@upstash/redis` instance is used in a high-concurrency serverless environment, connection limits can be hit.fixEnsure you are using the HTTP-based `@upstash/redis` client correctly, which handles connections efficiently for serverless. Verify that `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` are correctly set. -
NOAUTH Authentication required.
cause The `@upstash/redis` client or the `RedisLevel` instance is not correctly authenticated with the Upstash Redis service.fixDouble-check that `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` environment variables are correctly set and correspond to your Upstash Redis database. These are crucial for authentication. -
Hashed response when using redis.get() or similar.
cause By default, `@upstash/redis` can base64 encode responses from the server to prevent deserialization issues with certain data types.fixIf you are certain your data is always valid JSON and you wish to disable this behavior, you can set `responseEncoding: false` in the `Redis` client options: `new Redis({ ..., responseEncoding: false })`. However, this is usually not necessary with recent versions.
Warnings
- gotcha Traditional TCP-based Redis clients can face 'ERR max concurrent connections exceeded' errors in serverless environments due to connection limits and cold starts. `upstash-redis-level`, by using `@upstash/redis`, leverages an HTTP/REST API to mitigate these issues by being connectionless.
- gotcha When using `@upstash/redis` (the underlying client), large numbers (exceeding `Number.MAX_SAFE_INTEGER`) may be returned as strings instead of numbers. This is a JavaScript limitation and a design choice in `@upstash/redis` to prevent silent data corruption.
- gotcha In Node.js environments prior to version 18, the `fetch` API is not natively available. Since `@upstash/redis` relies on `fetch`, this can lead to `ReferenceError: fetch is not defined`.
- deprecated Upstash Redis, the backing service, deprecated its strong consistency mode (March 2022) and GraphQL API (October 2022). While not directly affecting `upstash-redis-level`'s API, applications relying on these underlying Redis features might experience changes or require refactoring.
Install
-
npm install upstash-redis-level -
yarn add upstash-redis-level -
pnpm add upstash-redis-level
Imports
- RedisLevel
const { RedisLevel } = require('upstash-redis-level');import { RedisLevel } from 'upstash-redis-level'; - Redis
const { Redis } = require('@upstash/redis');import { Redis } from '@upstash/redis'; - RedisLevelOptions
import type { RedisLevelOptions } from 'upstash-redis-level';
Quickstart
import { RedisLevel } from 'upstash-redis-level';
import { Redis } from '@upstash/redis';
const UPSTASH_REDIS_REST_URL = process.env.KV_REST_API_URL ?? 'http://localhost:8079';
const UPSTASH_REDIS_REST_TOKEN = process.env.KV_REST_API_TOKEN ?? 'example_token';
async function runRedisLevelExample() {
if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
console.error('Missing UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN environment variables.');
return;
}
// 1. Initialize the Upstash Redis client
const redisClient = new Redis({
url: UPSTASH_REDIS_REST_URL,
token: UPSTASH_REDIS_REST_TOKEN,
});
// 2. Initialize the RedisLevel database
const db = new RedisLevel({
redis: redisClient,
namespace: 'my-app-data',
debug: true,
});
try {
// 3. Perform basic operations
await db.put('user:1', JSON.stringify({ name: 'Alice', age: 30 }));
console.log('Put user:1');
const user1 = await db.get('user:1');
console.log('Got user:1:', user1); // Should output '{"name":"Alice","age":30}'
await db.put('user:2', JSON.stringify({ name: 'Bob', age: 25 }));
// 4. Iterate over entries
console.log('\nIterating through users:');
for await (const [key, value] of db.iterator({ gte: 'user:' })) {
console.log(`${key}: ${value}`);
}
// 5. Delete an entry
await db.del('user:1');
console.log('\nDeleted user:1');
const user1AfterDelete = await db.get('user:1');
console.log('Got user:1 after delete:', user1AfterDelete); // Should be undefined
} catch (error) {
console.error('Error during RedisLevel operations:', error);
} finally {
// In real serverless functions, connections are often managed by the runtime.
// Explicitly quitting might not be necessary or even desirable in some environments.
// However, for local scripts, it's good practice to close connections.
// (Note: @upstash/redis often manages its HTTP connections implicitly)
// await redisClient.quit(); // @upstash/redis doesn't have a 'quit' method in this context.
}
}
runRedisLevelExample();