Stateful Serverless Apps: The Cloudflare way

Building Performant Stateful Serverless Applications with Cloudflare

Modern applications demand scalability, low latency, proximity to the user, and the ability to support intricate business logic. However, meeting these requirements can be a significant challenge. Fortunately, new products are emerging that address these common issues, simplifying the delivery process for development teams.

Distributed systems and microservices have become an easy way to scale, but they create a nightmare for DevOps to deploy changes and orchestrate the infrastructure. The serverless paradigm promised that developers could focus solely on writing code, unburdened by the complexities of infrastructure management. AWS Lambda, Google Cloud Functions or Azure Functions are popular examples.

The downside of serverless is its ephemeral nature; the data required for requests, such as session information, shopping carts, and more, has to be stored externally, introducing latency for every request. Cold starts are a common issue, created by the architecture of these cloud providers, where the allocation of a new container and downloaded code need to be performed for every new instance of a function.

As I mentioned, in serverless not everything that shines is gold. How does Cloudflare propose to solve these day-to-day problems?

Infrastructure

“Cloudflare Workers” are serverless functions that leverage the same V8 engine that powers Google Chrome. With a presence in more than 300 locations around the globe the requests are handled very close to the end users; this is called “Edge” computing. Every deployment of your Worker is distributed globally by design to every location. Instead of provisioning a heavy container to run the function, V8 orchestrates isolated lightweight contexts for secure execution.

If you’re wondering whether using V8 means you can only run JavaScript, you’d be surprised. Workers have support for a variety of languages other than Javascript such as Python, Rust (natively), Go or even C, C++. This is possible by using the WebAssembly (Wasm) engine.

Minimizing the cold starts

A Worker takes single-digit milliseconds to start when typically it takes a few seconds in other providers. This achievement is possible by using a smart and optimistic approach. Let me try to explain it in a nutshell.
HTTPS connection requests perform a “handshaking”, establishing a secure channel between the user and the server using TLS in the transport layer. What if we start provisioning the Worker while we wait for the connection? This was the premise, and it provides a near-zero cold start, keeping in mind the lightweight execution context presented previously.

 

Dealing with the state

Here we introduce a game-changer, “Durable Objects”. Like a singleton class, Durable Objects are a unique, globally consistent, single-instance primitive within the Cloudflare ecosystem, designed to provide a distributed and strongly consistent internal state to build stateful abstractions that run across Cloudflare’s global network. I know, it sounds fancy and complex, read that again.

These objects are instantiated in a Worker, and by calling the object methods you exposed, you can manipulate the object’s internal state (actually a 10 GB SQLite database) that we can query with transactional SQL or using a KV interface.

 

Use cases

The key here is to split the business logic into several Durable objects instances, let’s bring this concept into real use cases:

  • AI agent session state: A Durable Object can represent an agent session, storing conversation context, tool-usage state, without leaking state across users or regions.
  • Streaming inference gateways: Workers make it easy to front LLM / inference APIs with low-latency streaming transforms, auth enforcement, or cost/metering before requests hit the AI backend.
  • IoT device coordination: Devices in a specific area, like a “smart home”, can coordinate actions through a single Durable Object instance.
  • Chat and messaging apps: Each chat room can be a Durable Object, handling all messages and user presence with global consistency. 
  • Collaborative editing: A single object can manage real-time edits for a document from multiple users, similar to the architecture behind tools like Google Docs. 
  • Multiplayer games: A durable object can represent a single game session, managing player state, game logic, and real-time updates. 
  • Live dashboards and analytics: Objects can aggregate and push real-time data to connected clients, simplifying the process of displaying live information. 
  • E-commerce shopping carts: Each user’s shopping cart can be a Durable Object, ensuring consistent access across devices and minimizing latency by keeping the cart object close to the user. 
  • Social feeds: A Durable Object can be created for each user to aggregate their subscriptions and feed content. 
  • Rate limiting: Durable Objects can be used to build a rate limiter by keeping track of a user’s request count. 

As you can see, the business logic of your application determines how you will use DO, but in case you need a global database, Cloudflare offers other data products, including:

  • D1: a managed, serverless database with SQLite’s SQL semantics, built-in disaster recovery, accessible using Workers and an HTTP API.
  • Hyperdrive: Turn your existing regional database in another cloud provider into a globally distributed database, allowing Workers to connect with your existing data.
  • R2: Object storage for unstructured data, such as web content files, AI RAG, or any kind of file with zero egress cost.

Honest pricing

Yes, serverless sounds exciting “You only pay what you use”. You don’t need to pay for virtual machines running all the time, but in production, your numbers start to grow and you discover hidden costs in common cloud providers. Egress fees, multi-region costs, invocation count, memory usage, and billable time are high in comparison.

Cloudflare offers competitive and clear pricing after switching from the free monthly tier. In addition, sub-requests made from Workers, like third-party API calls, do not count in the billing of milliseconds consumed; you only pay for CPU time used, not for idle time. Free egress of data, and no costs for storing the Worker codebase or restoring a previous deployment.

 

Final thoughts

For developers and organizations seeking to build the next generation of fast, reliable, and highly interactive online experiences, the time has come to explore and embrace this transformative technology. Dive into the documentation, conduct your experiments, and imagine how Workers and Durable Objects can become the cornerstone of your next cutting-edge, stateful serverless project. The future of application development is at the edge, and it’s remarkably stateful.

Ready to Build Smarter at the Edge? If you’re exploring how to scale globally, reduce latency, and keep full control over your app’s logic—Folder IT can help. We specialize in custom development for stateful, serverless, and AI-powered applications, optimized for performance and future growth. Contact us to bring your next idea to life.

 

Build your
tech team
faster
Scale with senior nearshore experts in your time zone.

Tags

NEWSLETTER
Get tech insights
in your inbox

Related

Access Elite
Software Developers
from Argentina

Get in touch
for expert solutions


«Outsourcing is too risky
and unreliable»


«Outsourcing is too risky
and unreliable»


«Outsourcing is too risky
and unreliable»

Get tech insights in your inbox

Get exclusive news and updates.