{"id":17392,"library":"upstash-redis-level","title":"Upstash Redis LevelDB Implementation","description":"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.","status":"active","version":"1.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/tinacms/upstash-redis-level","tags":["javascript","level","leveldb","leveldown","levelup","memory","upstash","redis","typescript"],"install":[{"cmd":"npm install upstash-redis-level","lang":"bash","label":"npm"},{"cmd":"yarn add upstash-redis-level","lang":"bash","label":"yarn"},{"cmd":"pnpm add upstash-redis-level","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core Redis client for serverless and edge environments.","package":"@upstash/redis","optional":false},{"reason":"Provides the abstract API interface for the LevelDB-like database.","package":"abstract-level","optional":false}],"imports":[{"note":"Prefer ESM imports for modern Node.js and client-side environments. CommonJS `require` is also supported but less idiomatic for new projects.","wrong":"const { RedisLevel } = require('upstash-redis-level');","symbol":"RedisLevel","correct":"import { RedisLevel } from 'upstash-redis-level';"},{"note":"The underlying Upstash Redis client is a peer dependency and must be imported and instantiated separately. It powers the connection to the Upstash service.","wrong":"const { Redis } = require('@upstash/redis');","symbol":"Redis","correct":"import { Redis } from '@upstash/redis';"},{"note":"Type import for configuration options when initializing RedisLevel. Essential for TypeScript projects.","symbol":"RedisLevelOptions","correct":"import type { RedisLevelOptions } from 'upstash-redis-level';"}],"quickstart":{"code":"import { RedisLevel } from 'upstash-redis-level';\nimport { Redis } from '@upstash/redis';\n\nconst UPSTASH_REDIS_REST_URL = process.env.KV_REST_API_URL ?? 'http://localhost:8079';\nconst UPSTASH_REDIS_REST_TOKEN = process.env.KV_REST_API_TOKEN ?? 'example_token';\n\nasync function runRedisLevelExample() {\n  if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {\n    console.error('Missing UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN environment variables.');\n    return;\n  }\n\n  // 1. Initialize the Upstash Redis client\n  const redisClient = new Redis({\n    url: UPSTASH_REDIS_REST_URL,\n    token: UPSTASH_REDIS_REST_TOKEN,\n  });\n\n  // 2. Initialize the RedisLevel database\n  const db = new RedisLevel({\n    redis: redisClient,\n    namespace: 'my-app-data',\n    debug: true,\n  });\n\n  try {\n    // 3. Perform basic operations\n    await db.put('user:1', JSON.stringify({ name: 'Alice', age: 30 }));\n    console.log('Put user:1');\n\n    const user1 = await db.get('user:1');\n    console.log('Got user:1:', user1); // Should output '{\"name\":\"Alice\",\"age\":30}'\n\n    await db.put('user:2', JSON.stringify({ name: 'Bob', age: 25 }));\n\n    // 4. Iterate over entries\n    console.log('\\nIterating through users:');\n    for await (const [key, value] of db.iterator({ gte: 'user:' })) {\n      console.log(`${key}: ${value}`);\n    }\n\n    // 5. Delete an entry\n    await db.del('user:1');\n    console.log('\\nDeleted user:1');\n\n    const user1AfterDelete = await db.get('user:1');\n    console.log('Got user:1 after delete:', user1AfterDelete); // Should be undefined\n\n  } catch (error) {\n    console.error('Error during RedisLevel operations:', error);\n  } finally {\n    // In real serverless functions, connections are often managed by the runtime.\n    // Explicitly quitting might not be necessary or even desirable in some environments.\n    // However, for local scripts, it's good practice to close connections.\n    // (Note: @upstash/redis often manages its HTTP connections implicitly)\n    // await redisClient.quit(); // @upstash/redis doesn't have a 'quit' method in this context.\n  }\n}\n\nrunRedisLevelExample();","lang":"typescript","description":"This quickstart demonstrates how to initialize `upstash-redis-level` with an `@upstash/redis` client and perform basic `put`, `get`, and iteration operations. It highlights using environment variables for Redis credentials and the `namespace` option."},"warnings":[{"fix":"Ensure you are correctly initializing `@upstash/redis` with `url` and `token` environment variables. The HTTP-based nature of `@upstash/redis` is designed to intrinsically handle transient connections in serverless functions without explicit connection management like `quit()`.","message":"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.","severity":"gotcha","affected_versions":"All versions (when using traditional Redis clients, not @upstash/redis)"},{"fix":"Handle large numbers as strings in your application logic. If strict numeric deserialization is required for numbers that fit within JavaScript's safe integer range, you may need to explicitly parse them or configure `responseEncoding: false` on the `Redis` client if applicable, though this is generally not recommended unless you understand the implications.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For Node.js < v18, install a polyfill like `isomorphic-fetch` (`npm install isomorphic-fetch`) and import it at the top of your entry file: `import 'isomorphic-fetch';`","message":"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`.","severity":"gotcha","affected_versions":"<=1.x.x"},{"fix":"Review Upstash documentation for updated recommendations. For strong consistency, Upstash improved replication for Read-Your-Writes consistency. For GraphQL, use the REST API directly.","message":"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.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Install `isomorphic-fetch` (`npm i isomorphic-fetch`) and add `import 'isomorphic-fetch';` at the top of your application's entry point.","cause":"Running on Node.js versions older than 18 without a `fetch` polyfill, as `@upstash/redis` uses the Web Fetch API.","error":"ReferenceError: fetch is not defined"},{"fix":"Ensure 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.","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.","error":"ERR max concurrent connections exceeded"},{"fix":"Double-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.","cause":"The `@upstash/redis` client or the `RedisLevel` instance is not correctly authenticated with the Upstash Redis service.","error":"NOAUTH Authentication required."},{"fix":"If 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.","cause":"By default, `@upstash/redis` can base64 encode responses from the server to prevent deserialization issues with certain data types.","error":"Hashed response when using redis.get() or similar."}],"ecosystem":"npm","meta_description":null}