Introduction to Serverless Functions

Usecases: Ordering System, Subscription Management & Grep

Jason Lengstorf

Jason Lengstorf

Learn With Jason
Introduction to Serverless Functions

Check out a free preview of the full Introduction to Serverless Functions course

The "Usecases: Ordering System, Subscription Management & Grep" Lesson is part of the full, Introduction to Serverless Functions course featured in this preview video. Here's what you'd learn in this lesson:

Jason continues to share usecases of serverless functions, and explains how Starbucks uses serverless functions to strategize about their website and application by being aware of the costs of running specific functions.

Preview
Close

Transcript from the "Usecases: Ordering System, Subscription Management & Grep" Lesson

[00:00:00]
>> If we look at another example here, you can run an entire ordering system on serverless. So Starbucks, for example, has a really good web app and I think it's a, what is it? Order Starbucks? Let's find it here, is it delivery, I think I can't remember what their online app is.

[00:00:28]
But so Starbucks has a web app that actually the whole website is a web app now. But the ordering here, this is all done through serverless functions. When you when you add things to your cart when you sign in. When you manage this stuff, this is all built on serverless functions and that allows Starbucks to significantly control their costs because they know exactly how much a function execution costs.

[00:00:50]
They don't have to keep thousands of dollars worth of servers running to make sure that they can handle capacity instead, they know that their average throughput is however many people order through their website. And they can estimate pretty well what that's gonna cost. And if they get a spike, they just fire up more serverless instances, it scales really well and helps them control costs where they're not, they don't have to keep a bunch of hardware running just in case.

[00:01:17]
That is a really good solution and a really powerful one. Subway does the same thing, Subway's ordering is powered through serverless functions. And so there are people that you can talk to about that like on Twitter. There are some of the Starbucks team, I think David Brunel, let me find David real quick because he run this, and would be able to point you too, if there are any case studies or if he had questions about exactly how that works.

[00:01:47]
You could go find him and he could probably point you in the direction of how to learn more about how this sort of thing works. Then you could also run, I jumped two, so we'll look at the first one here. So subscription management. You can do full fledged stripe subscriptions through a service.

[00:02:08]
So let's look at the, this is an example app. I built a tutorial around this so you might recognize the layout, but if I log in. And I think hope I use the same, yep. Okay, good. So I'm logged in and I'm on the free tier here, right?

[00:02:27]
And so when I created my account when I logged in, there is a function that gets called on signup, so identity sign up, that's a special function that gets called whenever identity events happen and netlify kinda like a web hook, and I'm able to go to stripe and then I use FaunaDB on this one which Fauna is another kind of like database solution, it's GraphQL driven.

[00:02:55]
We used it in the intro to JAMstack workshop on front end masters if you want to see more of how that works. And then in this handler, what we're doing is we are creating a stripe customer. So we just created a Netlify identity you user, so we listen for that and when they create their account, we also create a stripe customer.

[00:03:17]
Then we dive into this subscription, and we associate this customer with a pricing plan. And so the default pricing plan is free at zero dollars a month, you're just on a free subscription. Then we go in and associate your Netlify user ID with your stripe customer ID so that we can check this later.

[00:03:36]
And we attach a role to your account called free. And so this is where this gets really interesting, right? So our app is able to assign roles to our users based on certain information. So in this case, whenever you create an account, you automatically get the free role.

[00:03:52]
But then if we go further, let's say I want to manage my subscription. So I'm gonna bounce over to the stripe billing portal. And it says I'm on the free plan. So I'm gonna update this. And let's say I want the premium plan. I want all that Corgi goodness.

[00:04:06]
So I'm using a test credit card. So I'm gonna confirm, update my plan. It says I'm updated. Okay, I'm happy. So let's go back. And now check that out. I can see all the content, I have a new role my role was updated, and I'm able to see the content that was, that was previously not available to me.

[00:04:25]
So if I go and look at what happened, I have a handle subscription change. So this is a serverless function that stripe will call whenever you change your subscription, it says the user is a web hook. And what I'm doing in here, is I'm going in I'm validating that it's a legit stripe call.

[00:04:44]
And then I pull up my Netlify User ID based on the stripe customer ID. And I update the role to include whatever their the plan is. So if I have the the premium plan, we get the premium role. And we add that to our roles here. So we can update our user roles based on third party actions.

