Building Multiplayer Applications with Cloudflare Workers & Durable Objects

Spanish audio is available in the player settings
Rate this content
Bookmark

Durable Objects, part of the Cloudflare Workers platform, are the solution for strongly consistent storage and coordination on the edge. In this talk, you will learn about Durable Objects, why they are suited for collaborative use-cases, and how to build multiplayer applications with them!

FAQ

Durable Objects in Cloudflare are a storage solution that allows state to be stored at the edge. They are designed for building scalable collaborative applications like document editors, game servers, and chat rooms. Durable Objects provide unique instances globally for handling state and coordination across multiple client requests.

Durable Objects are used with Cloudflare Workers to handle state and coordination in serverless environments. Workers can create and interact with instances of Durable Objects via an API, allowing state to be maintained across requests. Each Durable Object instance is uniquely addressable, ensuring consistent state management.

Durable Objects are ideal for collaborative applications because they allow centralized coordination of state changes across multiple clients. This is essential for apps where users interact in real-time, such as in collaborative editing or multiplayer gaming, ensuring that all users see the same state.

Cloudflare's Durable Objects are designed to be infinitely scalable by leveraging a unique global distribution model. They are hosted in multiple Cloudflare data centers worldwide, allowing them to handle a large number of requests and maintain high performance and availability.

The billing for Durable Objects is based on the wall clock time during which an object handles an active request. Once the Durable Object returns a response, billing stops, making it a cost-effective solution for applications that require intermittent coordination across global users.

Durable Objects are suitable for high traffic scenarios as long as the load is distributed across many instances. Each Durable Object handles a portion of the traffic, allowing the system to scale efficiently by distributing the workload.

One limitation is that each Durable Object instance runs a single thread of JavaScript, which may not be suitable for tasks requiring intense computation or very high concurrency. Additionally, accessing a Durable Object from far geographical locations may introduce latency.

Matt Alonso
Matt Alonso
28 min
14 Apr, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Durable Objects are a part of CloudFlare's long-term goal to expand application possibilities on workers, allowing for the building of scalable collaborative applications. Durable Objects provide a way to store global state and coordinate multi-client applications. They can be created as close to the user as possible and have unique IDs for routing requests. Durable Objects have a persistent storage API with strongly consistent semantics and IO gates to prevent correctness errors. They are well-suited for collaborative applications and can be used with WebSockets. Performance impact and read replicas are considerations for accessing Durable Objects globally.

1. Introduction to Durable Objects

Short description:

I'm Matt Alonzo, a software engineer at CloudFlare. I work on the workers distributed data team and have extensive experience with durable objects. Durable objects are a part of CloudFlare's long-term goal to expand application possibilities on workers. They are not just a storage product but also ideal for building scalable collaborative applications like document editors, game servers, and chat rooms.

I'm Matt Alonzo and I'm a software engineer at CloudFlare. I work on the workers distributed data team who maintains durable objects. I worked at CloudFlare for almost three years and I spent almost the entire time working on durable objects. I'm very familiar with them. And durable objects are kind of part of a long-term goal for CloudFlare where we're trying to expand the types of applications that customers can build on top of workers. And durable objects have been thought of as kind of like a storage product that adds onto this. You can store state-on-the-edge durable objects, but there's a lot more to them than just that. Durable objects are also really well suited to building infinitely scalable collaborative applications like document editors, game servers, and chat rooms, and this talk is all about analyzing why.

2. Overview of Workers and Durable Objects

Short description:

I'm going to talk about workers, give a quick overview. I'll talk about durable objects, their API, and coordination. I have a case study and a demo. Workers is Cloudflare's serverless JavaScript platform. It uses its own runtime and runs globally. It's implemented with V8 isolates, allowing for efficient code execution. This is how workers can exist today.

If folks have any questions after the talk, feel free to email me or contact me on my social media. And so, I'm going to go through a few subjects today. I'm going to talk about workers, kind of give a quick overview of them. I'm going to talk about durable objects, what they are, what the API looks like, and and then I'm going to talk about coordination. Why are durable objects useful for coordination? Why would you want to use them? And then I have a bit of a case study, an application I built on top of durable objects, and then if all goes well, I'll do a demo of it.

