danilop / danilop/serverless-chat

Idempotency Concern of the ChatFunction

Open
#8 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
106
Forks
25
PR merge metrics
No merged PRs in 30d

Description

Description:

I would like to kindly bring attention to a potential issue in the [ChatFunction](https://github.com/danilop/serverless-chat/blob/5c283d20d69598ccda3a36fa180ae4808b0e7d30/fn/index.js#L73), which is invoked by the [chatRule](https://github.com/danilop/serverless-chat/blob/5c283d20d69598ccda3a36fa180ae4808b0e7d30/cloudformation/template.yaml#L54) IoT TopicRule. This function calls the [IotData.publish](https://github.com/danilop/serverless-chat/blob/5c283d20d69598ccda3a36fa180ae4808b0e7d30/fn/index.js#L232) to send chat messages to the frontend, where the frontend [appends](https://github.com/danilop/serverless-chat/blob/5c283d20d69598ccda3a36fa180ae4808b0e7d30/fn/index.js#L180) the new messages with previous ones. However, this message sending is not idempotent. Suppose the IoT-triggered function crashes after successfully sending a chat message to the frontend-facing IoT topic (but before acknowledging the event source that the function has finished). In that case, when the function is retried, another message will be sent and received by the frontend.

Suggested Fix:

Although there's no serious harm in sending two identical chat messages, this duplicate sending due to untimely retries can be prevented. Specifically, one can attach a unique id to each chat message when the message is being generated. Before the frontend appends the new message to the message list, it should check whether the message id is already present in a deduplication set for holding all previous chat message ids. If the id is already present, don't append the message. Otherwise, append the message as usual.

Thank you for considering this feedback. I hope that this suggestion could help improve the idempotency and user experience of the ChatFunction. Please feel free to reach out if you have any questions or concerns.

Contributor guide

No contributing guide indexed for this repository

Research direction

Read the ChatFunction in fn/index.js, especially the message generation, frontend append logic, and IotData.publish call, then inspect chatRule in cloudformation/template.yaml. Trace the retry scenario described in the issue. Done means repeated delivery does not append the same chat message twice while normal messages still appear once.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript
Domain
backend, cloud, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.