Skip to main content

Advanced

Disable automatic serialization

Your data is (de)serialized as json by default. This works for most use cases but you can disable it if you want:

const redis = new Redis({
// ...
automaticDeserialization: false,
});

// or
const redis = Redis.fromEnv({
automaticDeserialization: false,
});

This probably breaks quite a few types, but it's a first step in that direction. Please report bugs and broken types here.

keepalive

@upstash/redis is capable of reusing connections where possible to minimize latency. Connections can be reused if the client is stored in memory and not initialized with every new function invocation. The easiest way to achieve this is by creating the client outside of your handler and adding an https agent:

// Nextjs api route
import { Redis } from "@upstash/redis";
import https from "https";

const redis = Redis.fromEnv({
agent: new https.Agent({ keepAlive: true }),
});

export default async function (req, res) {
// use redis here
}

Whenever your hot lambda receives a new request the client is already initialized and the previously established connection to upstash is reused.

Javascript MAX_SAFE_INTEGER

Javascript can not handle numbers larger than 2^53 -1 safely and would return wrong results when trying to deserialize them. In these cases the default deserializer will return them as string instead. This might cause a mismatch with your custom types.

await redis.set("key", "101600000000150081467");
const res = await redis<number>("get");

In this example res will still be a string despite the type annotation. Please keep that in mind and adjust accordingly.