首页 > 解决方案 > How to use Event-Driven architecture to remove "api-based lambda calling another lambda" anti-pattern?

问题描述

Suppose, I have an api POST /order which invokes PlaceOrder lambda and expects response from this. PlaceOrder lambda does some works, invokes another lambda ProcessPayment lambda and expects response. Also, ProcessPayment invokes CreateInvoice lambda expecting response. Whole architecture is like a RequestResponse cycle. I woulde like to achieve that without lambda invoking another lambda as it is considered as anti-pattern. My question is what is the best design pattern to achieve this behavior within 29 seconds with event-driven architecture.

What AWS suggests: As per this official documentation, they suggests to use SQS. But regarding using SQS, I have some thoughts.

My thoughts:

  1. At event sources architecture, I can orchestrate these lambdas with SQS, SNS etc other event sources, but in that case, the nature would not be synchronous and thus I would not get response from the api.

My other solution:

  1. Using Step Function: I can orchestrate this workflow with step function, and I think it is more elegant solution in this synchronous calling case. But I would like to achieve this via event sources.

How can I design this scenerio with best practices using event-based achitecture?

Chaining

标签: aws-lambdamicroservicesaws-api-gatewayamazon-sqsamazon-sns

解决方案


事件驱动架构中,生产者和消费者之间的通信在设计上是异步的,这就是架构扩展的方式。

您可以在 EDA 中的 2 个服务之间获得几乎同步的通信,通过创建专用队列/通道在它们之间进行通信,确保它们被放大到延迟可接受的水平(接近同步值)。

这增加了一些复杂性,因为需要响应的服务必须在热循环中等待以尽快获取它们,并且如果消息丢失,您需要有重试策略等。


推荐阅读