What sits between your application and the client's browser? When the answer is "the internet", Cloudflare wants their Workers platform to play a part. They recently expanded that platform with Workers KV, a distributed, eventually-consistent key-value store available in 180+ edge locations.
Cloudflare Workers is the serverless platform that lets developers deploy JavaScript code and WebAssembly apps to Cloudflare's far-reaching datacenter footprint. Workers scale to thousands of instances and intercept HTTP requests to your site. They start up in less than 5ms, while running for a maximum of 50ms. Until now, if a Worker needed to retrieve data, the developer had to store it in the Worker script itself, or load data files from a Cloudflare Cache. The Cloudflare team shared that soon after launching the Workers platform, customers asked for a better way to store persistent data. That was the genesis of Workers KV.
Workers KV has a simple read-write API that can be invoked over HTTP or inside a Worker. Developers retrieve values as text, JSON, arrayBuffer, or stream. The service is designed for fast reads, with a reported median response time of 12ms. Any values written to Workers KV — values can be up to 2MB in size — are encrypted at rest, in transit, and on disk. Cloudflare upgraded the write experience during the beta period by adding an endpoint for bulk loading. Keys written to Workers KV are automatically replicated across the Cloudflare network, with global consistency happening in less than 60 seconds. Only the most popular keys are replicated globally, however, as Cloudflare's documentation point out that infrequently read values are stored centrally. This is a "serverless" service in that it is fully managed with no infrastructure operations exposed to the customer. All provisioning, upgrading, scaling, and data replication is handled by Cloudflare.
The product manager for Workers KV was careful to not reveal the backing storage or technology used for this service, but they were upfront about the service guarantees. The blog post announcing the service briefly explained CAP Theorem and highlighted their design decisions.
Workers KV chooses to guarantee Availability and Partition tolerance. This combination is known as eventual consistency, which presents Workers KV with two unique competitive advantages:
- Reads are ultra fast (median of 12 ms) since its powered by our caching technology.
- Data is available across 175+ edge data centers and resilient to regional outages.
Although, there are tradeoffs to eventual consistency. If two clients write different values to the same key at the same time, the last client to write eventually "wins" and its value becomes globally consistent.
Given those considerations, what uses cases do they recommended? Cloudflare called out a few examples based on what customers built so far.
- Mass redirects - handle billions of HTTP redirects.
- User authentication - validate user requests to your API.
- Translation keys - dynamically localize your web pages.
- Configuration data - manage who can access your origin.
- Step functions - sync state data between multiple APIs functions.
- Edge file store - host large amounts of small files.
Back when they announced the service in September 2018, Cloudflare also suggested that Workers and Workers KV could serve as a cheap, high-performing API Gateway that would be dramatically cheaper than something like Amazon API Gateway. In the same post, they listed other use cases related to changing Worker behavior without redeployment, A/B testing, or even storing shopping cart data for eCommerce sites.
Workers KV is now generally available with published pricing. If a customer already has a $5 Workers subscription, they have some usage of Workers KV included: 1 GB of storage, 10 million reads, and 1 million writes. For usage that exceeds those amounts, the cost is $0.50 per GB per month of storage, $0.50 for 1 million reads, and $5 for 1 million writes. Namespaces are containers for your key-value pairs, and developers can create up to 20 namespaces, with billions of key-value pairs in each one. Users can do unlimited reads per second per key, and up to one write per second per key.
Cloudflare says that this new serverless key-value store "gives developers a new way to think about building software: what should go on the server, what on the client and what in the Internet?"