Using Momento with Cloudflare Workers
Presented by: Khawaja Shams, Chris Price, Gift Egwuenu
Originally aired on October 2, 2023 @ 2:30 PM - 3:00 PM EDT
Join Khawaja Shams CEO and Co-founder of Momento, Chris Price, Software Engineering Manager at Momento, and Gift Egwuenu, Developer Advocate Cloudflare as they demonstrate how to integrate Momento with Cloudflare Workers.
English
Transcript (Beta)
Hi everyone, welcome to the Cloudflare TV live segment. I am Gift, Developer Advocate at Cloudflare.
And in today's segment, we're going to be talking about Memento.
So using Memento with Cloudflare workers. With me today, I have two special guests and I'll allow them to do the honors of introducing themselves.
So over to you, Chris.
All right. Hi, my name is Chris Price. I'm a software engineer at Memento and I work on our developer ecosystem team.
So I work a lot with our client libraries and our integrations with cool other companies like Cloudflare.
And I'll let Khawaja introduce himself and talk a little bit more about our product.
Thank you, Chris.
Thank you, Gift. Hi everybody. My name is Khawaja Shams. I'm the co-founder and CEO at Memento.
My other co-founder is Daniela, who's not here today.
Chris is being humble. He actually runs our developer ecosystem team or maybe he is giving himself too much credit because he's technically a manager, not really in it.
I'm just kidding. Memento is a serverless cache.
You can think of us as a fast origin. We are a in -region cache inside of AWS and GCP that makes it really, really easy for people to get access to their data very, very quickly without dealing with any infrastructure.
So you just call create cache and you start doing gets and sets or dealing with advanced data structures without having to worry about the underlying infrastructure.
And the one thing that Memento is not is that it's not an edge cache.
So I and Chris and everybody at Memento was super excited about deeper integration with edge-based functionalities because a lot of our customers, when they think about caching, they think about the edge and they want to get access to a fast origin that's in region from their edge workers and marry them with their edge workers.
So it's been awesome to see this partnership between Cloudflare and Memento.
That's interesting. So I'm curious, what have you seen developers viewed with Memento in the ecosystem?
What kind of applications do you find with people?
Yeah. Yeah. Sorry. I wasn't sure if you said integrations or applications.
So, OK. Applications. Yeah. Yeah. So people are building all kinds of really interesting things on top of Memento.
So we see a lot of gaming companies that love using Memento's publish and subscribe capability.
So Memento topics.
You can build chat applications and high scale notification systems in a matter of a few hours.
So there's a blog on our website where somebody was able to build something that required 3.75 million subscribers in an afternoon.
And the really cool thing about Memento versus some of the other PubSub systems is that it's got the HTTP web server kind of baked in.
So you don't have to have Memento and then have a fan out layer on top of it.
You can connect your devices directly to Memento for this PubSub capability, which really simplifies the architecture.
The other one is customers that are trying to do large scale machine learning and feature stores being able to get to a million TPS with a system without having to worry about what the underlying infrastructure looks like is pretty neat.
So we see people doing that. And then we also see customers like Wise Labs storing thumbnails on Memento as well.
Wise has got 10 million plus cameras and they use Memento to cache pictures of interesting things that might be going around that your security camera may be capturing.
Yeah, that's cool. That's pretty interesting.
All right. So I would be interested in seeing how Memento actually works.
So Chris, would you like to share a screen? Yeah, sure. So here we go.
Can you see it all right? Yeah. Okay. So this is the Memento console. So this is kind of the simplest place to get a quick sense of what all products we have and what capabilities you can do with them.
So the first screen you see here is just a list of all the caches that you've created in Memento, but you can go inside of a cache and you can do something like set a value for a key or just set this key to be the name foo and the value foo and we'll insert it.
And now in the get up here, we can get it.
This console screen is more for debugging or demo purposes, but you can imagine doing all of this stuff using just a couple of lines of code in whatever your favorite programming language is.
We also support different data types in here. So if I wanted to create a dictionary object instead of just a simple string object, I can do that.
And then when I insert that one, I get back the value. So then we have different APIs that allow you to read just like one field from a dictionary or set multiple fields in the dictionary, so on.
So fairly typical cache stuff for at least caches like Redis and things like that where the collection data types are kind of one of the core primitives.
So we also on our console have a place where you can create API keys.
And when you create an API key, you can provide a scope for the token.
So if you wanted to give out a token to be used in a browser or something like that, you can create a token that only has read only access or read write access.
So it gives you a lot more control over which users that are interacting with your cache have which level of permissions.
And then one more thing I'll show in here real quick is our topics support.
Our topics is like our PubSub product.
So it's a way that you can broadcast messages and then receive them from other subscribers.
So in this little demo screen that we have in our console here, if I just create a topic, call it topic, then it gives me these kind of two windows here.
On the left, I can see some example code that shows what it looks like to subscribe to a topic using Momentos SDKs.
And on the right, I can see what it looks like to publish messages to the topic.
And then I can publish some test messages here.
So I can say hello. So and I can start a stream of messages over here.
This is just calling that publish API every n number of seconds. And you can see that the subscriber here on the left is receiving them.
So these are some of the core products capabilities that we have.
And one of the ways that we were kind of excited about the opportunity to integrate with Cloudflare is in addition to being able to write code in your Cloudflare worker that would read or write values from a cache, which you would potentially do for performance optimization reasons, or just as a different way of sharing data between your application code and your Cloudflare worker code.
But you could also use this topic functionality, which would allow you to do something like when one of your Cloudflare Workers gets invoked, you could publish a message to a topic and then any other applications out there that were subscribing to that topic would see that message.
So there's all kinds of interesting integrations you can imagine setting up that way.
So those are kind of a super quick tour of some of the main capabilities that we have in the Momento console.
Awesome. This looks great. Thanks for sharing.
And one thing I would like to also highlight is how we've made the integration easy to work with our database integrations at Cloudflare, where we have enabled developers to be able to set up their connection seamlessly directly from the dashboard by selecting a database integration of choice, in this case, Momento.
So I'm going to show you how this would work when you're using a Cloudflare worker's application and you want to create a cache in Momento.
So I'll go ahead and share my screen.
So right here, I have a sample worker code.
Just to confirm, you can see my screen, right?
Yeah. Okay. So right here, I have a sample worker code. And essentially what I'm doing here is I'm using the Momento SDK API to create and read from my cache.
Over here, I was able to create two different tokens that are required to connect to the Momento SDK, which is my API key as well as online 63, which is my API key, as well as the specific cache that I would like to read from.
And just over here within the fetch request, I'm setting some checks to see if the API key was actually defined, if it's not sent back an error.
And here is basically where everything happens, right?
With the new cache clients where I'm setting the configuration, the credential provider, and the specific API key.
So with that, what I'm doing is just what you shared on the dashboard crease.
I'm essentially creating a key and then sending that to the sample cache that I've created and then reading that back from the cache.
So if I go ahead and run this worker locally, by running npm start.
I also already because I'm running this locally, I already created a dot dev dot vast, which is equivalent of a dot m file for saving the API key.
And then I have the cache, the Momento caching defined within my wrangler configuration file.
So if I go ahead and open this, I'll just move my screen over here.
You can see I'm receiving a response back from the cache.
And just to see how this works, also in the dashboard, the specific cache that I created is called workers.
And I am setting I'm setting the values here, first name, last name.
And what I'm doing also is getting back the value by calling clients to get the cache and a specific key.
And that's what happens here. The interesting bit I'll also like to show you is how easy for you to do the integrations seamlessly by adding the database integration directly from the dashboard.
So over here, where I have my worker, we have a new integration stab here where you can select any database connection you're trying to make, in this case, Momento would be the one that I want to work with.
So to deploy my applications, and, you know, sets my API, normally, you'd have to like do the whole setup yourself, collect the API key from Momento, add it to your environment, just as I did it locally.
But here, you just need to add the integration.
So I'll go ahead and click add integration. And then you have to go through some setup, which would enable you connect your account, your Momento account to allow Cloudflare have access to, you know, connecting to your cache and setting the API token.
So now that I've successfully connected to Momento, I would go ahead and select the specific region that I would like this to be.
I am in the EU region. So I'll go ahead and select EU West.
So what happens is, you then get two keys created for you two variables created for you the Momento HTTP endpoints, and then the API key.
So go ahead and add this integration.
Okay.
I think something's happening where I think I've added this multiple times.
And then let me see.
Okay.
So before now, I think I already added it. So maybe deleting it and reading it would work.
Because the API token is missing here.
So let me just see if that works. So heading back to the integration stub, I'll try to add the integration again.
Set.
What's happening here is that it's actually connecting to Momento using your SSL and creating an API key for you behind the scenes, right?
Yeah. Yeah. So that works.
And what then happens is I now have the Momento API key as well as the HTTP endpoints added as environment variables for this worker.
Okay, interesting.
So this is what I was saying before, when I was running into the thing, I will try to redeploy the worker to see if interesting thing is doing that will actually rewrites, like updates the variables that I just added.
Gotcha. Yeah. Okay.
It works. Good. It's all right. Yeah. Okay. So live demos, right? Yeah. Yeah.
Because I was wondering why, why was that happening? Okay. So essentially, what I showed is how you can actually connect Momento to your workers, and to the dashboard using the database integration and creates like a cache to in this application is pretty straightforward, setting, creating a key, and then listing it back from application.
But there are so many other things that you could do with the cache, like I explained earlier, there are so many different applications you could use.
And the power of Cloudflare having data centers around several regions, it would eventually help with low latency for your users switching, coming from different parts of the world or trying to applications from different parts of the world.
So stop sharing my screen. Yeah.
So the interesting thing about the integrations that I've seen is this gives developers access to work with Momento.
So in this case, with Momento, I am very curious what the different use cases you've seen.
So for example, you have the web SDK, you have the HTTP endpoints, how it enables developers to connect to Cloudflare Workers.
What do you think about the what do you think about the integration?
And what do you see like people do with this? Yeah. You want to go first, Kawaja?
No, go ahead. Okay. I can follow. Cool. So I mean, definitely one of the first things that comes to mind is if you have a page that you need to load that has some kind of expensive database query going through it, then being able to have your end users directed to the Cloudflare API, and then have it the first time that that worker gets invoked and makes the expensive database query, saving the result off into a Momento cache will mean that subsequent executions of that worker would be much, much, much quicker, obviously.
Another thing that we think could be powerful about this integration is if you have your own web service, your own web application that is processing a lot of data in real time and needs to send out updated values from the computations that it's doing in a way that would then be accessible to the Cloudflare Workers, your main service architecture could be periodically updating a value in the Momento cache.
And then when a request comes through Cloudflare, and it's just reading that value in from the cache, you're able to kind of get more like real time synchronization of data between the different parts of your system.
And then, as I mentioned, when I was showing my screen share a little bit earlier, I'm really excited about our topics support, the PubSub support, we're starting to really think about new use cases where we're doing like event driven applications where something that happens that causes as users are invoking Cloudflare Workers, having a way to use that as a trigger to an event by publishing a message to a Momento topic, and then again, having some of your back end infrastructure, subscribe to that topic to see the events that are coming in as your users are hitting the triggering the worker and react to those in various different ways, updating metrics or updating information in your database, etc.
So those are just a handful, I bet Kwasi has some more. Yeah, I think, in general, we see a lot of gaming companies wanting to have edge aware routing, for instance, to be able to get the data into the hands of customers fast and then, but sometimes you don't have all the information available at the edge, and you do end up going back to the end database.
And, you know, you don't want to make your really fast edge worker be waiting for a slow database.
So caching from the database works, but there's a whole lot of other things that we do beyond just key value, right?
Like we can have complex data structures, like leaderboards and so forth, that are available in the cache directly.
So there isn't even a database lookup in case of a cache miss, like we can be a primary source of the most up to date data, like what is the latest score for a football game or in a particular game that a customer, you know, a set of players are playing, things that have changed, you know, they can be sorted in a list or a sorted set or a dictionary in Memento as well.
So getting access to that rich layer of data structures available quickly, when there is a miss on the edge side is, I think, the kind of, is the backbone of the applications that we think are available.
Oh, thank you.
That's really interesting. And I'm glad to hear like the different use cases that you find useful for this integration.
Well, thank you so much for coming on.
If there is any additional resources you find that people would be interested to look up Memento, especially to learn about topics, like you shared, I would add that to the, to the stream once it goes, the recording goes live, so people can check it out.
But it was really nice to have you both join me to talk about Memento and Cloudflare Workers.
Yeah, you too. Thank you. Thank you for having us and for all the integration work and the partnership.
Really appreciate it. Sounds good.
Thank you. And that will end the stream. Thank you everyone for joining us.
Thank you.