[00:05:07]
The stripe subscription changes, stripe tells my app that it has changed and I'm able to update my users roles. To include whatever access they're supposed to have, and that means that I'm able to see premium content and if I go in and change it again, I can go in and update my plan to Pro, right?

[00:05:28]
And once I've updated my plan, I go back I've now lost access to the premium content, I can only see the pro and free content. So this is a really, really powerful use of this, where the code in this example and by the way if you wanna, there's a tutorial about this on, no.

[00:05:48]
[LAUGH] I guess the link change and I need to fix that there is a tutorial somewhere around here. Hear that we'll walk you through how that app that I just showed you is built, going through all the setup and the connection of stripe and everything. But this is all built on again serverless functions because it removes the need for me to set up a whole bunch of long running servers and setup all this stuff, I'm able to just tell stripe, hey, when something changes, call this function.

[00:06:18]
And then my function is just the logic to manage the subscription. And when it runs, it runs and when it doesn't, it doesn't. It doesn't cost me money to sit there. It only cost me one me money when it runs or in the case of the usage of this app, it's free because I won't hit the limit to actually start getting charged.

[00:06:37]
A kind of a galaxy brain use of serverless functions is this is something called common crawler lambda. And so, common crawl is a huge repository. Let's pull this up actually and look at it. So common crawl is like search engine crawling data for the whole internet, where it pulls everything down and creates this giant list of whatever.

[00:07:03]
You can just pull data from the internet and analyze it. This would be a great resource if you were trying to get into machine learning and you wanted to see lots and lots of websites about a certain topic. Or if you were trying to analyze headlines and things like that, you could do this is a great resource for that.

[00:07:21]
But what this does is this uses Python and serverless functions to basically let you run grep against the entire internet, which would be completely non starter if you were trying to do this on a single machine because there're billions of pages on the internet. But because you can spin this up across tons and tons and tons of lambdas, you can actually run this command against subsets of the data and do it in a reasonable amount of time.

[00:07:47]
So it's a very fascinating use, I don't know, I haven't actually used this. I've only seen people talk about it and it kinda blew my mind. But it's just such an interesting, it shows you what's actually possible when you start thinking about the web in this way. If you are familiar with David Wells, David Wells used to work for serverless which is a framework around serverless functions, and he has put together some really amazing resources on how serverless kind of works, things you can do with it.

[00:08:22]
So some of the things that he mentions are like, you could run software testing, you could spin up, say like an integration test through a tool like Cypress or visual diffing through tools like Applitools. And you would be able to then set each page or subsections of your site to run through a serverless function.

[00:08:44]
So that instead of having to run all of the integration tests in one process, each page gets tested by one serverless function, which means they run in parallel instead of in sequence. And so now you're doing a lot less work. Image and video encoding and analysis we talked about that with the gatsby-parallel-runner.

[00:09:01]
Instead of doing your images, one after the other, you do them all in parallel each in a serverless function and then you bring those results back. If you were going to dig into things like machine learning weekly look at that common crawl lambda. Data visualization would be another one if you've got a huge, huge, huge dataset and you need to do a lot of work on it, instead of doing it one entry at a time, you could do each entry in its own lambda and then smash those results together at the end.

[00:09:28]
So so many things that you can do with this, right? It's kind of, this is one of those things that it's more limited to our imagination than it is to the technical confines. Because there's not really a, there are I mean there are always limits to everything. But the the practical limits of like, I can just spin up a 1000 or 10,000 serverless functions to do some work.

[00:09:52]
Those are pretty, like those limits are yeah, you can do that you can absolutely do that. So it's more of like, all right, how would I actually make this work? How would I break this data up? How would I send these off? How would I reassemble it once it gets done?

[00:10:06]
What do I do if something goes wrong, if one of those functions fails, and it wasn't supposed to. There are things that you have to build to make that function. But ultimately, the power here is incredible, right? And any of that logic, the retry logic and the failure catching, it doesn't do that anyways in your server.

[00:10:28]
And the nice thing about this is you could even thinking about that failure case. If you're running the whole data set as one long running process and one of them fails, it just is kinda like a flakiness thing, not because it was actually wrong but because the network hiccup or something.

[00:10:47]
You have to start the whole process over again. But if you're using serverless functions, then you get a list of what failed and you can retry just what failed, and then plug that back into the data set. That gives you even better resilience. It gives you more options when you're trying to recover from errors that are just more difficult if you're doing everything as a single process.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now