Azure Functions Deep Dive
Yan Cui chats to Ian Griffiths about Azure Functions, how it works and how it differs from AWS Lambda.
About this talk
Real-World Serverless podcast
In Episode #99 of the Real-World Serverless podcast, Yan Cui chats to Microsoft MVP, Ian Griffiths and take a deep dive into Azure Functions, how it works and how it differs (significantly) from AWS Lambda.
Read the transcript
Introduction
Yan Cui: Hi, welcome back to another episode of Real-World Serverless, a podcast where I speak with real world practitioners and get their story from the trenches. Today I'm joined by Ian Griffiths, who is the author of Programming C# with O'Reilly, and he's also been active in the .NET community for a very long time, and nowadays he works as the Technical Fellow at endjin. Hey Ian, welcome to the show.
Ian Griffiths: Thanks very much for having me.
Yan Cui: Yeah, so I was talking with Howard, who's the co-founder of endjin. I guess I've known him for many, many years, back when I was active in the .NET community, and both me and Howard were big fans of the aspect-oriented programming guys at PostSharp. And so we were talking, and he mentioned that you guys have been doing a lot of work with Azure Functions. So I thought, you know, I'd get you guys to come on the show and talk about that, because like for me and for a lot of my listeners, we are probably more heavy on the AWS side, and so I'm really curious about how Azure Functions works and how it differs from Lambda. I guess before we dive into that, do you want to tell us a bit more about yourself and the work that you guys are doing at endjin?
Consultancy and Azure use cases
Ian Griffiths: Yes, so endjin, we're a fairly small UK based consultancy specializing mostly in data analytics. So we build on top of things like Azure, Fabric and Azure Synapse. And so the broad thrust of what we do is trying to enable people to exploit the power that's inherent in their data. So that's fundamentally what we do, one way or another, and that obviously works through application of various technologies, including but not limited to Azure Functions.
Yan Cui: Yeah, and I remember I saw a couple of talks that you have done at the various conferences around optimizing cold starts for .NET on Azure Functions. But I guess, now, before we talk about the cold start, are there any sort of, I guess, interesting use cases that you've done with Azure Functions with endjin?
Ian Griffiths: Yeah, so we've made quite a lot of use of Azure Functions over the years. So one of the standout ones for us was for a customer a few years ago now that was doing monitoring of shipping internationally, in order to discover things like illegal trading patterns or human trafficking or things like that. And it was all about analyzing large volumes of data that came in, and they were spending a huge amount of money on things in their data center to process that. And we helped them move over to a cloud-oriented approach for processing that, with a kind of balance between frequency of processing and efficiency of usage, and we used Azure Functions as part of this.
So there was essentially a recurring job that ran and did batch processing to ingest and process data for them, to ensure that they were able to do the analytics they wanted in a timely way, but in a way that wasn't, you know, hugely expensive to run. So I mean, it's not anything different from what you might do with AWS Lambda. The basic idea is that you need to run jobs from time to time, which are typically triggered by internal requirements. So it might be timer driven things, it might be messages arriving on a queue, it might be conditions being met somehow that trigger things to happen.
It might be just communication within services that are not publicly exposed to the outside world, but where you just need a compute node to run some code on to get some work done, usually for a fairly short amount of time. It might be a few seconds, it might be a few minutes, it might be a few milliseconds, but it's probably not going to be hours or days, in all likelihood. So it's those sorts of jobs. So yes, processing of that kind of data.
Origins and evolution
We've also... actually, an earlier thing I worked on, which actually ran on the predecessor of Azure Functions, which might be a useful thing to get into just to understand what Azure Functions really is and where it came from, was a system for spinning up virtual classrooms. So I worked on a project where people could essentially run labs where people needed access to machines configured in a certain way, so they could do hands-on labs as part of a training experience. And we automated the provisioning and scaling up of large numbers of virtual machines, so students actually had machines they could go and do the labs on without having to, you know, pre-install a whole load of stuff on their own laptops.
And I used to do loads of training back in the day, and the way we used to do it was someone would turn up at the weekend and install software on a room full of PCs ready for the Monday morning training to start. So this was automating that kind of work, and again, you need something that can sit and orchestrate a long running process, and that can recover from failures and just push through and make sure everything happens. And we were building that actually on the predecessor to Azure Functions, but basically that developed into what became Azure Functions today. So yeah, orchestrations and recurring work and sort of internal processes, it's all those sorts of things.
Azure versus AWS workflows
Yan Cui: Okay. I do remember reading about something similar to... on AWS you have AWS Step Functions, which is kind of the service that lets you build and model your business workflows as a state machine. On the Azure side, I do remember there was something like, was it Logic Apps? Was that Logic Apps?
Ian Griffiths: Yeah, that's it, yes. So I would say they are a slightly different part of the Microsoft house. So Microsoft has always had what we sometimes refer to as clicky clicky draggy droppy type experiences, because a large part of Microsoft's history is really empowering people to use computing in a way that used to require an IT department.
Yan Cui: Right, yeah.
Ian Griffiths: It's like a lot of the things Microsoft have done were trying to put power in the hands of individuals. And they didn't necessarily invent the things that they did. Obviously spreadsheets were already around before Microsoft brought them in, but Excel now kind of, you know, powers half of the financial world, and I think they're trying to do the same thing with Microsoft Fabric as well. And I think that, for me, the Logic Apps fit into that low code, no code type approach, where you're trying to enable someone who knows what they want to do and can describe the process to be able to get that set up and put in place without necessarily having to be able to drive a compiler.
And I think these days probably Microsoft would drive people more in the direction of Power Apps and that kind of world, rather than Logic Apps. I see Logic Apps as being the kind of logical predecessor to that. But they were kind of connected to the Azure Functions world, so you're right, there's a connection there, but I see that as sort of... it's more morphed off into a separate thing from the App Service world that it began in.
Infrastructure and isolation
Yan Cui: Right, gotcha. So I guess in this case, I have not played around with Logic Apps in the past. I did try out Azure Functions briefly, and I know a few friends who've done some projects using Azure Functions, and one of the things that kind of really stood out for me compared to AWS Lambda is the fact that it's actually using Docker under the hood. And you do, I think it even exposes the underlying container instance, so that you can potentially, you know, connect to it yourself directly. I guess, maybe, you know, can you talk us through how does Azure Functions work, and maybe how does it differ from Lambda?
Ian Griffiths: So one important thing to know is there's actually two different ways of using it. So yes, you can use it in an explicitly containerized mode, but that's actually, as far as I know, that's only available on Linux. So if you build Linux containers, you can host them inside the world of an Azure Function. But that's actually... most of what we've done has not been that way. We actually tend to, for historical reasons, have mostly built them on the Windows platform.
So there's a Windows version and there's a Linux version. They are the same API, because you're writing .NET code in both cases, and it's all .NET Core. It's just, are you compiling for Windows or Linux? Same source code could in principle run in either if you're avoiding any particular system calls. In the Windows one, it is containerized, but you don't get to see that. You're using the kind of internal containerization system that Azure itself is built on top of, and they do not surface that to you in the Windows world.
And they've actually changed what they do under the covers a few times. None of the details of that are public, but the specifics of the containerization system that it's built on have not been the same throughout the entire history of Azure Functions. Whereas if you do use the Linux containerized version, then obviously it's a bit more stable, because that is officially part of the public API, and yes, you can talk to it.
So with the Windows version, you can also kind of connect to it. You can get a command prompt into it, you can even attach a debugger to it and debug stuff up in Azure if you want to through Microsoft development tools. That'll work. But that's all basically done over sockets, so you don't really know quite what you're talking to. And if you poke around on the machine from inside of a process running in an Azure Function, it's sort of clear it's not quite a normal machine. It's some very specialized environment.
The badge rendering function and slow font metrics
Actually, let me give you an example of this. One of the examples I give in my cold start talk is another thing endjin did with functions, which was badge rendering. So you know the little badges you get saying when things were last built, or what the published version in this package repository is? You see at the top of readmes in a lot of GitHub repos that will say, you know, this is the latest version, and have a link to, you know, NuGet or to PyPI or whatever package repository you're using. And people tend to use... there are public sites like shields.io that will render these things for you.
But we needed some of our own. We were doing some badges that actually represented IP maturity. So we're measuring things like how recently has there been a review, does this conform to certain coding standards that are set up by the company, various things of that kind, for which there were no standard badges available. So we wrote our own Azure Function just to render these things as SVG. And we discovered that we had horrible startup performance when we first wrote this. It was like taking 20 seconds to get a response, and we couldn't work out why for a while, because you run it locally and it's all like a fraction of a second, because it's just not doing anything terribly interesting.
And the bit that we thought would be the slowest was all running, you know, in under a millisecond, so that was fine. The thing that was actually taking the time was reading font metrics, because we wanted to measure how wide the string was to work out how big to make the SVG for the badge. And it turned out the thing that was taking forever was loading the font description off disk, because you're running on a headless server that never uses fonts. And so as the first person ever to ask for the font list, it turns out that just takes forever.
Because on Windows, it does that very, very early on in the boot process, so the font is already cached by default. But on a headless server, it's like, oh, that's not a code path they've really optimized for. So that was a fun discovery about running up in that world, is it's like, oh, it is Windows, kind of, but not really. It has some quite different performance characteristics under certain circumstances. Easily solved once you know the problem, but not that easy to work out that that was the problem. It was a non-obvious thing.
Cold starts and pricing
Yan Cui: Yeah, so speaking of cold starts, are cold starts as big an issue for Azure Functions customers as it is for Lambda? On Lambda, there's a lot of people building, you know, user facing APIs with, you know, various different API Gateway or some different options. But ultimately, when you've got a user that's hitting your app and it's talking to an API, cold starts even for just more than a second is potentially a problem, especially when you've got, you know, API talking to API, and they all call the function behind the scenes, so the cold starts stack up. Is that something that you see people doing, in terms of like building entire applications out of Azure Functions, and, you know, having the problem with cold starts?
Ian Griffiths: It's a thing you might need to work at. It really depends, though, on your usage model, because there are different pricing plans with Azure, and the performance you get depends on how much you're prepared to pay. So if your usage model means that you're going to be more or less permanently busy, then it makes sense to pay for one of the plans where you've essentially prepaid for a certain amount of compute that's always going to be available. And in those cases, generally speaking, even a cold start will be... I forget the exact numbers, but it tends to be in the sort of low milliseconds numbers, because they pre-warm instances up for you. So that the amount of time it takes between a request coming in and your code running, they work hard to minimize it, because they can dedicate some compute capacity for that, because you're paying for it.
Right, so whereas there is also a consumption based pricing model available, where if you don't use the thing for three days, you pay absolutely nothing. But obviously in that case, they're not going to keep computers sitting waiting for a request to come in on your behalf, and so in that world there is a significantly higher delay. Now, you can still usually get it down to, you know, maybe a couple hundred milliseconds, which is not too bad for a thing that you've not paid for for weeks. But if you don't design for that and don't test as you're developing, it's actually very easy to get yourself into a situation where it's much worse than that.
And so, I mean, the example I gave of our little badge rendering thing, we did something that we didn't realize was going to have horrible performance and gave us like 20 second start times, which is obviously just completely unusable at that point. But that's not normal, that was an unusual case. So in practice, there are a few things that you need to do if you want to get the best out of the cold start performance, and it's not that different from AWS Lambda, to be honest.
Ahead-of-time compilation
So one of the changes that Lambda made in their .NET SDK was they took advantage of the ahead of time compilation features that got added to .NET a couple of years ago. Where rather than doing all of the JIT compilation, all the translation from IL into machine language at runtime, which has been the default in .NET forever, you can now say, actually, I'm going to emit pre-compiled machine language for the architecture I know I'm going to be running on, in addition to the IL. And so the JIT compiler doesn't have to do anything at startup. The thing can just run immediately, and that often makes, you know, sort of 100 millisecond type difference to startup times, possibly more depending on what you're doing.
So for example, if you do dependency injection, it's very easy to write code that touches every single component in your system a teeny bit on startup as part of its initialization code, whether it needs it or not. And that can actually, you know, if you've got sort of 40 dependencies, you can suddenly be looking at start times of over a second very easily. Whereas the pre-compilation technologies actually do a very good job of flattening that back out to being, you know, acceptably fast, even for a full cold start.
But like I said, that tends to be the biggest issue with the cheapest plans, the ones where you pay nothing if you're not using it, because you really do have to start totally from scratch with those. Microsoft has done a lot of work on the higher price points over the last few years to significantly improve the cold start, in the case where you're prepared to pay a recurring fee every day to keep the lights on. So in those situations, they do actually pre-warm things. They load your code ahead of it being needed on your nodes. They don't bring those into service until it's ready.
Because one of the features, for better or worse, of Azure Functions is that they assume you're okay with your code running for a long time. The basic assumption is you're probably going to start a process, leave it running maybe for hours, possibly days, between reboots. And so you're going to amortize most of those startup costs. You're going to average the amount to be very, very low, because the overwhelming majority of your requests are not going to be cold starts. But the consumption based pricing plans often change that. They often mean, well actually, most of your requests may well be cold starts, because presumably the whole reason you went for that model is that you expect to spend large amounts of time idle, enough that everything spins right down again. So it does depend on the usage model.
Yan Cui: So that's interesting, because Lambda has also done very similar things to what you described, in terms of having this two-tier pricing structure. On the Lambda side, they call it provisioned concurrency, so essentially you pay for a number of instances of your function to be, you know, warm all the time, so that you don't have to think about the cold starts. But I think on the Azure side, looking at the pricing page, they're not charging you based on a number of instances, but rather based on virtual CPU hours, and also by memory as well. So I guess, how does that translate to number of actual instances, you know, I guess warm instances of your function? Say, you know, if you're paying for a certain amount of vCPU, if it's not like a whole unit, like one vCPU, do you just get like a fraction of the time on that instance?
Ian Griffiths: It's not that clearly defined. They've deliberately left it a little bit on the fuzzy side. And really, I could be wrong, but I have the impression that it's set up in such a way that if you're using it enough that the charges are high enough that you need to know the details, you're probably better off going onto one of the higher price points anyway. Because once you move on to the premium pricing plans, where there's always at least one instance switched on, it's a little bit easier to work out. It does actually then translate more directly into "you have this many CPUs there". But it is a thing that we found difficult over the years, is getting an accurate... well, getting a complete pricing model for customers.
I don't think we've ever had it be wildly wrong, but it's often taken quite a lot of work to understand the workload the customer's going to present, in order to build the right pricing model in advance, because it's not always that obvious what it is going to do.
Scalability and concurrency
So for example, we were surprised by the extent to which Azure Functions is happy to scale out quite early on. So if you give it a sudden larger quantity of work, we were sort of expecting it to dump it all on one node to begin with and then gradually spread out, but it actually seems to go into quite an aggressive mode of going, "Oh, you're busy, right, I'm going to spread you across loads and loads of nodes." But because you're not paying for the actual nodes themselves, you're already paying for how long your code is running for any particular request, it doesn't actually make a difference to what you are charged. But you need to know that that's happening in order to work out how to optimize the code, because again, startup costs start to become significant there.
Because if you're running once on a thousand nodes, that looks very different to running 100 times on 10 nodes, right? So you need to prototype and measure, we have found, to make sure that the characteristics you're seeing are the ones you expect. So we spend a lot of time building sort of fake models of the real system and just timing them and measuring them, and seeing what the bills actually look like when they come in compared to what we thought they were going to, to make sure that we've properly understood how things are going to work.
Yan Cui: Okay, so in that case, am I right in thinking that it's the same as Lambda, in that one instance of your function, once it's been provisioned and initialized, it handles one request at a time, so there's no concurrency within your function instance?
Ian Griffiths: Ah, no, you will get concurrency inside your function instance.
Yan Cui: Okay.
Ian Griffiths: And once again, it depends on which pricing model you've gone for. One of the pricing models is actually to do with where Azure came from. So not Azure, where Azure Functions came from. So Azure Functions is a part of App Service, which is a set of technologies that Logic Apps was part of. They also had a thing called Mobile Services, which might still exist, but I don't ever hear of people using it anymore. But there's also Web Apps as part of that. So if you deploy a web app, that's essentially the same infrastructure as an Azure Function.
From WebJobs to Azure Functions
And this comes from the roots of Azure Functions, is that the previous way to do the sorts of things you do in Azure Functions was to create what was called a WebJob. So you would provision a web app, which you would allocate a certain number of compute nodes to, and you could also say, "Oh, by the way, I'd like to use those same compute nodes to run periodic jobs, or to run things when things come in off a queue." So you essentially piggybacked it onto an existing web app. And for a while it was just... it was called a web app even if you were just using it for pure compute, and then over time they separated that out into this distinct Azure Functions offering.
But you can still use it in that old mode. You can provision what they call an App Service, with either an App Service Environment, or you can have an App Service Plan, which essentially is where you're paying for some number of VMs at some capacity. And then you can have several different web apps and several different functions all assigned to those compute nodes, and in that case they won't scale up or down unless you tell them to. And in that case, they will definitely be handling many concurrent requests. So certainly the model for Azure Functions is absolutely capable of having very high numbers of requests in flight simultaneously, if you let it. So there definitely isn't any requirement or presumption that it's one thing at a time.
Concurrency risks and management
Yan Cui: Okay, that's interesting. And I guess there's pros and cons to that, because that's one of the things that the Lambda team has resisted up until now. And quite a few people have kind of commented in the past that they should introduce some kind of in-process multi-concurrency model, so that you are able to reduce the number of cold starts, so that you can, I guess, also leverage some of the downtime you have in your code, in your functions. So that when it's making a call to some API and you're just waiting for a response, you can handle another request in the meantime, and also reduce some of the, I guess, the amount of concurrency that you have to deal with at the sort of Lambda service level. But they haven't introduced that yet, and instead they've been focusing on reducing cold starts, you know, instead.
I guess one of the downsides of having this concurrency inside your function is that if one request blows up, it's going to impact multiple requests. All the concurrent requests being processed at the same time, you know, they're all going to blow up at the same time as well, similar to how you have that kind of problem when you're dealing with a server. Is that something that you guys have found as well? I guess in that case, what model do you normally prefer to work with?
Ian Griffiths: So one observation I'd make is that, because in the world of .NET we've always tended to have this very concurrent approach. So if you go back even to the early days of the original ASP.NET back in 2001, even then the assumption was that you are going to have a single web server that handles all the requests, or maybe a small number of web servers that handle all the requests, and it is all highly concurrent within a single process. And so it's always been that way in the .NET world. It's always been the assumption that you have long running processes that do lots of stuff concurrently. Multi-threading has always been part of the picture, so it's not really a new thing for Azure Functions. That's more just... it's carried on in that tradition, if you like.
And part of that has been that there has been a tendency to try and keep errors fairly contained. So if you have something that blows up inside a web request handler of some kind, whether it's a, you know, web UI or just a web service, normally it doesn't take everything else out with it, generally speaking. The way it works is that the pipeline of handling that processes the request just handles that and reports a 500 error, and you just continue. So unless you decide that it's the kind of problem that indicates that the whole process is in trouble, and you decide to do a fail fast, then the default behavior is it just carries on.
And that in turn is part and parcel of the idea that you probably don't care which machine you are running on. You know, if you don't know that you're on a particular machine, you've got a farm of servers and any single one of them could handle the request, then necessarily the request handling is going to be pretty self-contained. It shouldn't really be relying too much on process-wide state, because if you do that in a web app, you've kind of made a mistake, because that's not how web architecture really works. So you have to be able to work within the scope of a single request, and therefore failures tend not to have a particularly large blast radius.
Now, some people probably look at that and go, "That sounds horribly dangerous, because if something has blown up, what if it's actually destroyed some state somewhere else? You know, what if it's made my cache invalid?" And, you know, that is a reasonable point of view. But then you could also say, well, what if you've destroyed something in the database as a result of a failure going wrong as well? The answer is you have to use programming practices where such updates are atomic, and so either they succeed or they don't. So it requires some developer discipline, but I think that's true no matter where your state is shared, whether it's within the process or whether it's at a kind of more system scope.
But the reality is, our experience is it's not a big problem. You know, we don't tend to see... it's very, very unusual to see a problem where failures in one request have caused issues for some other part of the system that was running at the same time. So in practice, it seems to work okay.
Memory management
Yan Cui: Right, yeah. I mean, I've done a lot of multi-threaded programming before in .NET, and, you know, to get it to a certain level, especially to get it to a certain super high scale, you know, you do have to do certain things, which can be quite dangerous. You end up with deadlocks and other things like that. And also one problem that can, I guess, blow up the entire process is, you know, to do with out of memory exceptions. Those are not something that you can really quite catch in your application per se. But I guess the Azure process may have some, you know, safeguards in place, so that when it throws up, it doesn't just take down a whole server, but the next request is going to reset itself. And I guess that you just have a slightly bigger blast radius than you would do if you just had one request at a time. It's not going to be, you know, all the application requests, but it's just going to be a subset of that.
And I'm guessing the memory stuff, is that something that you have to set ahead of time when you configure the function, to say how much memory is allocated, like you do with Lambda? Or is it something that you just pay based on how much memory is actually consumed in processing a request?
Ian Griffiths: So in the pre-priced plans, you are essentially paying for a certain amount of capacity. So the oldest approach, where you have an App Service Plan, as it's called, the number of CPU cores and the memory capacity are just part of the specification. So it's like, you pay this much, you get this, you pay that much, you get that, and that's very straightforward. With the consumption based model, if I remember rightly, it's more that there's just a limit. They give you a certain amount of upper boundary, and if you go beyond that, then they'll just terminate the process.
That's not quite true. The garbage collector will try and stop you doing that, but if you genuinely try and use more memory than you have, then yes, it's going to fail with an out of memory exception. We were running quite close to the wind on that in the application I described with the shipping stuff, because we were doing fairly complex computations to try and look at all the data at once in certain aspects, to try and find correlations of patterns across the entire ocean. But that's the only time I can remember where the limit became a major factor.
But yeah, on the high volume things, you tend to be going for one of the pricing plans where the capacity is explicit. But yeah, for the consumption stuff, I'd have to go and look it up to be certain. But yes, in practice, once you get beyond a certain spending level, it almost always makes sense to move off the initial consumption based plans onto the premium plans anyway, which are, you know, elastic, but the capacities are more clear, and the memory capacities are essentially prescribed.
Idle behavior and eviction
Yan Cui: Right, gotcha. And I guess, do you also have this... because you mentioned one other thing that was quite interesting as well in terms of difference. You mentioned that with Azure Functions there's some built-in assumption that you're going to have a long running process, as opposed to with Lambda, you kind of just assume that it's just one invocation at a time. You know, you can run for up to 15 minutes, and then they actually have quite, you know, a lot of processing behind the scenes so that they garbage collect your idle instances, so that, I guess, they can reuse the resource somewhere else, for other functions or for other customers.
And I guess one of the benefits of that process, at least from my experience, is that you don't really have any sort of, you know, memory related problems like memory leaks or memory fragmentation and things like that. So on the Azure side of things, I guess there's no like background garbage collection that... I think on the Lambda side, if your instance is idle for like four to five minutes, there's a good chance that it's going to get garbage collected, and even if it's busy 24/7, it's going to get garbage collected after like eight hours. What's the behavior on the Azure side?
Ian Griffiths: So on Azure, excuse me, if you actually go idle on Azure, you will tend to be evicted in practice. Well, certainly on the consumption based plans. I think even on the elastic premium ones, I think they do actually bring new nodes online and then take old ones offline more frequently. But it's not documented exactly how long it will be, but empirically, when I was doing the cold start talks, there were some clear thresholds where things were obviously getting evicted.
So there's a very simple one of just how long it takes for the connection to get shut down. So obviously, if you've got an HTTP connection already open, you avoid the handshake for subsequent requests there. So within a certain time window, you can just see that the average timing is better, because it hasn't had to reopen the connection, and then I think over about a minute and a half, that thing goes away. But then there's another threshold, 20 minutes, where you can see that the response time is significantly better if your requests are less than 20 minutes apart. And then once you go over that, suddenly they get significantly longer if you're on the consumption plan, because you've got fully evicted.
So there is something similar going on, but the difference, I think, is that if you are permanently busy... so if you have a request coming in... well, not even busy, permanently active, without ever going idle for a long time. So if you get requests every five seconds, let's say, which obviously is not a busy website, but is a website that never sleeps, then you will remain in memory for significant lengths of time. And by inspection, it seems that it's not unusual to hang around for hours at a time.
I haven't attempted recently to work out if there's like an upper limit, if there's a time after which they will definitely boot you off one machine and move you on to another one. I would expect there probably is, but I haven't attempted to discover what that is, because it would require a long experiment, and I'm not quite sure how valuable the answer to that question would actually be to me. So I don't know for certain, but certainly it's normal to hang around for hours at a time. That definitely happens.
Language runtimes and models
Yan Cui: Right, gotcha. And what about the language support? I think I've tried the JavaScript before. There's also, obviously, I imagine TypeScript as well, and the .NET. What about the other languages, and are there significant performance differences in terms of cold start performance for different language runtimes?
Ian Griffiths: Yes, so I forget the full list of language support, but there's basically two different modes, one of which is it can effectively just run a command, and as long as that's able to accept incoming requests, it can then stick a front door in front of that that's able to route requests through to whatever your back end might be. So that's a slight oversimplification, but essentially that's how most of the runtimes work. So if you're running Node, or even Python, or something like that, effectively you start up a process that's hosted in the Azure Functions compute environment. They then have a gateway that provides the public facing piece, and that will forward requests to your back end.
So it doesn't really matter what you're writing it in, as long as you can deploy something. In principle, anything could theoretically work. And in practice, they only install certain runtimes for you out of the box. So there's official support for .NET, there's official support for Python and Node.js, and so there's a few that they provide out of the box support for. But because you can deploy a container in the Linux version, then I think in theory you could do all sorts of things in there. I've heard of people even like running just command line scripts as part of it. I think that's a thing you can do. I've certainly never worked on a system that's used that, but I believe it is supported.
The in-process and isolated models for .NET
So one thing to be aware of is that up until... actually, we're still there today actually, but it's going to go away. So up until .NET 9 ships, there's been two different ways the .NET code runs. So the default way for ages was for .NET code to be hosted specially, because the Azure host process would actually load your code into its .NET world. It would essentially load it into its own process, so you were sharing a process with bits of the Azure Functions runtime. Whereas almost all the other things worked with an out of process model, where the Azure runtime would talk to you through a socket, and that was how you communicated.
And they've now supported both models in .NET. They have what's called the in-process model and the isolated model. And the reason they had the in-process model was that enabled them to do certain things that were harder to do other ways. So for example, the warming up of your code in advance is easier for them to do if they've got some of their code in your process. And so that was initially done by saying, well, you are in our process, that is the model, that's how it's done, and so we're just going to load your libraries, and then we'll call into them when we're good and ready. So you were very much some guest components in somebody else's server process.
They have now introduced... I say now, it was introduced a few years ago, but it's now become the preferred way of doing things rather than the other way of doing things. They have this isolated model, where the Azure Functions environment connects to you via a thing... I think they use gRPC internally to connect to you.
And they describe the HTTP request that's come in, but you're not actually talking HTTP, you're actually just responding to incoming messages that tell you what the request was, and you describe the response to come back out. And then that gets sent back over gRPC to the gateway, which then turns it back into a real response for you. So that's going to be the preferred model going forward.
They actually wanted to make that the only model for .NET 8, that shipped in November of last year, but they had a last minute change of heart, because no one was ready yet. They've been telling people, "We're going to take this away from you, you're not going to be able to do the in-process model anymore," and people went, "La la la, can't hear you." And so Microsoft really had no choice but to carry on supporting that for another version. But they're saying they really are going to get rid of it for the next long-term support version of .NET, which will ship not this year but next year. So it's changing.
But yeah, that old in-process model was for a while kind of tied up with some of the performance characteristics of how things worked. Part of the reason it's taken them a long time to migrate to this out of process model is because they've been trying to get performance parity. And that actually would have improved the story for other languages as well, because for quite a long time C# had a bit of an advantage, because you got to be closer to the rest of the runtime. Whereas now that everyone's doing this out of process model, I think we're going to see better parity across the different languages.
So yeah, I don't actually know the stats on how widely used languages other than C# are in Azure Functions, because it's always been sort of a .NET first, if you look at how it's been implemented. I mean, they very clearly support other languages, and they're very keen to have people run anything on it. But from what I've seen in terms of how people use it, I think when people are doing things like Node.js on Azure, they're more likely to be running that in a container and using one of the containerization technologies rather than Azure Functions itself. But that could be skewed by the experience that I happen to have had. I don't have any kind of global stats on that.
Multi-tenancy and isolation
Yan Cui: Sure. I guess that's interesting, the whole hosted process, and I guess I can understand why they would do that for performance, but would that also create potential security concerns? Especially, I guess, let me... the question is, with Lambda, they've got this Firecracker on a given EC2 instance that runs your Lambda functions. There's no multi-tenancy, so your function is never going to be on the same host as some other customer's functions, but you can have multiple functions all running on the same underlying hardware, or, you know, virtual machine. I guess, do you see the same thing on the Azure side of things, so that it becomes safer for you to do this hosted process?
Ian Griffiths: So you're never going to be sharing a process with another customer, and actually you're never going to be sharing a container with another customer either, as far as I know. My understanding is that when you are running your code in Azure Functions, or any Azure App Service, there is container level isolation. Obviously you'll be sharing hardware, because we know how these data centers are built. You know, there's racks with, you know, huge numbers of CPUs in a single piece of hardware, and unless you're renting, you know, 256 CPUs, you're probably sharing hardware with someone.
But to the extent that cloud computing platforms isolate at all, you are isolated. So really, the only concern for the difference in isolation levels between the in-process and out of process model with Azure Functions would just be a case of, you can take down bits of the infrastructure that have been specifically allocated to you. But what's not happening is, you're not running inside of their gateway, you're not running inside of the front end piece. You are still in a separate container that's just for your provisioned instance. And yes, you're in the same process as the bit of the Azure Functions runtime that is on that side of the wall, but you're still very much boxed into a container.
Firecracker and WASM
Yan Cui: Right, gotcha. And talking about isolation, one of the things that the Lambda, the AWS teams have done in terms of providing a stronger form of isolation is that they created their own Firecracker microVM technology. And talking to Howard, that's something that you guys are actually experimenting with. I know you mentioned before we started on the recording that you are not actively working on that, but is there anything you can tell me about where you guys are looking at Firecracker for? You know, what's your use case?
Ian Griffiths: So we have a few use cases in mind. So multi-tenancy is one that we keep seeing a lot. So we have customers who want to build multi-tenanted systems, and there are always strong legal requirements around isolation of data. People often underestimate those. So we would like to see the Microsoft world move to stronger cross tenant isolation. We think there is a tendency for it to be too easy to have a single deployed instance of a service that ends up with the keys to too many different kingdoms in it.
It's not inevitable. There's nothing fundamental about how Azure is built that forces you to do this, but it's quite easy to build yourself into that, you know, to paint yourself into that corner by accident. And to be fair, Microsoft has been recently publishing some multi-tenancy guidance that helps make it easier for that. But we think that there is scope to kind of erect better boundaries between bits of the system.
And we think that the interesting thing for us about something like Firecracker is it enables you to get a container like boundary between places where it might not have been affordable to do that before. Because one of the problems we see is customers have large numbers of very small tenants, right? You might have hundreds of people who are paying practically nothing, and a small number of very big tenants. And you're happy to dedicate, you know, entire instances of your service to people who are paying you millions a year, but if you've got, you know, a long tail, you can't just give everyone their own compute very easily. So we're very interested in how else can you enforce boundaries, and so that's one reason lightweight VMs are interesting to us.
And also, another reason we're interested, not just in Firecracker, but also, oddly enough, we're looking at things like WASI as well, so WebAssembly System Interface stuff. We're interested in how can you minimize the amount that needs to be present before you can start running code. So obviously containers are a good deal more lightweight than virtual machines, which are a good deal more lightweight than ordering a new server from Dell and driving it to the data center. You know, it's better than it used to be 30 years ago. But containers are still kind of heavyweight compared to what they could be. And so more lightweight things, such as Firecracker, but also such as a WebAssembly driven model for separation and virtualization, both of those are interesting to us in terms of just achieving better density. You know, how can you get more properly separated worlds onto a certain quantity of hardware without, you know, flattening the whole thing?
Yan Cui: Yeah, that's very interesting. I think you're probably the first people that I... well, maybe one of the few people that I know that are actually experimenting with Firecracker directly, because it's a very low-level technology. Most people just kind of use it through using Lambda or AWS Fargate.
Ian Griffiths: Well, I think that's the thing we tend to do at endjin. I mean, I used to write device drivers for a living. That's how I got started in my career, as I wrote kernel mode device drivers for networking cards, and then wrote embedded software that operated inside of broadcast systems. And so I've always tended to be quite close to the metal, originally in my career. And so I tend to think that there's often value in looking at how things work at that lower level, because if you see things how they really are, you can see opportunities for composing things in other ways than the obvious ways, that can actually enable better efficiencies, or, you know, better security within the constraints that you're operating under. So yeah, that's why we do these experiments. We think that it's useful to think, well, how could things be different than they are? Because if you just accept the defaults, then you're going to be doing the same as everyone else.
Black box diagnostics
Yan Cui: So in your experience, what have been the biggest challenges with using Azure Functions?
Ian Griffiths: The biggest challenge, I would say, with using Azure Functions has been the fact that it's a black box. You don't know how great chunks of it work, and actually a lot of it has changed over time. And again, going back to these talks I've been doing about how to improve cold start, one of the interesting features of that is I rerun all the tests each time I deliver the talk, and often things have changed. Even though I'm deploying literally the same compiled output that I was deploying three years ago, you know, the same files pushed up there, and the behavior is just different, because they've changed something, usually with a view to improving something.
So a lot of the changes in recent years have been specifically geared towards better cold start, in fact. So things just got better all by themselves without me changing anything. But the downside of that is that when things go wrong, it can be very, very hard to know where to look. Because it's, by design, platform as a service, you know, you're not given a VM, so you can't do all the things you might normally do to diagnose problems, because you just can't run arbitrary commands on the thing. You can't just install whatever on there and take a look at it. You don't have access to significant parts of the system.
When authentication became the bottleneck
So for example, one system we built was using the security features built into Azure Functions. So there are means of doing service to service authentication, so you can basically have Azure itself impose a requirement that only certain identities can call into certain functions. And that's quite convenient, but it's also completely unclear how it works. And we eventually discovered that the scalability of that feature was less than the overall scalability of Azure Functions as a whole. So you could create situations where authentication was your bottleneck, and it wasn't at all obvious that that was the root cause.
It was only after a series of kind of A/B changes that we revealed, oh okay, if we just disable authentication, this whole problem goes away completely. Obviously you can't do that, but that was the thing. And what we were seeing was just inter service requests were gradually getting longer and longer and longer, and we worked out that we were sitting in some sort of queue inside of some component of Azure Functions that we had no control over, that was basically saying, "Oh, you need to be authenticated. Right, I'll get to you in a minute, because there's a huge long big list of other ones I've got to work on as well."
And the amount of compute that they had provisioned for things like just signature validation, you could overload it if you were doing requests too quickly. And it took us way longer to work that out than it should have done, because you just couldn't actually see any of the moving parts. It's like you're doing X-ray crystallography, you know, you're sort of throwing things into a black box and trying to see what pattern emerges from the other side. It's hard to actually understand what it is you're really seeing. So yeah, that is probably the hardest thing.
Why the platform stays opaque
Although I would say developing better relationships with Microsoft certainly helps with that, because if you know who to ask, then you can short circuit a lot of that work. So it's got better from that perspective, although again, it depends. When people move from one team to another, sometimes we lose our contacts, and then it gets harder again. But yes, I think it would be much easier if we could see more about how it's implemented, but I know why they don't do it, because they change things, right?
And they've talked about this. They've changed completely how the front end pieces work. So back in the day, I think a lot of it was based on some modified version of IIS, you know, their old Windows web server was part of it. You can still see vestiges of that from some of the logging that's in there. But they've now moved over a lot of these features to YARP, the new reverse proxy .NET based thing, because they can do more things more efficiently with that. And they couldn't really do that if they had made the guts public, because then you could take dependencies on how it works internally, and then they can't change how it works, and obviously that's a problem. So I see why they do it, but it is a downside of any platform as a service proposition.
Yan Cui: Yeah, that actually makes sense. You know, we talked about earlier how the hosted process does already have an impact in terms of what they can do on the .NET platform itself and how the hosting process can work, and they had to push back a whole version of .NET to introduce some changes that they really wanted to do. I can imagine, if, you know, they make more of it public, then people are going to just be depending on those implementation details that they really shouldn't. But you know, once they know it, of course they will.
Ian Griffiths: Yeah, I mean, I think they made it pretty clear you're not supposed to do that, and I think they have put barriers in place that mean that you realize you've shot yourself in the foot early if you do that. But it tends to be more just things don't work. If you try and take advantage of some internal detail, that would just not work. The lack of transparency is much more of a problem for diagnostics, because when things go wrong, you just need to know what's actually happening, and if you don't know even the shape of the system you're looking at, that just makes it quite a lot harder to do.
Reactive Extensions and closing
Yan Cui: Yeah. Thank you so much for taking the time to talk to us today, and for sharing your experience on Azure Functions and how it works under the hood. Before we go, is there anything else that you'd like to share? Anything that endjin is doing, are you guys hiring, anything that you want to share with the audience?
Ian Griffiths: So the thing I most want to share is something I spent a lot of the last year on. At the beginning of last year, so just over a year ago, we took over the maintenance of a project called the Reactive Extensions for .NET, or Rx.NET.
Yan Cui: I love that joke, by the way.
Ian Griffiths: Yeah, I love it as well. I've loved it since the day they first launched it. And it was an amazing invention from Microsoft, and then it took bits of the non-Microsoft world by storm. You know, so it's hugely popular in the Java, in bits of the JavaScript world. And then it sort of lost its way a bit in the .NET world. It became this open source project, and then it never really had a great deal of official support, and various people kind of had to move on in other directions, and it sort of ground to a halt.
So we actually volunteered to take it over, and so we've been bringing it up to date, and we're trying to fix a bunch of issues that have affected certain use cases. We've also written a free... I've written a free book, which is an update of a book by Lee, I forget his surname, that's embarrassing, I should have prepared this before I started the talk. But there's a book called Intro to Rx. My co-author's name is on that, and it's a free book available for all that completely describes Rx in great detail, being done hand in hand with our kind of resurrection of the support for that excellent project, which I think deserves to live on, and we're helping it to do that.
Yan Cui: Perfect. Yeah, I really appreciate that, because I was a big fan of Erik Meijer and his work in .NET, well, back when I was in the .NET world. And Rx, Rx.NET came out, it was a big thing for me. I spent a lot of time playing around with that, and I've been using the Reactive Extensions for JavaScript as well. So if you send me the link to the book, I'll make sure that it gets into the description as well, so anyone who's reading can go and learn more about this Reactive Extensions for .NET, which is an amazing library, something that's super, super cool to see being revived in the .NET world. So yeah, thank you so much again. And if people want to find you, how can they find you on the internet?
Ian Griffiths: So if you go to endjin.com, which is the homepage of the company I work for, there is a Who We Are section, and you can find everyone who works there, including me, listed there. You'll find my blogs there, videos I've written and so on. Or if you go to introtorx.com, that's the book. So I will send you the link as well, but introtorx.com, that will take you to the Rx bits, and you can sort of find me through there as well.
Yan Cui: Excellent, I've got those.
Ian Griffiths: Thank you very much for having me on.
Yan Cui: Yeah, thank you so much. Enjoy the rest of your day. Goodbye, everyone.
Ian Griffiths: Okay, bye.
Yan Cui: Bye bye, bye for...