Upstash
Upstash is a serverless database with Redis* and Kafka API. Upstash also offers QStash, a task queue/scheduler designed for the serverless.
Database Integrations allow you to connect to a database from your Worker by getting the right configuration from your database provider and adding it as secrets to your Worker. Upstash for Redis
To set up an integration with Upstash:
You need an existing Upstash database to connect to. Create an Upstash database or load data from an existing database to Upstash.
Insert some data to your Upstash database. You can add data to your Upstash database in two ways:
- Use the CLI directly from your Upstash console.
- Alternatively, install redis-cli locally and run the following commands.
➜ set GB "Ey up?"OK➜ set US "Yo, what’s up?"OK➜ set NL "Hoi, hoe gaat het?"OKAdd the Upstash Redis integration to your Worker:
- Log in to the Cloudflare dashboard and select your account.
- In Account Home, select Workers & Pages.
- In Overview, select your Worker.
- Select Settings > Integrations > Upstash Redis.
- Follow the setup flow, selecting the database created in step 1.
In your Worker, install the
@upstash/redis
, a HTTP client to connect to your database and start manipulating data:$ npm install @upstash/redisThe following example shows how to make a query to your Upstash database in a Worker. The credentials needed to connect to Upstash have been automatically added as secrets to your Worker through the integration.
Worker codeimport { Redis } from "@upstash/redis/cloudflare";
export default { async fetch(request, env) { const redis = Redis.fromEnv(env);
const country = request.headers.get("cf-ipcountry"); if (country) { const greeting = await redis.get(country); if (greeting) { return new Response(greeting); } }
return new Response("Hello What's up!"); },
};
To learn more about Upstash, refer to the Upstash documentation.
Upstash Kafka
To set up an integration with Upstash Kafka:
Create a Kafka cluster and topic.
Add the Upstash Kafka integration to your Worker:
- Log in to the Cloudflare dashboard and select your account.
- In Account Home, select Workers & Pages.
- In Overview, select your Worker.
- Select Settings > Integrations > Upstash Kafka.
- Follow the setup flow, selecting the cluster and topic.
In your Worker, install
@upstash/kafka
, a HTTP/REST based Kafka client:$ npm install @upstash/kafkaUse the upstash-kafka JavaScript SDK to send data to Kafka.
Refer to Upstash documentation on Kafka setup with Workers for more information. Replace url
, username
and password
with the variables set by the integration.
Upstash QStash
To set up an integration with Upstash QStash:
Configure the publicly available HTTP endpoint that you want to send your messages to.
Add the Upstash QStash integration to your Worker:
- Log in to the Cloudflare dashboard and select your account.
- In Account Home, select Workers & Pages.
- In Overview, select your Worker.
- Select Integrations > Upstash QStash.
- Follow the setup flow.
In your Worker, install the
@upstash/qstash
, a HTTP client to connect to your database QStash endpoint:$ npm install @upstash/qstashRefer to the Upstash documentation on how to receive webhooks from QStash in your Cloudflare Worker.
* Redis is a trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Upstash is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Upstash.