部署 Upstash Redis
Serverless Redis,REST API、按请求付费和全球区域。
平台入门
为什么
Upstash 是 serverless Redis,REST API(边缘/Vercel 可用)、按请求付费和多区域。无连接限制 — 完美 serverless。
平台即服务采用率 2024 年增长 24%,团队从虚拟机管理转向 git push 部署。the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research),PaaS 层捕获了大多数新工作负载。
何时使用
Serverless/边缘应用需 Redis(限流、缓存、队列)用 Upstash。高吞吐持久连接避免。
如何做
1. 创建 — upstash.com → 创建 Redis 数据库
2. REST — 获取 UPSTASH_REDIS_REST_URL + TOKEN
3. SDK — @upstash/redis 边缘友好 REST
4. 命令 — redis.set/get 等经 REST
5. 多区域 — 多区域创建,最近读
6. QStash — Redis 上消息队列
7. 限流 — @upstash/ratelimit 滑动窗口
示例
import { Redis } from "@upstash/redis";
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});
await redis.set("key", "value");
const value = await redis.get("key");
// 限流
import { Ratelimit } from "@upstash/ratelimit";
const ratelimit = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(10, "1 m"),
});
const { success } = await ratelimit.limit("user-id");
常见问题
- ⚠REST 比 TCP 慢 — 热路径用 TCP ioredis
- ⚠高 QPS 成本 — 按请求付费累积,监控用量
- ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致
最佳实践
- ✓边缘/serverless 用 REST — TCP 不行的地方可用(Vercel、Workers)
- ✓限流用 @upstash/ratelimit — serverless,应用无状态
- ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求