-
Notifications
You must be signed in to change notification settings - Fork 449
Closed
Labels
feature-requestfeature requestfeature request
Description
Use case
Sometimes, I need to limit concurrency of a lambda function on the level of its payload. Concurrent invocations are fine but only if they have different payloads.
Background:
I already faced this need in two of my projects:
- The solution I came up with in my first project was very similar to what's behind Powertools' Idempotency - I created a decorator that before running my handler placed a lock item (with a ttl set) in a dynamodb table. The same decorator then released the lock after the handler finished successfully. In my case, I also wrote to the table any caught exceptions and "dynamic" parameters that I wanted to make available to subsequent invocations (with the same payload). This was before I was aware of Idempotency
- In my second project I needed the same kind of concurrency control, but without a need to preserve additional info in the underlying table. This time, I considered using Idempotency, but I couldn't as it always preserves function's output in its persistent storage layer (e.g. dynamodb, I believe here). It also does not release the cache/lock on success. I understand why - it offers idempotency, not a concurrency control. Still, it felt like I could (mis)use it for my usecase as well. Not wanting to hack it, I created another decorator ...
Solution/User Experience
Implement a decorator similar to @idempotent
that would:
- prevent concurrent executions of a given lambda handler with the same payload making use of a lock placed in a dynamodb table
- decorator would release the lock on exceptions, but also if the decorated handler finishes successfully
- to safeguard against lambda timeouts it'd also set a ttl attribute (should correspond to the lambda function timeout)
- in contrast to
@idempotent
, it would not cache result and again, it'd release the lock on success
Similarly to @idempotent
, payload should be either the entire event or its subset.
Optionally
- decorator could (perhaps optionally) write any exceptions caught to the underlying dynamodb table
- functionality to preserve parameters in the underlying dynamodb table, so that they are available to subsequent executions of the same lambda function with the same payload
Alternative solutions
Modify the @idempotent
decorator to allow for switching off caching and releasing the lock on success. However, then the decorator can hardly be called idempotent
...
Acknowledgment
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Java, TypeScript
j-vm, am29d and igorlg
Metadata
Metadata
Assignees
Labels
feature-requestfeature requestfeature request