Making Azure Functions Work for Large Responses
For various reasons, some of us Azure Functions users out there need to return larger HTTP responses. In my case, I needed to create an intelligent proxy that returns some large and small files based on SRSBSNS rules. If you have tried this like me, you may have also noticed some odd qualities in your responses. For smaller content, you may have experienced long delays before the first bytes reached a client, and then slow transfer speeds after that. For larger content you may have noticed your seemingly successful HTTP 200 responses were empty or contained some UUID value instead of your actual content. If you were lucky, unlike me, you would have also noticed the OutOfMemoryException before another business pointed the problem out to you. Oh shucks, lets dig into this!
Reproduction
Thankfully this issue can be reproduced locally, lets try that out:
|
|
After invoking this function locally, you can watch your browser sit there for a few minutes as it waits for a response. Eventually you may get a 5xx response and see this in your func.exe
console window:
An unhandled host error has occurred. System.Private.CoreLib: Exception of type 'System.OutOfMemoryException' was thrown.
Worse yet because we set the HTTP response status code to a success status code before proxying the data, when run in Azure you may get an HTTP 200 response that contains some UUID value or that will be blank. Hopefully you get a 502 response instead. This can be a pretty perplexing failure when encountered as it sure looks like a success sometimes.
[Information] Executed 'FancyProxy' (Succeeded, Id=<some-other-uuid>)
And just in case you were wondering, this also impacts proxies defined in a proxies.json file:
|
|
Solution
It seems like we are waiting for some buffer to fill up before it returns our first bytes. That OutOfMemoryException we get also indicates that we have reached our memory limit, probably due to buffering. Looking at what I think is the source for this behavior, a workaround looks possible:
- WebJobsApplicationBuilderExtension.cs
builder.UseMiddleware<ResponseBufferingMiddleware>();
- ResponseBufferingMiddleware.cs
var bufferStream = new BufferingWriteStream(originalResponseBody);
- BufferingWriteStream.cs:WriteAsync
if (_isBuffering)
- BufferingWriteStream.cs:FlushAsync
_isBuffering = false;
If we can provide a courtesy flush before we release our content into the response stream, we can deactivate the buffering that is causing our bowl to overflow.
|
|
Conclusion
The most important result of this workaround is that we can now actually return larger responses from Azure Functions without experiencing any confusing failures. If you aren’t running into failures, there is also a cost consideration here. After some shoddy testing and quick calculations this workaround seems to also reduce the total function execution time by nearly 75% in some cases. This can be the difference between a free month of execution and a whopping $10 USD. Well, I’m sure that cost adds up for somebody. More seriously, if you attempted to solve this problem using more memory or a dedicated app service plan it could cost a great deal more and yet still possibly fail. Bypassing the buffering is vital for these kinds of responses and could save you some money 💹. I don’t understand what problem this buffering solves, but from my point of view it is just a source of trouble.