You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Workers KV can be used as a persistent, single, global cache accessible from Cloudflare Workers to speed up your application.
16
-
Data cached in Workers KV will be accessible from all other Cloudflare locations as well and will be persisted until expiry or deletion.
16
+
Data cached in Workers KV is accessible from all other Cloudflare locations as well, and persists until expiry or deletion.
17
17
18
18
After fetching data from external resources in your Workers application, you can write the data to Workers KV.
19
19
On subsequent Worker requests (in the same region or in other regions), you can read the cached data from Workers KV instead of calling the external API.
20
-
This improves your Worker application's performance and resilience while reducing load on the external resource.
20
+
This improves your Worker application's performance and resilience while reducing load on external resources.
21
21
22
22
This example shows how you can cache data in Workers KV and read cached data from Workers KV in a Worker application.
Storing application configuration data is an ideal use case for Workers KV. Configuration data can include data to personalize an application for each user or tenant, enable features for user groups, restrict access with allow-lists/deny-lists, etc. These use-cases can have high read volumes that are highly cacheable by Workers KV, which can ensure low-latency reads from your Workers application.
16
16
17
-
In this example, application configuration data is used to personalize the Workers application for each user. The configuration data is stored in an external application and database and written to Workers KV using the REST API.
17
+
In this example, application configuration data is used to personalize the Workers application for each user. The configuration data is stored in an external application and database, and written to Workers KV using the REST API.
18
18
19
19
## Write your configuration from your external application to Workers KV
20
20
@@ -221,19 +221,15 @@ export default {
221
221
</TabItem>
222
222
</Tabs>
223
223
224
-
This code will use the path within the URL and find the file associated to the path within the KV store. It also sets the proper MIME type in the response to indicate to the browser how to handle the response. To retrieve the value from the KV store, this code uses `arrayBuffer` to properly handle binary data such as images, documents, and video/audio files.
224
+
This code will use the path within the URL and find the file associated to the path within the KV store. It also sets the proper MIME type in the response to inform the browser how to handle the response. To retrieve the value from the KV store, this code uses `arrayBuffer` to properly handle binary data such as images, documents, and video/audio files.
225
225
226
-
## Optimizing performance for configuration
226
+
## Optimize performance for configuration
227
227
228
-
To optimize performance, you may opt to consolidate values in fewer key-value pairs. By doing so, you may
229
-
benefit from higher caching efficiency and lower latency.
228
+
To optimize performance, you may opt to consolidate values in fewer key-value pairs. By doing so, you may benefit from higher caching efficiency and lower latency.
230
229
231
-
For example, instead of storing each user's configuration
232
-
in a separate key-value pair, you may store all users' configurations in a single key-value pair. This approach
233
-
may be suitable for use-cases where the configuration data is small and can be easily managed in a single key-value pair
234
-
(the [size limit for a Workers KV value is 25 MiB](/kv/platform/limits/)).
230
+
For example, instead of storing each user's configuration in a separate key-value pair, you may store all users' configurations in a single key-value pair. This approach may be suitable for use-cases where the configuration data is small and can be easily managed in a single key-value pair (the [size limit for a Workers KV value is 25 MiB](/kv/platform/limits/)).
235
231
236
232
## Related resources
237
233
238
-
- [Rust support in Workers](/workers/languages/rust/).
239
-
- [Using KV in Workers](/kv/get-started/).
234
+
- [Rust support in Workers](/workers/languages/rust/)
Using Workers KV to store routing data to route requests across various web servers with Workers is an ideal use case for Workers KV.
16
-
Routing workloads can have high read volume and Workers KV's low-latency reads can help ensure that routing decisions are made quickly and efficiently.
15
+
Using Workers KV to store routing data to route requests across various web servers with Workers is an ideal use case for Workers KV. Routing workloads can have high read volume, and Workers KV's low-latency reads can help ensure that routing decisions are made quickly and efficiently.
17
16
18
17
Routing can be helpful to route requests coming into a single Cloudflare Worker application to different web servers based on the request's path, hostname, or other request attributes.
19
-
In single-tenant applications, this can be used to route requests to various origin servers based on the business domain (ex: requests to `/admin` routed to administration server, `/store` routed to storefront server, `/api` routed to the API server).
20
-
In multi-tenant applications, requests can be routed to the tenant's respective origin resources (ex: requests to `tenantA.your-worker-hostname.com` routed to server for Tenant A, `tenantB.your-worker-hostname.com` routed to server for Tenant B).
18
+
19
+
In single-tenant applications, this can be used to route requests to various origin servers based on the business domain (for example, requests to `/admin` routed to administration server, `/store` routed to storefront server, `/api` routed to the API server).
20
+
21
+
In multi-tenant applications, requests can be routed to the tenant's respective origin resources (for example, requests to `tenantA.your-worker-hostname.com` routed to server for Tenant A, `tenantB.your-worker-hostname.com` routed to server for Tenant B).
21
22
22
23
Routing can also be used to implement [A/B testing](/reference-architecture/diagrams/serverless/a-b-testing-using-workers/), canary deployments, or blue-green deployments for your own external applications.
23
24
If you are looking to implement canary or blue-green deployments of applications built fully on Cloudflare Workers, see [Workers gradual deployments](/workers/configuration/versions-and-deployments/gradual-deployments/).
24
25
25
-
## Routing requests with Workers KV
26
+
## Route requests with Workers KV
26
27
27
28
In this example, a multi-tenant e-Commerce application is built on Cloudflare Workers. Each storefront is a different tenant and has its own external web server.
28
29
Our Cloudflare Worker is responsible for receiving all requests for all storefronts and routing requests to the correct origin web server according to the storefront ID.
Copy file name to clipboardExpand all lines: src/content/docs/kv/examples/workers-kv-to-serve-assets.mdx
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,11 @@ With [Workers KV](/kv), you can access, edit and store assets directly from your
20
20
21
21
## Write static assets to Workers KV using Wrangler
22
22
23
-
To store static assets in Workers KV, you can use the [Wrangler CLI](/workers/wrangler/) (commonly used during development), the [Workers KV binding](/kv/concepts/kv-bindings/) from a Workers application, or the [Workers KV REST API](/api/resources/kv/subresources/namespaces/methods/list/) (commonly used to access Workers KV from an external application). We'll demonstrate how to use the Wrangler CLI.
23
+
To store static assets in Workers KV, you can use the [Wrangler CLI](/workers/wrangler/) (commonly used during development), the [Workers KV binding](/kv/concepts/kv-bindings/) from a Workers application, or the [Workers KV REST API](/api/resources/kv/subresources/namespaces/methods/list/) (commonly used to access Workers KV from an external application). We will demonstrate how to use the Wrangler CLI.
24
24
25
-
For this scenario, we'll be storing a sample HTML file within our Workers KV store. Create a new file `index.html` with the following content:
25
+
For this scenario, we will store a sample HTML file within our Workers KV store.
26
+
27
+
Create a new file `index.html` with the following content:
26
28
27
29
```html title="index.html"
28
30
Hello World!
@@ -118,7 +120,7 @@ export default {
118
120
</TabItem>
119
121
</Tabs>
120
122
121
-
This code parses the key name for the key-value pair to fetch from the HTTP request. Then, it determines the proper MIME type for the response to indicate to the browser how to handle the response.
123
+
This code parses the key name for the key-value pair to fetch from the HTTP request. Then, it determines the proper MIME type for the response to inform the browser how to handle the response.
122
124
To retrieve the value from the KV store, this code uses `arrayBuffer` to properly handle binary data such as images, documents, and video/audio files.
123
125
124
126
Given a sample key-value pair with key `index.html` with value containing some HTML content in our Workers KV namespace store, we can access our Workers application
@@ -130,7 +132,7 @@ Try it out with an image or a document and you will see that this Worker is also
130
132
131
133
In addition to serving static assets, we can also generate dynamic HTML or API responses based on the values stored in our KV store.
132
134
133
-
Start by creating this file in the root of your project:
135
+
1.Start by creating this file in the root of your project:
134
136
135
137
```json title="hello-world.json"
136
138
[
@@ -169,13 +171,13 @@ Start by creating this file in the root of your project:
169
171
]
170
172
```
171
173
172
-
Open a terminal and enter the following KV command to create a KV entry for the translations file:
174
+
2.Open a terminal and enter the following KV command to create a KV entry for the translations file:
173
175
174
176
```sh
175
177
npx wrangler kv key put hello-world.json --path hello-world.json --namespace-id=<ENTER_NAMESPACE_ID_HERE>
176
178
```
177
179
178
-
Update your Workers code to add logic to serve a translated HTML file based on the language of the Accept-Language header of the request:
180
+
3.Update your Workers code to add logic to serve a translated HTML file based on the language of the Accept-Language header of the request:
0 commit comments