Ashley Peacock, the author of Serverless Apps on Cloudflare, speaks with host Jeremy Jung about content delivery networks (CDNs). Along the way, they examine dependency injection with bindings, local development, serverless, cold starts, the V8 runtime, AWS Lambda vs Cloudflare workers, WebAssembly limitations, and core services such as R2, D1, KV, and Pages. Ashley suggests why most users use an external database and discusses eventually consistent data stores, S3-to-R2 migration strategies, queues and workflows, inter-service communication, durable objects, and example projects.
Brought to you by IEEE Computer Society and IEEE Software magazine.
Show Notes
- Twitter: @_ashleypeacock
- Serverless Apps on Cloudflare
- Cloudflare docs
- Learn Cloudflare Workers 101
- Bindings
Related Episodes
- SE Radio 345 – Tyler McMullen on Content Delivery Networks
- SE Radio 566 – Ashley Peacock on Diagramming in Software Engineering
Transcript
Transcript brought to you by IEEE Software magazine.
This transcript was automatically generated. To suggest improvements in the text, please contact [email protected] and include the episode number and URL.
Jeremy Jung 00:00:18 Today I am talking to Ashley Peacock. He’s the author of Serverless Apps on Cloudflare and he was a previous guest talking about diagramming and software engineering. Ashley, welcome back to Software Engineering Radio.
Ashley Peacock 00:00:31 Hey, glad you’re back and looking forward to talking about Cloudflare today.
Jeremy Jung 00:00:34 For anybody in our audience who hasn’t heard of Cloudflare, what is it?
Ashley Peacock 00:00:39 So I guess Cloudflare’s been around for quite a while. It started out as a kind of cloud security company and then over the years they’ve had different phases where they gradually grow from doing security related to websites. So bot protection, content delivery network, things like that. And they’ve slowly migrated to also introduce a developer platform that’s not unlike AWS. So you can go on, you can deploy your apps they’ve got everything you need. And those are the kind of two main pillars I would say of what Cloudflare offers.
Jeremy Jung 00:01:10 And I think maybe a lot of people’s first exposure to Cloudflare is as a CDN. So maybe you could explain it at a high level what a CDN is.
Ashley Peacock 00:01:19 Yeah, so let’s say you have a WordPress blog and you host it on a virtual machine or some shared hosting you’re effectively hosting it on a single server. And let’s say you’re really lucky, you write a really positive blog post, it goes viral gets retweeted everywhere. Your little VM or your shared hosting might not survive the rush of traffic. And what a CDN can do is it distributes your content from your blog. So the images, the JavaScript CSS and the content itself globally around the world. And then what happens is whenever a request comes in, the CDM will check and see if it has content in its cache. If it does, it’ll return it without ever hitting your VM. So that gives you decreased LA latency, so fast responses for end users and it also puts a lot less pressure on your VM. And then with Cloudflare that’ll also give you some security protection to protect you from attacks and bot protection and things like that. So it, it’s effectively like a proxy sitting kind of like in front of your website as a bit of a shield and just gives you those benefits that I outlined.
Jeremy Jung 00:02:26 And you mentioned it’s starting to become more of a developer platform. What do you mean specifically by that?
Ashley Peacock 00:02:35 So if you go back to the origins of Cloudflare, it was a security company the CDM we talked about. That’s how it largely started and the very kind of beginnings of its developer platform. Weíre allowing people who use the proxy to do things like modify the request headers before the content is sent back. So you could add a HTP header for example or modify the response. And that was the very kind of beginning of what’s called a Cloudflare Worker, which is like a serverless function. You can write some code, be executed when a request comes in. And then over the years they’ve taken what was something very simple to just modify the headers and you can now deploy fully fledged applications to Cloudflare develop platform. So Workers give you compute very similar to a DS Lambda in some ways, but also very different.
Ashley Peacock 00:03:26 And they have the other kind of core building blocks that you would expect to build applications. So databases, caches, queues, all those sorts of things that you use from AWS like R2 and Aurora and all those things. But it’s a lot more focused than AWS. So AWS these days has, I don’t know how many services it has, but it’s in the hundreds, right? And Cloudflare focused on the kind of core building blocks for your website I would say. And particularly in the last few years, they’ve really kind of pushed that developed platform and added what I would say are the kind of missing pieces. Like they didn’t have a database, they didn’t have object storage, but it’s really come on leaps and bounds in recent years. And I think it’s at the point where it’s starting, you can start to see the momentum and people start to pick it up and realize actually it’s a kind of complete platform for building apps these days.
Jeremy Jung 00:04:13 And you mentioned Workers as an example, it sounds like that’s very similar to AWS’s Lambda. Why would someone who for example, is already on AWS look at something like Workers?
Ashley Peacock 00:04:25 I think one of the huge selling points for Cloudflare in my opinion is with Lambda for example, let’s just take Lambda because it’s probably the biggest one that everyone knows it’s been added to AWS but they also have hundreds of other services. They’ve kind of bolted on serverless and Lambda onto their offering. And it works, very nicely. It’s not a bad product by any means, but with Cloudflare’s offering, they always envisioned their entire platform being serverless. So it’s designed from the ground up to be serverless. Not only that but they focused on things that are important to me and you with developer experience and how easy and enjoyable it is to build on the platform. Like if you’ve ever built anything on a US lately, you probably spend half an hour trying to get your IAM Policy just right because it keeps on not quite working. You tweak it, you tweak it, tweak it, eventually you get there or you need it and then you need a VPC or you need an IP address or something. With CloudFare there’s no such roadblocks. It’s more like the platform is kind of working with you and they’ve really designed it for engineers versus AWS to a point has to kind of build and market to the CTOs and people that are not building those products. Whereas Cloudflare, I think it’s taken a slightly different approach.
Jeremy Jung 00:05:35 What it sounds like is maybe the developer experience or the ease of use is more optimized on Cloudflare than it would be for someone getting started with AWS?
Ashley Peacock 00:05:45 Yeah, I can give you a couple of examples. So I think things that you don’t get generally in other places, so let’s say you want to connect your Worker, so your kind of serverless function to a database or a cache or any of the resources that Cloudflare gives you, you use what’s called a binding. And if you’ve ever used dependency injection, it’s kind of like automated dependency injection. But rather than you doing it yourself at runtime, when your Worker is started by Cloudflare, because they a request comes in, your Worker is invoked and spun up automatically by CloudFare, the request is executed against it and at that point they will inject the binding that is needed to run your Worker. So if you have access to a database, it’s just injected for you and it’s effectively in the environment for you. You don’t have to mess around with secrets or any configuration, it’s just there for you.
Ashley Peacock 00:06:37 And that also then flows in, and this is kind of what I was talking about with the integrated nature and the developer experience of when you run your Worker locally, Cloudflare will use their kind of local emulation to spin up effectively kind of like steps that are faking. They’re not running against real resources. You can just run that locally and you’ll get the exact same experience as you would in production, but you’ve had to make no changes. You can just run a single command, it’ll start your Worker, it’ll have access to all your bindings, however many there are. And it’ll just pieces together very kind of seamlessly
Jeremy Jung 00:07:10 Trying to understand how the binding works in practice. So with a traditional application, if you had a database you would need let’s say credentials or a connection string and like you said you could put that into some kind of secret store or environment variables. How are the bindings different? What does my code look like where it can find how to connect to the database in this case?
Ashley Peacock 00:07:38 Yeah, so you first, because everything is serverless, you create the resource you need and cloud have their own CLI called Wrangler. So if you are just hacking together something you can easily just run a command with Wrangler that will make you a database or a cache or whatever you need. It’ll basically give you back an ID or sometimes you use the name of the resource that only your account has access to and then you take that ID or that name and you put it in a configuration file that’s historically been in TOML but it’s now you can now use JSON and it’s effectively just, yeah your configuration goes in that JSON file and then at runtime Cloudflare reads that file and it injects the correct one. And in that configuration file you have things like environment. So you can have, obviously you don’t want to have your staging and production databases to be all mixed. So you can effectively spin up multiple databases and configure the correct one for each environment. And then yeah, when it’s run at runtime it’ll be injected.
Jeremy Jung 00:08:35 So the distinction I guess is, instead of having references to a specific host name or IP, you are putting in this ID into your configuration and then Cloudflare is figuring out where that database or that other service actually is.
Ashley Peacock 00:08:52 Yeah, correct. And then in your actual code there’s a parameter on your or your Worker if you’re using a framework, they’ll have code that the framework has written that makes it accessible but it’s effectively like it being in your environment for you. And you don’t have to worry about how you interface with it necessarily because they will inject like an SDK for you to use. Each of them comes with an SDK, there are wrappers from the community that obviously enhance them, but generally speaking, all the kind of core functionality is wrapped for you in an SDK that’s injected for you at runtime.
Jeremy Jung 00:09:24 And you had mentioned the local development example, so it sounds like if you’re running locally in that config file there’s something you could put that tells it instead of trying to connect to my production instance running on Cloudflare, instead run this local emulator and connect to that instead?
Ashley Peacock 00:09:46 Not even that. It will effectively, when you run the command by default it will just run and emulate everything locally and then if you want to run it remotely, you can actually run them remotely by effectively just passing in a flag when you run the command. And then it will effectively push your Worker into like a sandbox environment so it doesn’t deploy it to like your account, it’s in a kind of safe sandbox environment but it has access to all your bindings. So let’s say, I don’t know, you are finding it difficult to debug an issue in let’s just say staging, right? because staging you can mess around with the data. It’s not user data. You could easily run that Worker locally, point it at the staging database and run everything locally and then try and replicate yourself super easily, which I think is super nice.
Jeremy Jung 00:10:29 So what this would look like is you have your project with all your source code files and you use the command line tool that that Wrangler tool and based on which flags you pass in, it either will emulate the dependencies by default or like you said, you can pass in a flag and then it will actually run it on a server instead.
Ashley Peacock 00:10:50 Yeah, that’s exactly right.
Jeremy Jung 00:10:51 I’m trying to just make sure I understand the binding parts specifically. When you say things are being injected at runtime, like what exactly is being injected? Like what do I see in my code and then what is actually going in at runtime?
Ashley Peacock 00:11:09 So each Worker has an entry point. So if we just take a bare bones Worker, the function is called Fetch and it has a few parameters on it. One of them is obviously the incoming HTP request. So you can pull out the URL, the headers, whatever you need. And then the second one is basically literally called environment. And on that environment, when you are configuring it in your Wrangler file alongside the ID, you define a name for the binding. So, you give it a descriptive name for whatever it is, if it’s a database or cache or whatever. And then you basically do environment dot the name and then you can just call methods on it. So let’s take a cache, very simple, right? With a cache you basically you can set or you can get or you can probably, you can delete and maybe list. You basically just call those methods on the environment and it will execute those remotely against whatever the binding is for such as a cache
Jeremy Jung 00:12:04 In your configuration file, that TOML file or that JSON file. That’s where you create these environments and you specify what services or resources you have available. And then in your environment parameter you can access all those things you put in your config file?
Ashley Peacock 00:12:23 Yeah and Cloudflare will just effectively read that configuration file and inject all of those at runtime seamlessly.
Jeremy Jung 00:12:29 We talked a little bit about Workers which you said are similar to Lambdas. So those are serverless functions. Since serverless is sort of an overloaded term, can you sort of explain to people who aren’t familiar with them what that really means to you?
Ashley Peacock 00:12:47 Yes, so as you said, there are many definitions for serverless. So if you ask a hundred engineers they’re going to give you a different definition. I think for me there are kind of some core tenets of what makes serverless serverless. I think one of the main ones is that you pay based on usage. So there are some services from AWS, I think Redis is one of them where it has like a base cost. So even if you are not using it, you have a base cost. So it’s not scaling to zero and for serverless it should scale to zero. You shouldn’t need to do any maintenance. So you shouldn’t need to like upgrade the version of the program language or the software you’re using. Or if you’re using, let’s just say kick with Redis, you shouldn’t need to upgrade your Redis version. It should be handled for you by the platform and you shouldn’t need to wait for your resources to spin up because you’re not spinning up a server. So the whole thing about server is it should be quick and that’s exactly what it’s with Cloudflare, if you issue that Wrangler command to create a database, you’re kind of creating a logical database rather than an actual physical instance. And that is available instantly for you to use. So I think those are some of the key things of when it comes to service, what I would consider it service to be.
Jeremy Jung 00:13:57 And something I used to hear about often is what people refer to as the Cold start problem where it takes time for the resource to be available. Is that something that you also have to be aware of on Cloudflare?
Ashley Peacock 00:14:11 No. So there are exceptions as with all things, but unless you have a huge Worker that is perhaps doing something on boot, you’ll generally never see cold starts with Cloudflare. And the reason is the runtime is very different to what you can run on at least the typical Lambda Cloudflare’s environment is it’s their own runtime and it’s based on the same runtime that you run in Chrome. So it’s called the VA engine, it’s exactly what runs in your browser. So that makes JavaScript kind of king at least JavaScript TypeScript are the primary languages that are supported. You can also use web assembly to then compile from pretty much, any language with good web assembly support. Rust is a big one because Cloudflare internally use a lot of Rust and then that runtime is also enriched with node libraries. So it’s not full node but it’s got kind of compatibility with a lot of node.
Ashley Peacock 00:15:04 Especially recently they’ve been really adding the kind of compatibility to it. But in terms of the cold start, what they do is quite clever where when a request comes into your Worker or everybody, if a request comes into anything in this day and age, it’s all goes through HTPS and it needs to do what’s called a TLS handshake where the client basically they exchange certificates and they do a handshake and it basically verifies that the connection is secure. And that takes, I donít know how long it takes but it takes some milliseconds where in that time Cloudflare basically knows where the request is going and it’s warmed up your Worker while it’s doing that handshake so that the time the handshake is finished, let’s say it takes 50 milliseconds, your Worker then able to handle that request instantly and there’s basically no cold start. And that’s thanks firstly to why I just explained with the handshake and secondly with the runtime because you can imagine you open a tab, each one of those is its own V8 runtime and you can see how quickly your tabs available, right? It’s available instantly. So I think it’s a mixture of their technology stack plus some clever engineering
Jeremy Jung 00:16:07 It sounds like, so maybe that’s one of the tradeoffs between the Cloudflare Workers and Lambda where I believe with Lambda they have specific language support and it’s not specifically tied to can it run in web assembly whereas with Cloudflare it sounds like if you can compile to web assembly then you, you can run it.
Ashley Peacock 00:16:28 Yeah, I would say the vast majority of code at the moment is written in JavaScript and TypeScript for sure because that’s, as you can imagine, it’s basically like a browser. So it’s natively runs out of the box. There was Python introduced towards the end of last year or earlier? Yeah, last year which is natively running but it’s not fully there yet. I think you can see the direction they’re going in but things like dependencies don’t quite work that well yet. But I think more will come and certainly Rust is one of the main ones because it has the web assembly sport but it’s for sure a tradeoff where on AWS you can have a full node environment for example or you can have Ruby or whatever else that they support on Lambda.
Jeremy Jung 00:17:10 And something I’ve heard about web assembly is that there are certain limitations where you can use these languages but certain Io level access for example is maybe not available. Does that also apply on this environment?
Ashley Peacock 00:17:25 It does. So whether you’re using Web Assembly or using JavaScript, you actually don’t have access to the file system, same as in the browser because you have the same kind of restrictions that you have in the browser. So if you are reading configuration files you can’t read those from the file system because you just simply don’t have access. You’ll need to push them to object storage or if the configuration’s quite simple, particularly in JavaScript you can obviously just define objects and have the configuration defined in your code but yet the restrictions that you’ve probably encountered probably apply here as well, I would say.
Jeremy Jung 00:17:58 So we’ve mostly been talking about Workers but AWS as you mentioned has hundreds if not thousands of different services. I imagine Cloudflare doesn’t have quite as many but there’s still probably a lot. What would you say are the core services to focus on outside of just Workers?
Ashley Peacock 00:18:16 So you have R2 from Cloudflare, which is basically an S3 compatible object storage. So if you want to store user generated files or your own generated files and have them served from a bucket, it’s completely compatible with S3. You can even just pour all your content from S3 into R2 and use the AWS SDK and everything will just mostly work. There is a few kinds of little APIs that aren’t implemented but generally speaking it will work. And the nice thing about Cloudflare is that it has no egress fees unlike a W. So if you’re doing a ton of egress via S3, you could move that content to R2 and you’re not going to pay egress and it might save you obviously a lot of money because the egress costs are, they’re kind of like a bit of a hidden cost aren’t they, where you’re not really, it’s kind of hard to work out where it’s coming from sometimes and they can really rack up for a Worker or R2, you don’t get charged egress besides R2, you have D1 which is the serverless database that’s effectively SQLite.
Ashley Peacock 00:19:14 They’re a little different to things you’ll find on AWS because they’re designed to be per tenant, so they’re limited in their kind of size if you’ll, it’s not meant you’re meant to have one database per user, you have multiple databases typically they have a key value store. So its interface is similar to Redis but it’s kind of designed is quite different. It’s designed for kind of read heavy rather than write heavy and it’s eventually consistent rather than Redis is obviously just it’s Realtime sub millisecond but it’s also Redis is hosted in one location. It generally sits close to your application server, right? Whereas with cloud for everything is global and I guess like we were talking about trade-offs earlier, one trade-off is because it’s global nature, there are these eventual consistencies concerns where you write, it’ll take a while to property around the entire network in terms of like hosting your static content because until recently at least Workers were primarily just for handling APIs and you could do content but it was much more common to use a framework.
Ashley Peacock 00:20:17 So you can imagine Next.js, Astro, all the popular JS frameworks are deployable to Cloudflare and they have a product called Pages. And that effectively is just your full stack hosting where it will serve your static assets and it will also convert your Next.js routes. For example, if you’re using Next.js, it’ll convert those to kind of effectively like a Worker at build time. So it works with Cloudflare’s platform. So that allows you to host your, your front end and more recently you can also host the assets with Workers too. Those are some of the key ones I would say.
Jeremy Jung 00:20:49 And when you mentioned how D1 is like SQLite but it’s really designed to, I think you said it’s almost like having one database per user. Is that because the size of the database is limited and the Io in terms of simultaneous writes and reads is limited? What are the limitations or differences in design there?
Ashley Peacock 00:21:10 The limitation I think is probably exclusively the storage. So I think they’re limited to 10 gig, which obviously per user’s probably generally fine but if you have thousands, 10, thousands hundred thousands of users, it’s not going to scale to meet that demand. So yeah, it’s definitely designed in a way that it’s split by tenant or user or some sort of other configuration. In terms of like what I would use, I would probably use an external database for now because it doesn’t have things yet that it needs. Like at the moment bindings are static so imagine you create tender bases with 10 different IDs, it becomes really difficult to bind those. So you could add a binding for every single user but if you have 10,000 users it’s going to be a lot of bindings. I don’t think they’re going to let you even bind that many things. And secondly, like I said, everything is global but with D1 the database is created where you first make the request or you can give it a suggestion of where you want to be created.
Ashley Peacock 00:22:09 So Europe or North America and so forth. And I think this year read replicas will drops, then you’ll have a truly global database and there have been murmurings of like dynamic bindings which you would imagine would be something useful for this. But I think at the moment a lot of people that are using Cloudflare are using other database providers because it’s, it’s easy to connect to. Neon is a really popular like Postgres serverless database. You can connect it to any server full Postgres database such as Aurora and they have a product called Hyperdrive that’s quite cool because you can imagine if you are in a service environment, those functions are spun up and spun down all the time and if you have like a connection string, it takes a while to connect that database. It might take 500 milliseconds to connect to that database, but with Hyperdrive it basically creates connection pools. So Cloudflare will do it automatically for you. So rather than each time you’re having to connect to your Postgres database and handle the security and everything, those connections are ready to go. So that removes that problem and I think it’s a really nice solution to allow people to use external databases with Cloudflare that, like I said, most people I think tend to use external ones for now.
Jeremy Jung 00:23:16 And with that Hyperdrive example, would that be where you create a database in, for example in AWS and in your TOML file or your config file, you are specifying your connection string and all that stuff and you’re saying that I want Hyperdrive to connect to my database then at runtime your application will know okay I’m connecting to Hyperdrive to do this and not directly to the database.
Ashley Peacock 00:23:43 Yeah, exactly. So it basically works out the box so you don’t need to change anything like obviously apart from configuring hyperdrive, but it will, yeah it will run the connection pools for you and with a little bit of configuration you can use all the SDKs you would normally use because it’s just pooling and effectively proxying the requests. So yeah, from what I’ve heard at least it’s out of the box. You just plug it in and it’ll work.
Jeremy Jung 00:24:07 And with the key value example, you mentioned that it’s like Redis but it’s more for read heavy workloads. What’s the limitation on writes? Is it just, it can only accept a certain amount per second?
Ashley Peacock 00:24:21 In terms of writes? You can write unlimited times per day once you’re on the pay plan, that’s just $5 a month. And in that pay plan you do get a lot and you can write to the same key once every second. But the issue is more, it takes 60 seconds for that write to prop gate. You can imagine you are using Redis, right? You often write, read, write, read, write, read. But that’s just not going to work. And if you have users around the world, so like if I make a request to a Worker for example, we haven’t touched on it but that Worker is spun up as close to the end user as possible. So if I make a request I’m in London, it’s going to spin up that Worker in the London data center and then wherever you are in the US it’s going to spin up a Worker in the US close to you and the latency will be much quicker than if you are hosting it in an a s region where we both have to go to different places.
Ashley Peacock 00:25:13 Maybe I have to go to America or you have to come to Europe, it’s high latency. But what you have then is when data is written, let’s say to KV, if your Worker is in the US it’s going to write to KV in the US and then it’s going to have to propagate around the whole network. So if I then one second later go and access or read from exact same key, I might get the old value not the new value. So it just makes it a little bit difficult to support like high write workloads, it’s designed really as like a heavy, heavy cache where you are infrequently writing I think is what it’s designed for. And I think it was one of the first kind of products alongside Workers because I think people were doing things like Auth at the edge where you can write someone’s JJBT or whatever into the cache and then it’s just in there and you just, that’s something where if someone’s like firing through pages or page after page after page, you probably want to cache that content and that’s really heavy read write because you log in once you write the token tops are good.
Jeremy Jung 00:26:12 That’s 60 seconds for propagation when you perform the write and let’s say that you use the Auth example, somebody gets their JWT, it’s written to the key value store that’s close to the user. I’m assuming that should be reflected pretty quickly. The 60 seconds you’re talking about is more that if that cache is read from another country for example, maybe that read replica I guess you could say that may not have the update for up to a minute later.
Ashley Peacock 00:26:43 Yeah that’s exactly correct and that’s primarily because there is effectively like tiers within the cache. So within a country you might have multiple different data centers and each one of those will have their own kind of cache. And then you might have a regional kind of central data center where it’s like the next tier up and then you have the kind of very central, the kind of really big data centers that are like the central stores. That means the Worker is going to write when the writes happen they need to go to that central store, right? So they’re also a little longer latency because it has to go to one of those central stores and then from there yeah it propagates across the entire(?).
Jeremy Jung 00:27:19 Yeah I can see that being a little bit tricky for you as a developer where you have to account for the fact that anything you do, right, if it’s the same user who wrote it reading it back, you have a much higher chance of it being updated sooner. But there’s, since you said it’s eventually consistent, there’s no guarantee that it will.
Ashley Peacock 00:27:38 Yeah, that’s exactly right. So it’s just one of those considerations you have to have when you’re using it that that’s if you went in and you sometimes you see people like they’re expecting Redis and it’s not Redis and if you don’t read the docs and understand then that’s going to be, it’s going to be a bit of a shock when you write and you’re building something and you send to your friend and they’re like, oh something’s broken. Like it’s not getting any data or something in it. Yeah, just it might take you a while to work out unless you understood.
Jeremy Jung 00:28:02 Does that also apply to D1, the SQLite equivalent as well?
Ashley Peacock 00:28:07 No. So at the moment when you create a database, that database exists in a single location. So all your writes are going to that one database if you like. So this year I think they will introduce read replicas and I don’t know how long the latest would be, they haven’t really kind of said, but I suspect it’ll be a lot lower. Because you can imagine for a database that’s designed to be more transactional, you can’t have 60 seconds for a write to propagate. So my assumption would be that the latency is going to be a lot lower and even with Postgres or whatever right there is some latency with the re replicas so I assume they’re factor in and it’ll be, I don’t want to say it’s going to be because I haven’t seen but I’m hoping it’s going to be what you’d expect when you have a database with replicas and that sort of latency that’s pretty typical.
Jeremy Jung 00:28:53 So at least for the moment since there aren’t the read replicas, you should be able to treat D1 as if when you make a write it actually went to disc if you were using SQLite as an equivalent.
Ashley Peacock 00:29:06 Yeah, for sure. And there’s definitely use cases for D1 I think if your data’s not going to be huge or unless say you’re processing some asynchronous workloads, the data is very temporary, maybe you write it and then you delete it, maybe you’ve got some messages flying around because I think we didn’t talk about it earlier, but Cloudflare has a queues product where it’s effectively sending a message to queue and then you can have consumers pull from that queue. So maybe you have some sort of transient load where you can push that data into D1 or if you just know your data’s not going to be huge, it’s perfectly fine. So there are use cases for it. I think what I would say is if you are going to use D1 because we talked about the database is central and the Workers can be spun up anywhere in the world across 330 cities that Cloudflareís in and they have a clever little tool or configuration called Smart Placement.
Ashley Peacock 00:29:56 And what that effectively does is, and this doesn’t work just with D1, it works with anything. So let’s say you’ll spin up something in a Worker and you have something hosted in AWS database or even just hitting like an E2 image or even just hitting like an API from a SaaS like from your backend, maybe like open API or whatever it might be. What Smart Placement does is, it learns over time as requests come in what you are hitting downstream and it records the latency and over a period of let’s say 24 hours, it learns your traffic patterns and what you are hitting and it’ll basically move your Worker and spin them up in the most sensible place to reduce the latency. And that’s because you are then moving the Worker away from the user perhaps.
Ashley Peacock 00:30:38 So they might have a slightly higher latency to get to the Worker but if that Worker is then making five calls the database or three calls to some APIs that are all hosted in let’s say America and your Workers in the persons in Europe, putting that Worker closer to those downstream services is going to reduce the latency because it’s talking a lot more to those services. So I think that’s a really nice thing that you can use and there’s no harm in enabling that and it’s completely free to use alongside Workers. So there’s no downside to using Smart Placement.
Jeremy Jung 00:31:09 And that Smart Placement, would that apply to all your users? Basically like if I have users in Europe and users in the US you were talking about how Smart Placement may move the Workers closer to where the data is stored versus where the user is stored. Is that sort of applied globally where regardless of where I’m connecting in the world Smart Placement may decide oh, I should put this Worker in Virginia because that’s where the data stores is connecting to are?
Ashley Peacock 00:31:38 Yeah, it’ll effectively just monitor all the traffic going to your Worker which obviously I guess it depends on your app but it’s likely to be from multiple places and then it will work out after it’s learned and obviously it might adjust over time so your app changes, right? So it’s not static so if you remove some API calls or whatever it’s going to learn but yet it will adjust for global.
Jeremy Jung 00:31:59 And you mentioned how there’s R2 which is very similar to S3. Are there any large differences between the two that people should be aware of?
Ashley Peacock 00:32:09 I don’t think there’s any huge differences. I think if you use some of the more niche S3 kind of features then there is a page with the compatibility on that you can look up to see what’s non implanted. But they are definitely like quite niche things like pre-signed URLs for example. You can do those with R2 and it provides, I want to say the exact same kind of resiliency and uptime guarantees that S3 has, which I think is something stupid like 11 nines or nine nines, I donít know how many nines it is but it’s a lot of nines and I think R2 provides or at least claims to provide the same uptime that S3 gives you. So I think probably for 99% of usage or maybe 98% who knows, but very high percentage is going to be you could lift and shift and it’s probably going to work out of R2 rather with S3 and then you just, you save on the ingress and if you did want to move there are some tools from Cloudflare to help you move.
Ashley Peacock 00:33:01 They kind of have funny names but one of them is called Sippy and it’s effectively like an in-flight migration when something is requested from the R2 bucket, if it’s not in the R2 bucket it will pull it from S3 at that point in time and then all future requests will come from the R2 bucket and then they have one called super sleeper(?) which effectively is just like a mass migration tool. So it’ll pull all of your data out of S3, put it in R2 in one big kind of batch. So there are some useful migration patterns to help you move, if you want to move because I think one of the challenges that Cloudflare has is obviously AWS has quite a moat and one of the trickiest things right of moving an app if you want to move is the data, right? It’s not normally the apps that are hard to move, it’s the data. So I think having those tools, at least for R2 to help you migrate is definitely useful.
Jeremy Jung 00:33:47 You mentioned there’s a queue product as well. Is that something you see a lot of people using or is that more niche or early?
Ashley Peacock 00:33:56 I don’t know how widely it’s used. I don’t think it’s niche because it’s obviously if you can offload something asynchronous, right? It’s kind of nice to do and I think if you didn’t have accused product there’d be some kind of raised eyebrows of how do I do anything asynchronously? You just can’t. I think that’s a no go. I think what’s interesting is in beta at the moment there’s a product called workflows and a really kind of popular thing that’s been around lately is durable execution. So if you think AWS step functions or I think it’s temporal Io and effectively what it is you lay out a list of steps that you need to complete a workflow. I donít know if this is universe across the board but generally speaking, they will recover from outages without losing where they are on the workflow. So if imagine you have 10 steps and it gets to step five and you need to call the downstream API, if the API call fails just because they have a momentarily blip, it will effectively retry the workflow but it won’t rerun steps one through four, it will jump straight back to five and it will basically store the data in cache if you have transient data and pull it out automatically for you and run through that workflow.
Ashley Peacock 00:35:07 And I think it’s really becoming popular because of things like AI and agents, and you can imagine these kind of long running processes that act as workflows. Like if you want an AI agent to write a blog post right, you need, I saw someone building one that’s like hundreds of prompts and hundreds of requests and if one of those just fails you don’t want to re-prompt a hundred failed requests and it’s going to be a bit of a nightmare. So I think the interesting thing is that that workflow product I think is getting a lot of usage even though it’s only in beta and I can see it taking a kind of quite a market share from queues but it’s also, it’s not really designed in a, you think about a queue, it’s more kind of transactional in that you are having interservice communication with queues versus workflows. It’s kind of just safely offloading and being able to recover from failures and that kind of thing. But yeah, those two are definitely quite interesting.
Jeremy Jung 00:35:56 Yeah, I think one of the reasons I asked about the queue is I think anytime you have something where you’re running things like the functions as a service, you often need to have a lot of different services talk to one another, you need some kind of queuing system and it feels like that has to be pretty robust and that has to be able to be done in a way that’s like not super complicated or super confusing. And so I’m trying to get a sense of either with your own projects or projects you’ve seen from other people, are they using Cloudflare mostly for let’s say the CDN aspect and the serverless functions aspect but not necessarily to chain a whole bunch of things together and build some kind of more complicated workflow?
Ashley Peacock 00:36:45 Yeah, I would definitely say the CDN aspect is their bread and butter because that’s why they start out with huge, huge enterprises like the biggest companies in the world are using Cloudflare for their CDN. So it’s definitely the most premier pro they have. But I think their dev platform is at the point where it’s starting to get traction and gain some market share. Like if you look at the stack overflow, what’s it called, the developer survey they do each year you can kind of see the Cloudflare is slowly moving up if you go back five years it just didn’t have the maturity around the products to realistic challenge AWS in terms of deploying your applications into Cloudflare. I think in terms of queues, I don’t think I can speak to like how often it’s used. I don’t think I have a good read.
Ashley Peacock 00:37:34 I have used it myself and it works very nicely out the box and I think it definitely is easy to use. It’s just writing a message to a queue and then you basically have a Worker that is basically access a consumer and it’s as simple as defining a function on your Worker. So like the fetch function is what is used for HCB guests and then you just define a separate fetch function that I can’t remember what it’s called, but it basically is, it’s there and whenever a message is received your Worker is spun up and it will consume those messages and Cloudflare will handle things like scaling the consumers. So I can’t remember the limits, but it will scale your work horizontally based on demand, which I think is a really nice feature out of the box. You don’t have to worry about things like Kafka and like partitions and all that kind of fun stuff.
Ashley Peacock 00:38:19 You’d have to worry about any of that. And when we talked about local Dev, if you want to test this locally again you don’t have to go and find a docker container for Kafka and spin that up and then connect everything together. You can just test it locally and run those two Workers or run them in the same Worker however you want to do it and you’ll be able to see your messages go into a queue and then be consumed locally which I think is a really nice kind of DevEx thing that we were talking about. And you were talking about I think interservice communication, right? Being in a microservices architecture for example, you have lots of services talking to each other and when we talked about bindings earlier, we didn’t really talk about service bindings, which are how different Workers communicate and there are different flavors.
Ashley Peacock 00:39:01 The kind of like older version that’s still very much used is you can define a binding between two Workers then those Workers can communicate with each other and in terms of your code you call fetch, which in JavaScript is just basically making a HTP request and it looks like it’s making a HTP request but under the hood it’s not really, it’s using Cloudflare kind of internal networking magic to basically run the two Workers probably basically side by side and there’s no actual networking so it’s super quick, it’s super reliable and you don’t have the typical fallacies of cloud computing that you often, hear touted about, the network’s not reliable bandwidth’s not limited so forth. There are obviously still concerns but they’re not as such a big concern because you’re not going over the internet, right? You don’t have the HCP networking.
Ashley Peacock 00:39:48 And more recently to make it even more interesting is they introduced the ability to do RPC. So you can again connect two Workers together but rather than using the fetch method where you have to like serialize and de serialize Json on both sides, you can actually just effectively call methods on between two Workers and as long as what you are passing is serializable in Json, so objects, strings, numbers, et cetera, it looks like you’re making a local method call but it’s actually calling your Worker that’s completely deployed separate and then it obviously returns you something that is serializable so it might return you an object, it can even return you like functions that you can then call,it really is quite cool.
Jeremy Jung 00:40:28 Yeah, so it sounds like for the service-to-service communication, whether it’s the fetch replacement or the direct RPC or the queuing system, there are the building blocks for somebody who wants to have these more complex scenarios.
Ashley Peacock 00:40:44 Yeah, absolutely and I think it just makes it easier I think like having these, I don’t know if you’ve come across these kind of things before, but when I tend to speak to people they’re like oh that sounds like they haven’t heard about these kind of things before because I think those service bindings and obviously RRPC is not new, right? But it the way that it just is kind of built into the platform, you don’t have to do anything, right? You’d have to configure the RPC or anything, you just say connect this Worker to this Worker in some configuration and Cloudflare handles the rest. So I think it’s very powerful.
Jeremy Jung 00:41:11 And something I think you mentioned before the call that you thought was unique to Cloudflare and something we should talk about are durable objects. Can you explain what those are?
Ashley Peacock 00:41:21 I’ll try my best because I think even if you sometimes speak to someone from Cloudflare, it’s one of those things they struggle to market I think sometimes because it, yeah it’s tricky but you can effectively think of a durable object as it’s like a class that you define in your code. So it looks like a regular draft off script class, and you write them, the methods as you as you normally would. But what happens is when you create an instance of this class or when cloud creates an instance of this class, we’re using that RPC technology we just talked about you’re effectively acting on something running on their web server. So you can think of it like instantiating a class, but you are doing it with Cloudflare SDK and it gives you a stub and all the methods that are called will be calling kind of an instance of that class that’s on cloudless data centers but it’s durable.
Ashley Peacock 00:42:07 So it comes with built-in storage. So it has a key value store, it’s not KV, it’s the little key value store and it also has a SQLite database in there as well. So it’s sort of like imagine being able to deploy on demand because you can spin these up in milliseconds, right? You don’t have to deploy them ahead of time. It’s effect by being able to deploy billions of tiny little mini servers around the world. And when I say billions it’s because when I’ve spoken to some people, there are literally some customers who have literally billions of durable objects. And the nice thing about it is because Cloudflare’s platform is serverless, even though you have these mini servers, you’re only charged when they’re running. So if you make a request, it runs for two seconds, you get charged for that two seconds and then it will be evicted from memory, and you don’t get charged anymore.
Ashley Peacock 00:42:55 And I’ve never heard of anything like this anywhere else. And what they were initially kind of marketed at is a way for multiplayer Realtime communication like, Google docs, that kind of stuff because they also have web sockets built in and web sockets are quite complicated, but it effectively implements everything for you that you need to do for web sockets. And it has all these nice little features like storage web sockets and it’s just, it’s very unique. I’ve never come across anything quite like it. So it is hard to explain but hopefully you got it.
Jeremy Jung 00:43:24 So when you mentioned it has the web socket support, are these durable objects things that the client like the browser would be able to interact with directly or is this for interacting from Workers?
Ashley Peacock 00:43:37 No, so it’s, I don’t know if you actually do it from Worker and that’s an interesting one but it’s definitely mainly used from the client. So what will happen is you’ll call a Worker and that Worker has a durable object binding on it and all that binding is you basically tell it the name of your class that’s defined in your code and then when you want to create a new instance you just call it a function on that binding and say hey, give me a new one with this ID. So you could create, let’s take Slack or Discord or any of this kind of chat-based applications. Each channel could effectively be a durable object. So let’s say you are logged into Discord already because that’s not handled by durable objects or anything and you want to join a brand-new channel.
Ashley Peacock 00:44:20 So you go through the search, through the list, you find one, you click on it, and you hit join and at that point you need to be able to receive messages from that channel, right? And that’s all handled by your job object. So what will happen is on the client side it’ll make a request and in the browser there’s a library for handling web sockets that just ships with the browser and you basically give it a URL and you can pass any parameters you want. So in this case it would pass the channel ID for example, which in Discord is like a long number. And let’s say we are using that channel ID to effectively key our durable objects. So for every single channel that exists in in the entirety of Discord, there is a durable object that represents that channel. And when the client makes a request to the Worker, it basically says is the client trying to upgrade to web sockets?
Ashley Peacock 00:45:07 And that’s basically like a HTB header that is just set on the client automatically. When you request a website, again if you’re creating a brand-new channel, you’ll obvious need to create the durable object but let’s say it already exists and you just want to join an existing channel. You basically say durable object, create me a new one and pass in that channel ID as its primary key if you like. And then what the durable object will do is it will create a client and server web socket pair. Because when you’re dealing web sockets, there’s always a client and a server and they communicate with each other and they have a connection, the durable object will effectively accept the server-side connection because then it can receive messages and it’ll pass the client web socket back to the client. And then again on the client, the client also accepts the web socket and then when they’re both accepted, they’re connected and then let’s say okay, I’ve joined the channel and I want to say hello.
Ashley Peacock 00:45:55 So I type in hello press enter. And effectively on the client side again where you are just using the browser, the browser APIs, the combo web sockets, I think you just call them a method called send web socket message or something and you can pass whatever you like in there so you can pass a string of hello, but the chances are you probably want to send a bit more information like who sent the message and all that kind of jazz. And then when they send that message, you’ve defined a function on your durable object. There’s automatically called by Cloudflare whenever the client sends a web socket message to the server and then your durable object will be like, oh hey I’ve got a message, it’ll wake up, it’s got built in storage. So let’s say all the storage is kept in that durable object. So you can create SQLite tables to store all the messages.
Ashley Peacock 00:46:39 So it’ll probably want to write it to that storage so that when the next person joins write, it can retrieve the history and send the most recent messages for people that joined after initial messages sent. And then it can obviously broadcast it to everyone in that channel. So let’s say there’s a hundred people in that channel, the durable object keeps a list of all the clients that are connected, and you can basically just retrieve that list of clientís loop over them and just broadcast that message. And that’s fundamentally how it works, at least for web sockets. It sounds a little bit complicated because there’s obviously a lot of steps but like if you have been in code, it’s really simple and it’s really a small amount of code.
Jeremy Jung 00:47:14 And that particular example was with web sockets, but it sounds like you can interact with them using a CTP call as well, just regular ones.
Ashley Peacock 00:47:23 Yeah, so I think durable objects are a way of representing business entities, if you like, in your application. And a really easy example might be, let’s say you are handling support cases. So, imagine you are using AWS or using any service out there where you hear a problem, you can submit support requests, right? And the traditional way to do that will be, you might have some database tables and all that jazz, it’s all stored there. But with durable objects each time a new case is created you can basically create a new durable object. You can have the storage within there and then you can just define some methods that your Worker calls that interacts with that kind of entity. And itís kind of just wraps everything nicely in a bow. And, because you can do things like web sockets, although you don’t have to like I said, you can just have this working on regular HTP for example and just you press the dead button and it send a post request, it calls the Worker so on.
Ashley Peacock 00:48:21 And the drill object, I was playing around with something the other day where a lot of these tools are email based, right? You send an email to an email address. A nice little bit of functionality you could do with Cloudflare is you can ingest programmatically email. So you can have a Worker that fires when you receive an email. So you can imagine you have a support email, you trigger this Worker, it creates a terrible object and then you can know you can ping pong messages back via email. But what I was also able to do is there was a link in the email that allowed them to do live chat and they just pressed the link and then it has an ID for the durable object and then you can switch from email to live chat just like seamlessly and everything is encapsulated within this durable object and you can, as I said earlier, spin up an infinite number of these things pretty much so they scale massively and it kind of changes a little bit how you think about building your applications just because it’s quite different to have this kind of functionality if you like.
Jeremy Jung 00:49:16 Yeah. So it, sounds like you have this SQLite store, this key value store and you can have all of your clients share these objects. Any client who needs to have access to the same information. Like the example you gave was like a Discord chat channel. Everybody in the channel would want to see the messages that other people are sending so they would all be interacting with the same durable object. I think what some people might be wondering is, earlier we were talking about D1, which is the SQLite offering and KV, which is the key value store. What’s the distinction for people who want to store data and share data between using those services and using durable objects?
Ashley Peacock 00:50:01 I think my opinion on it would be that the durable objects you can probably imagine, they’re a little bit tricky to grasp. So I think Katie was there because it was the very first thing that they built to work with Workers. It’s and still heavily used for liking those high read cases. But I think if I was going to start a brand-new project and I was going to reach for something that I wanted to like store my data and like I said, with D1 at the moment you need to split by 10 and it’s a bit difficult to do. But with durable objects, like I said, you could spin those up on demand and I think they actually have higher storage. I think you can store 50 gigs in a durable object, which obviously if you’re thinking about a list of chat messages, that’s a lot of chat messages.
Ashley Peacock 00:50:40 And obviously, you can have things in place where it archives off data and stuff. Right? Because if you think about Discord, how often do you need messages from like six years ago? Probably very infrequently, right? I think D1 is there. I wouldn’t be surprised if it’s there to make people feel comfortable because when you think about, imagine if you didn’t have a database, like you went to the Cloudflare and there’s no database, you’d be like, huh, they’ve got everything but where’s the database? And if you didn’t tweak, they have this powerful durable object, you might just kind of discount it as not having the thing. So I think it’s a tricky thing because then they’re also relying on education and teaching people about durable objects and how they work, because they are very different. What I would say though is for some data D1’s always going to be better.
Ashley Peacock 00:51:25 I think particularly when it has read replicas because of the nature of them. Durable objects, when they’re created, they exist in that place. So if your request comes into your Worker in Europe, that durable object will also live in Europe indefinitely. So even if we were communicating via a durable object, it would either be in Europe or it’d be in the US. And with D1, once they have read replicas right, you are going to have lower latency. So yeah, if you have stuff that’s you’re not going to breach the limit of D1, I would definitely reach for D1, particularly when they have read replicas. But if you do need that global coordination and there is a lot of stuff that needs that global coordination, right? Like you think we’re talking multiplayer and Discord, but Google docs for example, right? You can see where people know like Mirror, all these tools, you can see where people’s mice are in real time, where they’re clicking the typing in Realtime, right? There’s so much Realtime stuff that’s really tricky to coordinate and that’s why there’s only one durable object or one instance of that durable object at any one time is to enable this kind of global kind of communication.
Jeremy Jung 00:52:27 And has it been stated by Cloudflare themselves that there will never be the equivalent of a read replica? This will be a single instance?
Ashley Peacock 00:52:36 For durable objects?
Jeremy Jung 00:52:38 For durable objects.
Ashley Peacock 00:52:39 Yeah, I think it’s by design. So I’ve never seen anything about read replicas because I think it’s one of those things where it’s there to enable global synchronization across many clients and it’s also designed to remove some of the problems you have with that. So it’s single threaded for example. So if you have multiple requests, come into it, they will be queued. But then that removes the problem of things like transactions and, multiple writes happening in different places and all that kind of stuff. So there are benefits through them and they do have, even though it’s single threaded, they can go up to a thousand requests a second. So they’re by no means small, they’re small than mighty I guess I would say in nature.
Jeremy Jung 00:53:15 Yeah, I mean that also may help people make a decision in terms of when they make sense because I’m assuming if you’re single threaded, even if you have a read heavy workload, at some point you’re going to hit the ceiling depending on how your application is designed.
Ashley Peacock 00:53:32 Yes. But I think because they’re designed to be split in whichever way is sensible, I think you’d have to get to like enormous, enormous scale before you hit that problem. Like you can imagine even the busiest of Discord service, right? They’re not that busy. I don’t know if I’ve ever been a Discord where it’s a thousand messages sent per second. I’m sure maybe there are, I’m sure there maybe are some, some crazy ones during like sporting events maybe where there are super high messages. But a thousand requests a second is, is a lot, right? Even if I watch like a live YouTube video, right? And you have the live chat on the write and it’s like flying through messages, it’s still nowhere close to that, right? Maybe at like unbelievable scale you might hit that limit. But I think it’s definitely one of those things where upfront you need to decide a good partition. You can’t just have one durable object for your entire application, right? You need to split it by user or channel or, some kind of sensible thing. And then I think you’ll be fine for a very, very long time.
Jeremy Jung 00:54:28 Yeah, I mean if you take an example of say a corporate Slack server, then I could see maybe the entire business as a whole. Maybe there’s a thousand messages going through a second, but then if you scope it down to per channel for example, then it would be much less. So I suppose that’s all a part of your design and like you mentioned how you partition or how you choose when to create drillable objects.
Ashley Peacock 00:54:56 Yeah, exactly. It’s similar to even if you weren’t using durable objects, right? It’s how you partition your data and that’s one of the most important decisions you can make up front is how you create your database tables, how they link together and all that kind of stuff. So yeah, it’s kind of good engineering still applies.
Jeremy Jung 00:55:10 Tying all these things together. Can you give an example of a project you’ve worked on with Cloudflare and how all those services connect and what you used?
Ashley Peacock 00:55:22 I have a few. I think one of the ones I’ve been working on at the moment is AI is all the rage and I was building effectively what’s like a rack pipeline. So you can imagine you’re building something with AI and you need to do some rag, which for anyone not familiar with effectively is like retrieving like relevant context before your prompt the LM and you’re passing it some relevant context. And to do that you kind of pass the, let’s say the user’s query that they’ve typed into let’s just say chat bot and you want to retrieve all the relevant sources that you’ve stored somewhere, right? That you’ve built over time and then you want to pass back the LMs so it has more contextual information to help answer the query. And on Facebook it sounds quite simple, you have some data, and you just need to retrieve it.
Ashley Peacock 00:56:05 There’s actually quite a lot of moving pieces and I think I found that the ones on the market there was some like good ones but I think it’s one of those spaces where there’s a lot of room for people to kind of go in and do it. So I try building and in terms of what you need, you need to be able to create kind of databases if you like, like knowledge bases and effectively each one for me was a durable object. Users can upload files. So let’s say this is basically the information that you are giving, like the knowledge you’re giving to the AI and those can be in like markdown, .csb, .pdf, all sorts of formats. So you need to have something that handles all of those. But you could just store that on R2, the equivalent of S3. And then Cloudflare, we’ve talked about the main products, but they have a vector database called Vectorize which does exactly what it says on the tin.
Ashley Peacock 00:56:52 You basically give it an embedding which is like a mathematical representation of your data and you give it to a vector database and then you can query it and say give me all the similar kind of pieces of text based on the input query. And then I tie all these things together with a workflow because I need to retrieve each individual document. You need to be able to kind of separate it. Because the document could be hundreds of pages long and obviously you don’t want to insert 100 pages in one go. You might want to separate it into like 200-character chunks or a thousand chunks or whatever. Then you need to generate those embeddings that I was talking about using embodying model. And somewhat surprisingly, we haven’t talked about AI at all, but Cloudflare does have an AI product called Workers AI where they run their inference globally.
Ashley Peacock 00:57:35 So in all the places your work can run, I’m pretty sure the, they also have GPUs that run a number of like open-source models. They don’t have any of the big ones like Anthropic open AR, anything like that. But they do have the LA models, they have the deep seed model and stuff like that. And some of the open-source embedding model as well. So I use that as well. And then once you’ve generated the embeddings, you need to insert them into the VEX database. And then after that, because you’ve chunked the document, you then need to store each chunk somewhere. Each chunk is goes into the durable object effectively and all these things tied together. So it’s probably the project I use that uses the most amount of Cloudflare services. The other one that’s a lot simpler and maybe a bit easier to grasp because there’s lots of moving parts in that.
Ashley Peacock 00:58:15 One is I’m quite into like my eSports, so like competitive gaming and me and my friends, we all play League of Legends for anyone that plays and we also like watch eSports and we were kind of wanted the schedule of the games and we also wanted to know when the games were happening live because sometimes we’d forget. So I built a Discord bot where it scrapes the content off the official website. It pulls out in the schedule, and it also takes like screenshots of the standings that you can get. And most importantly it runs the work on a five-minute schedule. So you could, you have Cron that you’re available like it used to and it just spins up the work every five minutes and it basically checks if there’s a live game and if it is, it posts like a thread on Discord and says hey there’s a game happening. Like, and then people will just jump on and chat about the game. So they use a few different ones. Like I said, like uses Worker uses D1 for scraping, like we haven’t talked about it, but they have a product that’s basically like puppeteer as a service or browse rendering where you can basically websites programmatically and scraped and screenshots and stuff. So yeah, those are probably a couple.
Jeremy Jung 00:59:15 You’ve mentioned the languages you can use, and you’ve mentioned the different services they have. In your experience when you write, like let’s say your JavaScript or your TypeScript code, is the majority of it agnostic to the platform? I mean outside of the interactions with the specific services, do you feel like you have to write code that’s specifically for Cloudflare or is it more that there’s still large parts of your code that if you needed to move to another platform, you feel like you could?
Ashley Peacock 00:59:47 I think by and large the vast majority of the code you write will just be like vanilla Typescript or on the JavaScript or obviously you might use packages right from NPM or wherever you want to get them from. But because at its core it’s the same runtime that runs in your browser. Or in some cases, like I said, Cloudflare has ported node compatibility. But obviously Node is a very well-known runtime. It’s almost like a smaller runtime than let’s say Node for example. If you want to take this and run it somewhere, let’s say Lambda, that is full node, it’s much easier than doing the reverse, right? So I think that’s also one of the challenges of if you do run a full node environment, you can move it to Cloudflare and you can be like, oh this library isn’t available, so it doesn’t work.
Ashley Peacock 01:00:31 Right, which happens a lot less now. because like I said, they’ve expanded the compatibility but it’s a lot safer I guess to go from something that’s more kind of niche or condensed and that makes it easier to move it elsewhere there. Obviously, there are things like the bindings, right? Those are entirely proprietary to Cloudflare. So those would cause you a problem. I think if it were me and like if I were building something that I would stand the test of time, I’m still, I’m going to write it in a way where, let’s take the ports and adapters like architecture pan, right? Where you basically encapsulate anything that’s external to your application is encapsulated in an adapter. So let’s say you’re talking to D1 or KV or whatever it might be, I would wrap it in a class and then all communication goes via that single class.
Ashley Peacock 01:01:20 And that way, let’s say I want to move, right? I’m not happy for whatever reason, all I have to do is change the code in the adapter. And because I’m returning objects that are agnostic of Cloudflare, of course you have the data, right? You have to move the data that it’s going to be the tricky bit, but the actual code, as long as you’re doing it in a way that is there’s good engineering practice in there, I think you could limit the blast radius of the impact if you had to move. And like I said, the core of it will absolutely be movable because it’s like ranked JavaScript in your browser.
Jeremy Jung 01:01:51 As we wrap up, if somebody wants to get started with Cloudflare, you have a book, how do you recommend they get started?
Ashley Peacock 01:01:58 So if you want to get started, obviously my book will be a great introduction so you can get that directly from the Pragmatic Bookshelf. That’s the book. And it’s also available for Amazon and all the kind of big book retailers out there. I think it covers everything we’ve talked about. So all the services we’ve talked about, Workers and everything like that. The Docs on Cloud’s website is pretty good. They’ve just rewritten a lot of them. So if you don’t want to buy the book, then absolutely the docs, there are a ton of tutorials on there that are great to get style, lots of code. Cloudflare themselves have a YouTube channel; they’re slowly adding more things on. I think if you want to see, I think it’s one of their dev rail people go through, I think it’s like an hour-long video of like a Workers 101, I think it’s called.
Ashley Peacock 01:02:41 And that’s a really good primer on getting started with Cloudflare. And outside of that, like I mentioned, if you’re the kind of person that just wants to learn by hacking and slashing something away, you can go and sign up for a free account right now. You’ll get access to everything I talked about apart from durable objects. And there might be one or one or two more that are exclusively on the paid plan. But yeah, you could split up a Worker, use D1, use workflows whatever it might be today. And then just, yeah, get learning yourself. And also if you do get stuck, like I said, you can reach out to me. Or there’s also the Discord, which is like, I know 70,000 engineers are on that, on that Discord. And everyone’s really helpful. It’s like a really nice community where people post I’m stuck with this. And people will reply and talk how to talk about things. So that’s also a good way to get into the community, I think.
Jeremy Jung 01:03:28 And if people want to reach out to you, how do they find you?
Ashley Peacock 01:03:32 If you want to reach out to me, you can find me, I think this one is probably on X, Iím just called Ashley Peacock.
Jeremy Jung 01:03:39 Ashley, thanks so much for chatting with me about building apps on Cloudflare.
Ashley Peacock 01:03:43 No, thank you. It’s been fun. I really enjoyed our conversation, so thank you.
Jeremy Jung 01:03:46 This has been Jeremy Jung for Software Engineering Radio. Thanks for listening.
[End of Audio]