AWS Lambda Node.js Handler 的 callback 效果

文件 AWS Lambda Function Handler in Node.js 中寫到:

The Node.js runtimes v6.10 and v8.10 support the optional callback parameter. You can use it to explicitly return information back to the caller.



When the callback is called (explicitly or implicitly), AWS Lambda continues the Lambda function invocation until the event loop is empty.

雖然呼叫 callback 塞了要回傳的資料,Lambda Function 還是會繼續執行到 event loop 清空,但呼叫端其實也沒辦法先得到結果。

AWS Lambda Context Object in Node.js 裡說明 context.callbackWaitsForEmptyEventLoop:

callbackWaitsForEmptyEventLoop – Set to false to send the response right away when the callback executes, instead of waiting for the Node.js event loop to be empty. If false, any outstanding events will continue to run during the next invocation.

也就是說如果把 callbackWaitsForEmptyEventLoop 設為 false,雖然會在 callback 被呼叫時,立刻送出 response,但是 Lambda Function 就會進入 freeze 狀態,其他還未執行的 event 會再下次被呼叫時才執行到。

結論就是,一個 Node.js Lambda Function 本身無法做到先把結果回傳給呼叫端,本身繼續處理完其他可以非同步處理,不影響回傳結果的事。有一個稍微變通方式可以做到,是用 Event 方式呼叫另一個 Lambda Function 做額外非同步處理。

Azure Functions 的 Java 啟動表現頗慘

先看圖

簡直慘不忍睹,問了微軟,他們建議是用 App Service 方案開啟 Always On

看 App Service 方案定價,似乎就等於租了一個虛擬機

感覺和 AWS Lambda 落差滿大,AWS Lambda 可是真的按使用計價,而且沒有這樣的 overhead

另外 Azure Functions 的 C# 表現,看起來也不如 AWS Lambda 穩定