Using Turso with Cloudflare Workers
Join Glauber Costa and Pekka Enberg Founders of Turso, and Gift Egwuenu, Developer Advocate Cloudflare as they demonstrate how to integrate Turso with Cloudflare Workers.
Hi, everyone. Welcome to this live segment. I am Gift, Developer Advocate at Cloudflare.
And with me, I have some special guests for this segment. So I'll let them do introductions.
Over to you, Kuba. Thank you, Gift. Thank you so much. First of all, happy to be here.
Thank you so much for having us. I am Glauber Costa, and I am the founder and CEO of Turso.
I will speak more about Turso later, but with me here, we have my co-founder, Pekka.
Why don't you just introduce me like you usually do?
Oh, because we promise we will be good guests, Pekka. So the funny thing is that me and Glauber know each other way back, and we actually spent the last, I think, decade doing stuff together.
But yeah, Pekka, I'm the CEO, co-founder of Turso.
And let's talk more about Turso, I guess. Yeah. Before we get into that, well, I'm very happy to have both of you on.
I would like to give a brief introduction of why we are having this segment in the first place.
And if you're not familiar, last week, we had birthday week here at Cloudflare, where we made tons of announcements.
And one of those announcements was adding additional integrations to Cloudflare marketplace.
And one of those integrations is Turso. So this segment is essentially going to be a space where we share what Turso is, and also show you how the integration works with Cloudflare Workers.
So I'll hand over to Glauber.
What exactly is... Happy birthday, Cloudflare. First and foremost, thank you for everything.
Cloudflare has been such a great partner of the ecosystem in general.
It's hard to imagine most of the developers' lives without Cloudflare today.
So happy birthday. How old is Cloudflare, by the way? Just around 10 years.
That's in. That's in, yeah. Awesome. So let me... I'll just get started telling you a little bit more about what Turso is.
And then again, now, while we have this introduction that Pekka is going to be demonstrating today, Turso is a database.
It is more specifically, it's an edge database. And our focus area is having a database that can be fast from anywhere and support workloads like multi -tenant, multi-user SaaS applications.
So each database is a SQLite file, and you can create thousands of those databases.
You can start for free. And we focus a lot, again, in data placement and data replication with the intention of making your database fast from everywhere.
It pairs extremely well with a player like Cloudflare, which is also putting a lot of effort, especially through Cloudflare workers, into having this global compute platform, so to speak.
So one of the things that Cloudflare talks a lot about that I love is region earth.
So how you can do compute from all over the earth, always close to your users.
And again, the focus of Turso is making sure that the data is going to be close to your users as well.
Yeah. That sounds great. I also find that interesting because I find that there is alignment between what Turso is doing and what Cloudflare Workers is doing.
So I can't wait to get into the demo.
So I'll let you, Pekka, share your screen and show us exactly what you can do with Turso.
Yeah, absolutely. So let me share my screen.
So, yeah, I hope you can see my browser window. So basically, we're just going to show a simple demo of how to build a REST API with Cloudflare Workers and Turso.
So it is a fictional e-commerce site selling mugs. And actually, as you can see on the right, it's work by James, who's a technical writer at us.
So I'm here just to take credit for his awesome work.
And in this example, we're going to use an object relation mapper called Drizzle.
It doesn't have to be Drizzle. There are other options as well.
But in this example, that's what we're going to use.
And basically, I was thinking that I'll just briefly show the code. We can go into more detail if needed.
And then I'm just going to walk through the steps. So this is, again, on GitHub.
And I think we also have a blog post about this. But basically, yeah, it's an example of how to get going.
And Gip, this is a stack that we see so much in the open, Cloudflare workers and the Drizzle ORM.
We are not involved with the Drizzle project.
For those of you who want to check out, it's an ORM focused on edge environments.
We do sponsor the project, though, because we think it's important to make sure that especially the open source community is well-maintained, well-kept.
And we're users of open source. We benefit from open source.
So we're always ready to give back whenever possible. But this is a stack that if you go on Twitter, for example, you're going to see mentions of that all the time.
I have seen a couple of it. Yeah. Yeah, absolutely. But maybe I'll just jump into the code so you can see a little bit of Drizzle as well.
So as I mentioned, this is just a simple worker.
I know if we scroll down here, you can see the exported fetch.
And it just builds this router, which is essentially the REST API.
And basically, from integration perspective, it's really centered around creating the database client.
So Torso is still in this environment, a remotely accessed database.
We have an HTTP-based protocol. But basically, the way you configure it is through two things.
You give it an URL. And this is the same URL from anywhere you access it from.
So Torso deals with figuring out what's the closest replica of your database.
And then also authentication token. Now, this is actually what the Cloudflare Workers integration does for us, which I'll show you in a second.
And basically, this is creating the client based on the configuration and then also wrapping it into this Drizzle object.
And for example, if we look at the route to get all the mugs that we're selling on this platform.
So it is really just db.select from mugs.
And this is a TypeScript type that specifies it.
And without going into more detail about Drizzle, this is essentially. And then we just return it as JSON.
And there's a bunch of other routes as well. So if we go back to the GitHub project.
So first thing we're going to do in this demo.
So I already installed the CLI. So when you're working with Torso, you almost certainly are going to install the CLI.
It is there to manage your databases and so forth.
But obviously, I already have it. So the first thing we're going to do is we're going to create a database to use with this example.
And we're going to call the database the mug store API.
So that's the name of the database. And the CLI really tries to guide you what the next steps are.
But we already have them here. So as I mentioned in that or showed in the example code, two things that we need the URL and authentication token.
I'm just going to copy paste these commands. And here I'm basically just running this command to get the URL and capturing it into this environment variable, which we will use a bit later.
And let's do the same thing for the token.
And now we have basically have the two configuration pieces that we need.
And there's two things that I wanted to show today. So first of all, you can develop locally with Cloudflare Workers.
In this example, we're always going to have the database remote.
But what we need to do is do a little bit of configuration.
So we're going to again, copy this command and basically creates this dot n file, which I will not show you because it will have my authentication token, which happens a lot in demos.
So let's just do that. And then also, this is something I didn't manage to configure with Wrangler.
So actually, it also needs this.
Because basically, what we need to do, yeah, we need to give this configuration to two entities here.
One is a drizzle kit, which is again, part of drizzle, which manages the migrations.
And then on the other hand, Wrangler, the locally running thing needs this environment or this configuration as well.
I didn't manage to make Wrangler actually eat that dot n file.
So I'm just going to copy it to an external file.
What I see also, sometimes people use the Wrangler configuration file.
But again, I don't want to put it there because it's my secret authentication token.
Then also in this example, if you just clone it repository, you will have to install the dependencies.
I did all of that already. And then before we can get into running the code, I'm just going to run these commands, which are essentially steps that you do with drizzle.
I highly encourage people to look it up themselves.
Basically, what it does, it takes the TypeScript type definitions of the mugs and all the stuff that the domain model that we have generates some SQL.
And then it has support for SQLite and also Torso, of course, which is SQLite based.
So it migrates the database for you, and then you can also seed some data.
So some test data for this example. And that's it. Now we can basically, unless there's any questions at this stage.
Glauber, do you have any questions?
No. Do you have only questions? No. I actually do have one question.
Go ahead. Where you were running the environment, the authentication token, I saw that you created that from the CLI.
So interestingly enough, the database integration on the Cloudflare dashboard would also automatically create that for you, right?
Yeah, absolutely. And I will show that. And this is basically for the case when we are now running this locally.
So we will get to the integration for sure.
This token, by the way, is not an account token, is a per-database token. So just even if we were showing now, it's totally fine, it can generate others.
Every one of your databases will have a different token.
Yeah, absolutely. So they're all separated like that.
So we'll get to the integration part just in a second.
So now we can do basically, we can start the Wrangler in dev mode. And now we will have the function running on my machine, accessing a remote torso database.
So assuming everything went well. So here we can see that sort of the route, which just shows the different endpoints.
And for example, we can see, let's look at the mugs in the system.
So it's just doing the select from mugs, which I showed in the beginning.
And that's pretty much it for the local development case.
So now let's go to the cool stuff that you guys announced last week.
So let's go and deploy the function on Cloudflare. And it just takes a small amount of time to get there.
So now what I did, and of course, I actually had the Cloudflare project there before, but I don't have the integration configured.
So now the code that I was running locally is running on Cloudflare, but we don't have the configuration for the database, right?
Because exactly what you were pointing out.
So what we're going to do is we're going to, so if we actually go to the overview page.
So this is like, this is your normal Cloudflare Workers dashboard. We can see the project that I just deployed here.
And then the cool bit that you announced last week is this integrations tab.
And there's a bunch of different providers.
You can find Torso here as well. And at least I find this super cool.
So what you do is you click on that integration. You need to give it some permissions.
And then this is actually the step that maybe now explains. So I created the credentials from the CLI, but actually what it does, it's just connecting to our API server and getting the credentials from there.
So what we do with the dashboard is we connect to Torso.
We authorize Cloudflare to be able to access the API on my behalf.
And then now this is the part where you do the integration. So we select an organization, Torso of course supports different organizations.
I created this demo database under my personal one.
So I'm just going to pick that.
And then I select a database and it should be here. So the mug store API, which we were just using, and you can also configure access level.
So I'm just going to give it full access, but we also do support read only databases.
And then this is the final bit that you were asking about.
Now Cloudflare Workers dashboard will ask, what are the...
So this has essentially managed secrets like the URL and the authentication token.
And of course we make the demo use the same secret names as here.
But basically once I do the add integration and wait a second. So basically Cloudflare Workers under the hood does the same configuration step.
And what is cool about this is that now I don't have to manage it anymore.
It can be done actually also from the Wrangler CLI and the GitHub read me explains how to do this, but this is pretty cool in terms of basically managing the integration.
And now we can just quickly run the same commands.
So we're going to see. So this is now the code is running on Cloudflare workers, which is hopefully like 10 kilometers from my house.
You have a data center there, and then we can just check the mugs.
And it, on the first try, for some reason, it uses the cached version of the secrets, the Cloudflare Workers team.
I don't know why it does that the second time it works.
So yeah, but that's basically, so it is some, it's related to something around the caching of the secrets or something, but nonetheless, so this is the way where you do it and you do it once.
So you maybe even do it before you deploy and so forth, but in the interest of this demo, these were the steps.
So that's the, in nutshell, that's the demo we wanted to show. And again, you can find the whole thing on GitHub where you can play around.
Thank you so much for sharing.
Thank you. Let me stop sharing my screen. It looks great. Like the combination of Tosu, Cloudflare Workers, and how it makes integration seamless when you're trying to deploy the application to production.
I am very curious, what are the kind of applications you've seen?
Because you mentioned that you've seen a lot of people building with Tosu, Cloudflare Workers, and Drizzle.
Yeah. What would be the best suited applications for Tosu? Yeah. No, there are many.
I would say that any, there is a combination of factors that make Tosu and Cloudflare Workers a very powerful combination.
And it's usually the fact that you expect users from different places in the world, right?
So like if all of your users are in a specific location, you might not even want to reach for Cloudflare Workers.
Although the DX alone, and the experience of running those functions, it's worth it.
But to make it like the full power of that is realized when you expect users coming from many places in the world.
And you need those things to be served with low latency.
What falls into this category? Many things. And I think the most important, the one we see the most is SaaS applications, e-commerce applications.
I spoke to somebody the other day who had absolutely no interest in using neither of our technology, neither Cloudflare nor Tosu, so in a discovery call.
And what he told me is like, look, I sell subscriptions to a service that when you sum it up, I mean, it costs $70,000 a year for consumers.
So if it takes one, you don't make a decision to purchase something that costs $7,000 a year like this.
So if the website takes a second to load, two seconds to load, nobody cares.
But we know that most e -commerce is not like that. Like most e -commerce, especially when you're selling things that have low individual value, it is a purchase that if you don't make it at that point, the user will just go away.
So those websites, I mean, those technologies are things that are prime to see this combination together, right?
Travel agencies is something that we see a lot for a similar reason.
Essentially things where you want to see a quick, snappy experience wherever you are in the world.
Absolutely, yeah. So I think, yeah, so just to add on that, so it's like a complementary, Torso is a great complementary thing for workers.
And the reason people usually go for workers is latency, but also the serverless aspect of it as well.
And when you're building those types of applications, I think usually it's the API layer that people really do on Cloudflare Workers and then use Torso for that as well.
Yeah, I agree. I agree. And also just adding Torso as your data layer, as well as it being latency-free and accessible to different users makes it just a very good combination.
Cool. Well, I am very happy about the integration and I hope that people watching this or that would watch later would be happy to try out both Torso and Cloudflare Workers for building the applications.
If you haven't heard of Torso before, and this is your first time, and if you want to check out more of what we do, we are at torso.tech.
And on the website, you're going to find links to our Discord page.
You're going to find links to libsql, which is our fork of SQLite on top of which we built the database.
Everything really you need to know about is there. And if you haven't heard of Cloudflare, I don't know, I've questioned a lot about you.
You're here, so I'll see you.
Yeah, exactly. I'm assuming that all of the audience knows about Cloudflare.
But Torso may be news to you, in which case, we're at torso.tech. Thank you so much for joining me.
If there is any other questions, I'll possibly pass it on to you, but also all the links that you mentioned, I'll have the team add it because this is going to be restreamed over the course of the few days or weeks.
I'm not sure.
But I have the links attached, including the demos, the GitHub repo, so people can easily access them.
Yeah. Cool. Thank you. Thank you so much. It was a great pleasure to be here with you.
All right. Have a wonderful rest of your day.
You too. Bye-bye. We're betting on the technology for the future, not the technology for the past.
So having a broad network, having global companies now running at full enterprise scale gives us great comfort.
It's dead clear that no one is innovating in this space as fast as Cloudflare is.
With the help of Cloudflare, we were able to add an extra layer of network security controlled by Allianz, including WAF, DDoS.
Cloudflare uses CDN, and so allows us to keep costs under control and caching and improve speed.
Cloudflare has been an amazing partner in the privacy front. They've been willing to be extremely transparent about the data that they are collecting and why they're using it, and they've also been willing to throw those logs away.
I think one of our favorite features of Cloudflare has been the worker technology.
Our origins can go down and things will continue to operate perfectly.
I think having that safety net provided by Cloudflare goes a long ways.
We were able to leverage Cloudflare to save about $250 ,000 within about a day.
The cost savings across the board is measurable, it's dramatic, and it's something that actually dwarfs the yearly cost of our service with Cloudflare.
It's really amazing to partner with a vendor who's not just providing a great enterprise service, but also helping to move forward the security on the Internet.
One of the things we didn't expect to happen is that the majority of traffic coming into our infrastructure would get faster response times, which is incredible.
Like, Zendesk just got 50% faster for all of these customers around the world because we migrated to Cloudflare.
We chose Cloudflare over other existing technology vendors, so we could provide a single standard for our global footprint, ensuring world-class capabilities in bot management and web application firewall to protect our large public-facing digital presence.
We ended up building our own fleet of HA proxy servers, such that we could easily lose one and then it wouldn't have a massive effect.
But it was very hard to manage because we kept adding more and more machines as we grew.
With Cloudflare, we were able to just scrap all of that because Cloudflare now sits in front and does all the work for us.
Cloudflare helped us to improve the customer satisfaction.
It removed the friction with our customer engagement.
It's very low maintenance and very cost effective and very easy to deploy and it improves the customer experiences big time.
Cloudflare is amazing.
Cloudflare is such a relief. Cloudflare is very easy to use. It's fast. Cloudflare today plays the first level of defense for us.
Cloudflare has given us peace of mind.
They've got our backs. Cloudflare has been fantastic. I would definitely recommend Cloudflare.
Cloudflare is providing an incredible service to the world right now.
Cloudflare has helped save lives through Project Fairshot.
We will forever be grateful for your participation in getting the vaccine to those who need it most in an elegant, efficient, and ethical manner.
Thank you. The biggest IT priorities in the state of Arizona really are focusing on modernization of legacy applications and technologies and bringing together and delivering better digital services to our citizens.
As a government entity, we face a tremendous amount of cyber interest from around the world.
We have to guard our systems against a lot of different things.
We're running a program that we're calling the Statewide Cyber Security Program.
Statewide Cyber Readiness Program, essentially providing cyber protections to all of our local government entities, including cities, counties, K-12 school districts, trying to help them up-level themselves to prevent cyber attacks across the entire state.
Most of the organizations that we work with, especially talking about some of the smaller entities, don't have dedicated full-time cybersecurity professionals or IT staff.
One of the things we looked at is, what is the ease of deployment for these tools?
And Cloudflare stands out above a lot of the others, especially in that it doesn't require any sort of deployment in your environment.
It's all just a quick DNS change.
And usually we've seen with onboarding and deployment within half an hour to a couple of hours at the very most for some of our more complicated entities.
We're getting very high-powered, very technical cybersecurity detections and protections with this platform with essentially little to no effort to deploy them.
With the Arizona Cyber Readiness Program, we were able to get access to all the feature sets with Cloudflare, including the web application firewall, bot management, rate limiting, caching, while the website stays online.
Maricopa County is the fourth-largest county in the United States.
We're about half the population of Arizona, and we're the second-largest voting district next only to L.A.
County. Leading up to the 2020 general election, I was really concerned about implementing as many possible security controls as we could to protect our systems.
With Cloudflare, if we need to block an attack, it is trivial. Within 60 seconds, I can block an attacking IP.
I see Cloudflare as a critical partner in everything we're trying to do across the state.
An attack against one of us is an attack against all of us.
And the only way that we can be truly successful is by looking out for each other.
Cloudflare is going to be an integral part of that. As part of our readiness program, it's going to be right at the edge, guarding and watching all the traffic that's coming through to hit all of those services.
And so it's going to be providing, you know, critical front-end work to make sure that our citizens are protected.
Q2's customers love our ability to innovate quickly and deliver what was traditionally very static, old-school banking applications, into more modern technologies and integrations in the marketplace.
Our customers are banks, credit unions, and fintech clients.
We really focus on providing end-to-end solutions for the account holders throughout the course of their financial lives.
Our availability is super important to our customers here at Q2. Even one minute of downtime can have an economic impact.
So we specifically chose Cloudflare for their Magic Transit solution because it offered a way for us to displace legacy vendors in the Layer 3 and Layer 4 space, but also extend Layer 7 services to some of our cloud-native products and more traditional infrastructure.
I think one of the things that separates Magic Transit from some of the legacy solutions that we had leveraged in the past is the ability to manage policy from a single place.
What I love about Cloudflare for Q2 is it allows us to get 10 times the coverage as we previously could with legacy technologies.
I think one of the many benefits of Cloudflare is just how quickly the solution allows us to scale and deliver solutions across multiple platforms.
My favorite thing about Cloudflare is that they keep development solutions and products.
They keep providing solutions.
They keep investing in technology. They keep making the Internet safe.
Security has always been looked at as a friction point, but I feel like with Cloudflare, it doesn't need to be.
You can deliver innovation quickly, but also have those innovative solutions be secure.
My name is Kyra, and I'm an office coordinator at Cloudflare.
The role itself is very varied from day to day.
I come into the office and make sure that everything is prepared for the employees, that the area is clear and works for the teams and the different jobs they have to do, help out with events on site, internal and external, make sure our fire marshals are organized, and working closely with the management of the building which our office is based in.
So it's a very varied role.
What I enjoy about working in Cloudflare is honestly the people. My job is very people-based.
The thing I enjoy the most is making the environment lovely for the employees, and the grateful attitude that I get from the employees for giving them a nice environment.
My name is Kyra, and this is my life at Cloudflare.