如何在所有 Azure 函数实例之间共享对象状态 [英] How To Share object state across all Azure Function Instances

查看:19
本文介绍了如何在所有 Azure 函数实例之间共享对象状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个动态扩展的 C# Azure Function App(消费计划),即根据负载添加新实例,现在我们有一个需要在此 Azure Function 的所有实例中使用的对象,因为该对象很复杂(有代表)我无法序列化以放入外部缓存,以便所有实例都可以从那里访问它,请建议处理这种情况的最佳方法.

We have a C# Azure Function App (consumption plan) which scales dynamically i.e new instances are added depending on the load, now we have an object which needs to be used across all instances of this Azure Function, as the object is complex (has delegates) I cannot serialize to put on an external cache so that all instances can access it from there, please suggest the best way to handle this scenario.

推荐答案

您的问题中有一些根本矛盾的地方:您不能同时拥有两者 a) 一个可扩展到多台机器的功能;b) 所有实例共享内存中的对象,就好像它们在单台机器上运行一样.反之亦然:无服务器意味着如果没有人使用您的功能,所有基础设施都可以关闭(从而为您节省资金)——但这隐含意味着您将丢失内存中的状态并且需要能够序列化它.

There's something fundamentally contradictory in your question: you can't have both a) a function that scales to many machines; b) all instances sharing in-memory objects as if they were running on a single machine. It goes the other way too: serverless means that if nobody is using your function, all infrastructure can be shut down (hence saving you money) - but that implicitly means you'd lose your in-memory state and need to be able to serialize it.

最小化共享状态,使其可序列化(存在处理委托 [1] 等事物的模式),并使用外部存储(Azure 存储或 Redis 等缓存).

Minimize the shared state, make it serializable (there are patterns for dealing with things like delegates [1]), and use external storage (Azure Storage, or a cache like Redis).

[1] 对于委托,一个技巧是为委托维护一个句柄字典,然后序列化该句柄.同样,对于多态性,您可以序列化类型名称.

[1] For delegates, one trick is to maintain a dictionary of handles to delegates, and then serialize the handle. Similarly, for polymorphism, you might serialize the type name.

这篇关于如何在所有 Azure 函数实例之间共享对象状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