Highly Available,
Infinitely Scalable
- 99.99% uptime guarantee
- Automatic scaling to meet your demands
- No server management required
const redis = new Redis.fromEnv();
const cacheKey = `item:${itemId}`;
// Check cache
const cachedItem = await redis.get(cacheKey);
if (cachedItem) {
console.log("Cache hit");
return JSON.parse(cachedItem);
}
const getSession = async (key: Key) => {
const sessionId = await getSessionId();
return redis.hget(`s:${sessionId}`, key);
};
const setSession = async (key: Key, value: string) => {
const sessionId = await getSessionIdAndCreateIfMissing();
const sessionKey = `s:${sessionId}`;
await redis.hset(sessionKey, { [key]: value });
return redis.expire(sessionKey, 900);
};
const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(10, "10 s"),
});
const identifier = getIpAddress();
const { success } = await ratelimit.limit(identifier);
if (!success) {
return res.status(429).send("Too many requests");
}
const redis = Redis.fromEnv();
const LEADERBOARD_KEY = "game-leaderboard";
export const updateScore = async (playerName: string, score: number) =>
await redis.zadd(LEADERBOARD_KEY, { score, member: playerName });
export const getTopPlayers = async (top: number) =>
(await redis.zrevrange(LEADERBOARD_KEY, 0, top - 1)).map(
(entry) => ({ player: entry.member, score: entry.score })
);
const redis = Redis.fromEnv();
const CHAT_HISTORY_KEY = (userId: string) => `chat-history:${userId}`;
export const saveMessage = async (userId: string, message: string) => {
await redis.lpush(CHAT_HISTORY_KEY(userId), message); // Add message to history
await redis.ltrim(CHAT_HISTORY_KEY(userId), 0, 99); // Keep only the latest 100 messages
};
export const getChatHistory = async (userId: string) =>
await redis.lrange(CHAT_HISTORY_KEY(userId), 0, -1);
Upstash enables companies of all sizes to create at the moment of inspiration
“We chose Upstash specifically because it offers an HTTP interface for Redis, which perfectly suited our needs”
“The Upstash JS SDK is incredibly easy to use – all I needed to do was run npm install, initialize the Redis instance, and start running redis commands – super simple!”
The platform tailored for the serverless revolution
Data is replicated across 8+ regions worldwide to provide lowest latency for your users. Add/remove regions without any downtime.
Start free, then pay only for what you use with per-request pricing. You'll never pay more than the cap price, guaranteed.
The help you need, when you need it