0

I have implemented a system where FrontEnd upload a photo into a blob container, then Blob fire an event on EventGrid on "BlobCreated" that trigger an Az Function which generate a thumbnail and save it into a MongoDb instance. I follow the rules Microsoft suggest to avoid cold start, using an Elastic Premium Plan and configure one minimum Instance to be always warm. enter image description here

enter image description here

I do not have lots of events per seconds, to be honest I could have 30 minutes without any events and then some minutes where users pushes images on blob. I noticed from the Application Insight logs that if too much time passes between one upload event and another, function need some seconds (3 - 4 seconds) before first log (I have a log at just the begin of my function). enter image description here

If I upload three photos consecutively, time between upload and function start is some milliseconds. enter image description here

Function is initialized as following: ThumbnailGenerator.cs

namespace ThumbnailGeneratorV2;

public class ThumbnailGenerator
{
    private readonly IReceiptDbService _dbManager;
    private readonly IMimeInspector _mimeInspector;
    private readonly int _thumbnailDimension;

    public ThumbnailGenerator(
        IReceiptDbService dbManager,
        IMimeInspector mimeInspector)
    {
        _dbManager = dbManager;
        _mimeInspector = mimeInspector;
        int.TryParse(Environment.GetEnvironmentVariable("ThumbnailDimension"), out _thumbnailDimension);
    }

    [FunctionName("ThumbnailGenerator")]
    public async Task Run(
        [EventGridTrigger] EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read, Connection = "AzureWebJobsStorage")]
        Stream myBlob,
        ILogger log)
    {
        log.LogInformation("Function Start");
        ...
    }

Startup.cs

[assembly: FunctionsStartup(typeof(ThumbnailGeneratorV2.Startup))]
namespace ThumbnailGeneratorV2;

class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddSingleton<IMimeInspector, MimeInspector>();
        builder.Services.AddSingleton<IReceiptDbService, ReceiptDbService>();
        builder.Services.AddSingleton<IDbRepository, DbRepository>();
    }
}

P.S. the Az Function is outbounded with a VNET cause database is in a private network.

Someone has some suggestions how to keep the function really alive?

Thanks.

DavideP
  • 57
  • 10

1 Answers1

0

The function is taking much time to start when there is a long gap between events.

This is due to the function app is unloaded from memory after a period of inactivity to save resources. When a new event comes then the function app needs to be loaded back into memory.

And also, you need to configure the function app to use a minimum number of instances to ensure that there is always at least one instance running.

enter image description here

Another approach

Use Azure Event Grid's retry policy to resend events that were not processed due to cold start.

enter image description here

  • Use Azure Application Insights to monitor function app's performance and identifying issues.

enter image description here

enter image description here

  • Using Azure Container Instances to run function app in a containerized environment.

For more information refer to Function Diagnostics and Function App settings.

Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • My Function has already a Premium Plan and has 1 always running instance. Please look the screens in the post. Moreover the event is not lost (even if I have configured a retry policy), function take only some seconds to wake up. – DavideP May 16 '23 at 10:07
  • Since you are experiencing a delay of a few seconds when the function is not used for a while, it is possible that the function is being unloaded from memory due to inactivity. When a function is unloaded from memory, it must be reloaded the next time it is called, which can cause a delay. – Rajesh Mopati May 31 '23 at 07:15
  • And to prevent the function from being unloaded due to inactivity, you can try using the Always On feature in Azure App Service. This feature ensures that your function is always running, even when there are no requests being processed. And Set the Always On option to On. This should help keep your function warm and ready to handle requests, even when there is no activity for a long period of time. – Rajesh Mopati May 31 '23 at 07:16