And so, workers is Cloudflare's serverless JavaScript platform. The idea is you write a fetch handler like this one above, it takes in a request object, it does some work, and it returns a response. And so you take your worker code, you upload it to Cloudflare, and we handle the rest. This is pretty similar to a lot of other serverless platforms, but there's a few big differences with workers. Workers uses its own runtime, and workers don't run in regions. There's only one, really, region with workers. It's Earth. You deploy once, and your code runs in any of Cloudflare's network locations worldwide. When you use workers, your domain is also using Cloudflare's CDN products, and so DNS queries to your domain return a Cloudflare Anycast IP. When you send traffic to an Anycast IP on one of Cloudflare's, it's going to go to the nearest Cloudflare data center, no matter where you are in the world. And so this means when you make a request to a Cloudflare worker, it's going to run near you in the nearest Cloudflare data center, regardless of where you're making the request from. And this map here is a map of the latest one I could find of all of Cloudflare's locations around the world. So as you can see, it's pretty hard to be far from a location where workers can run, unless you're in the middle of the jungle or something.

And so a core part of being able to run code like this on Cloudflare's edge is the way workers is implemented is very different from other serverless platforms. We have our own JavaScript runtime built on top of V8. And this is the same JavaScript engine used by Node.js and Chromium. And instead of implementing multi-tenancy by starting up multiple runtime instances like other serverless platforms work, what workers does is we implement multi-tenancy with V8 isolates. This is a feature of V8 that allows you to run code separated, all in the same process. And so we'll run multiple customers code in the same OS process, they all run in different isolates, and they're completely isolated from each other, they can't interfere with each other, change global state, or anything like that. And so isolates are much cheaper to start up than an entire OS process. And so when a request for a cold-started worker hits the worker's runtime, we spot a new isolate. And this takes only a few milliseconds, compared to hundreds of milliseconds to cold-start a whole runtime. And this is really important to workers. It wouldn't be possible for us to run a whole runtime per customer on the Edge. This kind of idea is how workers is actually able to exist today.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Living on the Edge
React Advanced Conference 2021React Advanced Conference 2021
36 min
Living on the Edge
React 18 introduces new APIs for rendering applications asynchronously on the server, enabling a simpler model for architecting and shipping user interfaces. When deployed on edge networking platforms like Cloudflare Workers, we can get dramatic performance and user experience improvements in our applications. In this talk, Sunil will demo and walk through this new model of writing React applications, with some insight into the implications for data fetching, styling, and overall direction of the React ecosystem.
Database Access on the Edge with Cloudflare Workers & Prisma
Node Congress 2022Node Congress 2022
31 min
Database Access on the Edge with Cloudflare Workers & Prisma
Edge functions are pushing the limit of serverless computing – but with new tools, come new challenges. Due to their limitations, edge functions don't allow talking to popular databases like PostgreSQL and MySQL. In this talk, you will learn how you can connect and interact with your database from Cloudflare Workers using the Prisma Data Proxy.
You can check the slides for Alex's talk here. 
Static first websites with Cloudflare Workers
Node Congress 2022Node Congress 2022
30 min
Static first websites with Cloudflare Workers
Static websites give you all sorts of great benefits. You don’t have to worry about security or scalability. They are simple to cache, cheap to host and a breeze to maintain! But sometimes I miss all the fun things you can do with just a little bit of state! Combining the Cloudflare Pages platform with Durable Objects and the HTMLRewriter API, you can have your cake and eat it too! We’ll replicate a full WordPress experience with comments, top posts, like buttons and a page counter. All on Cloudflare’s free static site hosting platform.
You can check the slides for Jonathan's talk here.
The Future of JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
The Future of JavaScript Runtimes
JavaScript was born in the browser, Node brought it to the server embracing unix primitives and async I/O and lately Cloudflare Workers & Deno Deploy have brought it to the edge. Let’s take a look at where JavaScript runtimes are heading and how it will shape the software we write.
Automate the Browser With Workers Browser Rendering API
JSNation 2024JSNation 2024
20 min
Automate the Browser With Workers Browser Rendering API
In this talk, we will explore how the Browser Rendering API can automate browser tasks, freeing developers from repetitive manual work. We will begin with an overview of Cloudflare Workers and how they enable running JavaScript at the edge. Then, we will discuss browser automation in detail, covering how to interact with the DOM, fill out forms, and scrape data from web pages. We will showcase real-world examples of how browser automation with Cloudflare Workers can improve the user experience of web applications, increase productivity, and automate tasks, such as generating screenshots and PDFs of web pages, testing web applications, and running performance audits. At the end of this talk, attendees will gain a better understanding of how to use the Browser Rendering API to automate browser tasks and take their web development skills to the next level.
Building full-stack applications on the Edge
React Advanced Conference 2022React Advanced Conference 2022
9 min
Building full-stack applications on the Edge
Scaling and maintaining applications can be a pain, especially when thinking of how to build to get the user experience to a level that you are proud of. With all these shifting parts to consider, it is important to abstract parts of your applications to global reliable services. In this talk, we will discuss some of the services that Cloudflare provides, and the role they play in improving the overall developer and user experience.

Workshops on related topic