Building Multiplayer Applications with Cloudflare Workers & Durable Objects

Spanish audio is available in the player settings
Rate this content
Bookmark
Durable Objects are perfect for collaborative applications as they provide a single point of coordination between multiple clients, eliminating the need for complex multi-region deployments. They are used with Cloudflare Workers to handle state and coordination in serverless environments. For example, a multiplayer version of Conway's Game of Life can be implemented using Durable Objects, enabling thousands of people to play in separate rooms. The pricing model for Durable Objects is based on active request duration, making them cost-effective for global deployments. Cloudflare ensures the scalability of Durable Objects by hosting them in multiple data centers worldwide. Each Durable Object instance runs a single thread of JavaScript, which may not be suitable for tasks requiring intense computation. WebSocket connections in Durable Objects are treated the same as HTTP requests, ensuring consistent performance. The Storage API and Durable Objects have IO Gates to prevent concurrency issues and ensure consistent data storage. Read replicas are being considered to provide a closer view of the stored data.

From Author:

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!

This talk has been presented at Node Congress 2023, check out the latest edition of this JavaScript Conference.

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 Transcription

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
The Talk discusses the future of React and introduces new APIs, including streaming rendering and server components. React Suspense allows for asynchronous loading of components and data fetching. The use of serverless computing, specifically Cloudflare Workers, is explored as a way to improve performance. The Talk emphasizes the potential for simplifying the React ecosystem and the excitement about the new API.
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
This Talk discusses database access on the edge with CloudFlare workers and the challenges of serverless platforms. It explores solutions for database access, including CloudFlare-specific solutions and using Prisma data proxy. The Prisma toolkit and demo are showcased, demonstrating how to convert an application to use a database. The process of setting up Prisma Data Platform and deploying the application to CloudFlare workers is explained. The Talk concludes with insights on database usage and the differences between serverless, CDN, and the Edge.
Static first websites with Cloudflare Workers
Node Congress 2022Node Congress 2022
30 min
Static first websites with Cloudflare Workers
The Talk covers the historical landscape of web development, the rise of static site generators, the serverless revolution, edge computing, and using Cloudflare services to enhance static websites. It explores the challenges of early web development, the shift to static sites and client-side rendering, and the advantages of server-side and client-side rendering. It also discusses Cloudflare services like Cloudflare workers, KV, durable objects, and HTML rewriter for building fast and minimal hosting solutions. The Talk highlights the use of DurableObjects for analytics and integration, dynamic content on static sites, JAMstack, and the advantages of using Cloudflare Workers for automatic deployment, multiple language support, and combining static pages with JavaScript functions. It introduces the concept of edge computing and the difference between Cloudflare Pages and Workers. It also touches on error handling and the use of HTML rewriter, Cloudflare KVstore, and DurableObjects for managing state and storing dynamic data.
Automate the Browser With Workers Browser Rendering API
JSNation 2024JSNation 2024
20 min
Automate the Browser With Workers Browser Rendering API
The Talk discusses browser automation using the Worker's Browser Rendering API, which allows tasks like navigating websites, taking screenshots, and creating PDFs. Cloudflare integrated Puppeteer with their workers to automate browser tasks, and their browser rendering API combines remote browser isolation with Puppeteer. Use cases for the API include taking screenshots, generating PDFs, automating web applications, and gathering performance metrics. The Talk also covers extending sessions and performance metrics using Durable Objects. Thank you for attending!
The Future of JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
The Future of JavaScript Runtimes
Today's Talk explores the future of JavaScript runtimes, their evolution, and impact on software development. It discusses the historical trends of JavaScript, the adoption of new tools and libraries, and the convergence of Node and Deno. The emergence of isolate clouds and their potential to replace traditional VMs and containers is also highlighted. Additionally, the talk touches on the possibilities of JavaScript in exotic use cases, its impact on machine learning, and the potential for TypeScript to become the de facto language for JavaScript development.
Building full-stack applications on the Edge
React Advanced Conference 2022React Advanced Conference 2022
9 min
Building full-stack applications on the Edge
This Talk explores building full stack applications on the edge and the blurring line between front end and back end development. It discusses the benefits of using the JAMstack and APIs for scalable web development. The Talk also highlights the improvements in edge technology, such as serverless functions and efficient data storage, and the deployment of functions and durable objects on the edge network. Overall, the Talk emphasizes the importance of delivering high-quality experiences with low latency through edge computing.

Workshops on related topic