简体   繁体   English

AWS Lambda之间的同步

[英]Synchronization between aws lambdas

I am using several AWS lambda functions (and multiple instances of the same ones) to access objects within AWS. 我正在使用几个AWS lambda函数(以及相同的多个实例)来访问AWS中的对象。 Any information on thread safety of lambdas? 有关lambda的线程安全性的任何信息? While order of writing is not important (in my case), atomicity of read-modify-write is. 尽管写入顺序并不重要(在我的情况下),但read-modify-write的原子性却很重要。 Anyone came across proper/intended solution? 任何人都遇到适当/预期的解决方案?

I came across this because I'm trying to achieve the same thing. 我遇到了这个问题,因为我试图实现同一目标。

Problem: I have a cluster of state machines running in EC2 instances. 问题:我在EC2实例中运行着一组状态机。 Each invokes a Lambda asynchronously (InvocationType.Event). 每个都异步调用Lambda(InvocationType.Event)。 I want to run the lambda code each time but perform a downstream network call only once from within the lambda. 我想每次都运行lambda代码,但只在lambda内部执行一次下游网络调用。

Solution: Using a DynamoDB table with a relatively low TTL, I perform a conditional put of an item with unique id generated from event data that will be the same across all lambda invocations for that unique event. 解决方案:使用TTL相对较低的DynamoDB表,我对从事件数据生成的具有唯一ID的项执行条件放置,该事件数据在该唯一事件的所有lambda调用中都相同。 Then, I only perform the downstream network call if the conditional put succeeds. 然后,如果条件放置成功,则仅执行下游网络调用。

It's a distributed synchronization technique often used in distributed systems for "fail-fast" systems, just via a lambda. 这是一种分布式同步技术,仅通过lambda即可在分布式系统中用于“快速故障”系统。

A strict read-modify-write atomicity requires serialization of processing (maybe using locks etc.). 严格的读取-修改-写入原子性要求处理序列化(可能使用锁等)。 This is not possible with Lambda. Lambda无法做到这一点。

What you can do is this: Write your requests into a SQS Queue and then use a single-threaded (or properly synchronized) piece of software on a single EC2 instance to process the queue entries. 您可以执行以下操作:将请求写入SQS队列,然后在单个EC2实例上使用单线程(或正确同步)的软件来处理队列条目。

The CAP theorem ( https://en.wikipedia.org/wiki/CAP_theorem ) says that this impacts your availability, because this process on a single EC2 instance by design becomes your single point of failure. CAP定理( https://en.wikipedia.org/wiki/CAP_theorem )说,这会影响您的可用性,因为按设计,单个EC2实例上的此过程将成为您的单点故障。

You could also use a RDS DB with transactions (or even DynamoDB with conditional writes/consistent reads). 您还可以将RDS DB与事务一起使用(甚至将DynamoDB与条件写入/一致读取一起使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM