Skip to content

Feature request: Distributed Lock Client #3444

Open
@codyfrisch

Description

@codyfrisch

Use case

I believe incorporating a distributed lock client into the powertools would be of value. There are times multiple events being processed need to update the same resource (requires making calls to some API to retrieve updated data, and updating a record). The triggering events may not even be related to each other, other than all triggering an update.

A distributed lock would permit one event to perform the needed work and update a record, while others wait for it to finish (or skip entirely). This saves on calls to APIs (rate limits exist), and possibly save on execution time by skipping the update being handled by another instance.

Example is an application that from time to time needs to get updated data for a customer account from a third party service (the application is a plugin for the service, the data needed is the subscription and account details for the plugin from the third-party marketplace). These updates are needed for execution. One incoming event can gain a lock while updating data for the account, other executions will wait while the update occurs and check for the lock to be released (time required for the update is well understood and <1s).

Another is setting up per-minute distributed rate limit tracking - when an event is received a dynamodb record is created to track the number of requests received in a minute. Different account tiers have different limits. When a tracking record is needed, an API call must be made to get the account tier, current minute API usage (if any) as well as start the API minute timer (so the windows are deterministic). A distributed lock would prevent many incoming events from duplicating this work (and contributing to the API rate limit being tracked!). The rate tracking record allows additional requests to receive a 429 response, causing the external platform to continue to display "in progress" rather than a false "success" if the event were queued. This also minimizes needless executions to find out that the rate limits have been reached.

Solution/User Experience

I would suggest a DynamoDB based lock, built on conditional expressions much like the idempotency tool. There is an existing Java based client in awslabs, https://github.com/awslabs/amazon-dynamodb-lock-client, and at least one defunct typescript based project, https://www.npmjs.com/package/@deliveryhero/dynamodb-lock.

(I am internally developing a DynamoDB based distributed lock, as I need it, and am open to contributing if I can get authorization, and up to speed on this project. Though an ElastiCache based solution would probably be a more efficient solution, it has higher costs at low volume.)

Alternative solutions

Alternative solutions could use ElastiCache, Valkey, Redis, etc. or there could be multiple provider choices within the module.

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussingThe issue needs to be discussed, elaborated, or refinedfeature-requestThis item refers to a feature request for an existing or new utility

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions