You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/idempotency.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -373,6 +373,40 @@ sequenceDiagram
373
373
<i>Idempotent successful request cached</i>
374
374
</center>
375
375
376
+
#### Successful request with responseHook configured
377
+
378
+
<center>
379
+
```mermaid
380
+
sequenceDiagram
381
+
participant Client
382
+
participant Lambda
383
+
participant Response hook
384
+
participant Persistence Layer
385
+
alt initial request
386
+
Client->>Lambda: Invoke (event)
387
+
Lambda->>Persistence Layer: Get or set idempotency_key=hash(payload)
388
+
activate Persistence Layer
389
+
Note over Lambda,Persistence Layer: Set record status to INPROGRESS. <br> Prevents concurrent invocations <br> with the same payload
390
+
Lambda-->>Lambda: Call your function
391
+
Lambda->>Persistence Layer: Update record with result
392
+
deactivate Persistence Layer
393
+
Persistence Layer-->>Persistence Layer: Update record
394
+
Note over Lambda,Persistence Layer: Set record status to COMPLETE. <br> New invocations with the same payload <br> now return the same result
395
+
Lambda-->>Client: Response sent to client
396
+
else retried request
397
+
Client->>Lambda: Invoke (event)
398
+
Lambda->>Persistence Layer: Get or set idempotency_key=hash(payload)
399
+
activate Persistence Layer
400
+
Persistence Layer-->>Response hook: Already exists in persistence layer.
401
+
deactivate Persistence Layer
402
+
Note over Response hook,Persistence Layer: Record status is COMPLETE and not expired
403
+
Response hook->>Lambda: Response hook invoked
404
+
Lambda-->>Client: Manipulated idempotent response sent to client
405
+
end
406
+
```
407
+
<i>Successful idempotent request with a response hook</i>
408
+
</center>
409
+
376
410
#### Expired idempotency records
377
411
378
412
<center>
@@ -544,6 +578,7 @@ Idempotent decorator can be further configured with **`IdempotencyConfig`** as s
544
578
|**useLocalCache**|`false`| Whether to locally cache idempotency results |
545
579
|**localCacheMaxItems**| 256 | Max number of items to store in local cache |
546
580
|**hashFunction**|`md5`| Function to use for calculating hashes, as provided by the [crypto](https://nodejs.org/api/crypto.html#cryptocreatehashalgorithm-options){target="_blank"} module in the standard library. |
581
+
|**responseHook**|`undefined`| Function to use for processing the stored Idempotent response. This function hook is called when an existing idempotent response is found. See [Manipulating The Idempotent Response](idempotency.md#manipulating-the-idempotent-response)|
547
582
548
583
### Handling concurrent executions with the same payload
549
584
@@ -744,6 +779,42 @@ Below an example implementation of a custom persistence layer backed by a generi
744
779
745
780
For example, the `_putRecord()` method needs to throw an error if a non-expired record already exists in the data store with a matching key.
746
781
782
+
### Manipulating the Idempotent Response
783
+
784
+
You can set up a `responseHook` in the `IdempotentConfig` class to manipulate the returned data when an operation is idempotent. The hook function will be called with the current deserialized response object and the Idempotency record.
The responseHook is called after the custom de-serialization so the payload you process will be the de-serialized version.
807
+
808
+
#### Being a good citizen
809
+
810
+
When using response hooks to manipulate returned data from idempotent operations, it's important to follow best practices to avoid introducing complexity or issues. Keep these guidelines in mind:
811
+
812
+
1.**Response hook works exclusively when operations are idempotent.** The hook will not be called when an operation is not idempotent, or when the idempotent logic fails.
813
+
814
+
2.**Catch and Handle Exceptions.** Your response hook code should catch and handle any exceptions that may arise from your logic. Unhandled exceptions will cause the Lambda function to fail unexpectedly.
815
+
816
+
3.**Keep Hook Logic Simple** Response hooks should consist of minimal and straightforward logic for manipulating response data. Avoid complex conditional branching and aim for hooks that are easy to reason about.
817
+
747
818
## Testing your code
748
819
749
820
The idempotency utility provides several routes to test your code.
0 commit comments