Search code examples
azure-functionsazure-functions-runtime

Azure Functions - Initialization Logic


I am trying to understand Azure functions with HTTP Trigger -

  1. Where should I be putting the initialization code which I intend to run only once and not on every HTTP Call. Like, connecting to Keyvault, Loading master data from database etc.
  2. Is there a way multiple functions can share memory. I might want to use same Keyvault and database in multiple azure functions.

Solution

  • Function invocations are meant to be independent from each other, so there is no way to guarantee that some code is executed only once. If your function comes under heavy load, it might even be distributed to multiple machines and they all operate independent.

    However, what you can do is declare static variables in you function and they will often retain their value between invocations. Often, no guarantees!

    Secondly, there is no way you can share memory between invocations. To share state, you should fall back on other storage solutions like a Redis Cache for example