Serverless architecture has revolutionized how we build applications, but it comes with a well-known Achilles' heel: managing state. For stateless functions, it’s a dream. But as soon as your application requires real-time collaboration, live presence, or persistent chat, you enter a world of complexity. How do you manage WebSocket connections when your functions are ephemeral?
The traditional workarounds are often complex and costly. You might find yourself managing sticky sessions on load balancers or bolting on a separate stateful service like Redis just to handle pub/sub and connection mapping. This adds significant operational overhead, undermining the core promise of serverless.
At Vaultrice, we faced this exact dilemma. We needed to build a global, low-latency, and strongly-consistent backend for real-time state, without building a traditional, resource-intensive server fleet. This post details the architectural journey we took and how we solved this problem by betting on Cloudflare's cutting-edge stack, with Durable Objects at its core.
The Game Changer: Our Bet on Cloudflare Durable Objects
After evaluating the landscape, we chose Cloudflare Durable Objects (DOs) as the foundation of our architecture. They offered solutions to the most difficult stateful problems:
For Strong Consistency: We needed to guarantee atomic operations for features like live vote counters and collaborative data editing. The single-threaded execution model of a Durable Object instance was the perfect fit. It ensures that all operations for a given data object are processed serially, eliminating the risk of race conditions that plague eventually-consistent systems.
For Cost-Effective, Stateful WebSockets: The biggest challenge in building a real-time service is managing persistent WebSocket connections at scale. The Durable Object WebSocket Hibernation API was the key. This powerful feature allows us to maintain a client's WebSocket connection at the Cloudflare edge while the DO itself hibernates, waking it only when a message arrives. This is fundamental to our ability to offer a generous free tier and an efficient, scalable service making powerful real-time features accessible to projects of any scale.
The Vaultrice Architecture: A High-Level Flyover
Our architecture is designed for simplicity and performance, abstracting the power of the edge into a developer-friendly API.
The flow is straightforward:
- The Stateless Entrypoint (Cloudflare Worker): Every request first hits a standard Cloudflare Worker. Its job is purely to handle authentication (verifying API keys or access tokens) and route the request to the correct Durable Object instance.
- The Stateful Core (Durable Object): This is where the real work happens. Each Durable Object acts as a self-contained, stateful micro-server for a single data object—like a chat room, a shared document, or a user's settings. It manages the in-memory state, handles WebSockets, and interacts with its private, strongly-consistent transactional storage. All complexity is contained here.
- The Simple Abstraction (Our SDK or our React bindings): All of this is hidden behind our simple,
localStorage
-like SDKs (NonLocalStorage
andSyncObject
). The developer never interacts with the Durable Object directly; they just call familiar methods likenls.setItem('key', 'value')
ordoc.title = 'New Title'
.
Key Challenges We Abstract Away For You
Building this architecture reliably is non-trivial. The real value of Vaultrice is in the hard problems we've already solved so our users don't have to.
Seamless Reconnection & Presence:
Ensuring a user's presence is accurately reflected across flaky networks and browser tabs is notoriously difficult. We built a robust session management system within our DOs that handles reconnection and state reconciliation automatically, so your "who's online" list is always accurate.
Secure, Multi-Tenant Data Isolation:
Architecting a secure multi-tenant system requires careful data scoping. Our platform maps each customer's data objects to unique Durable Object instances, providing a strong layer of isolation. This is then enhanced by our own security model, with features like Object ID Signature Verification, which cryptographically ensures that even a valid API key can only access authorized data objects.
Concurrency & Data Integrity:
With many users modifying the same data, preventing data corruption is paramount. Instead of forcing developers to build complex locking mechanisms, Vaultrice provides a suite of server-side atomic operations for common mutations like counters, array pushes, and object merges, which are executed safely within the single-threaded context of the DO.
Conclusion: Leverage the Edge, Today
Stateful applications at the edge are the future of the real-time web, but the path to building them from scratch is fraught with complexity. By building Vaultrice on Cloudflare's powerful serverless platform, we've created a "batteries-included" solution that makes this new architecture accessible to any developer.
We handled the hard parts of distributed systems so you can focus on what matters: building amazing user experiences.
- See this architecture in action by cloning our React Chat Starter on GitHub.
- Deploy your first global, real-time feature in minutes on our generous free tier.
Top comments (0)