- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.8k
Description
When batching, we merge multiple requests into one, so we need a solution to merge the corresponding contexts as well.
Jul 22, 2025: see updated proposals in #13320 (comment).
API proposal:
Different use cases require different strategies for merging these context fields in the resulting batched request. To address this need, the proposed API allows for individual control over each field:
Supported policies
* keepfirst: use the context value from the first request in the batch
* keeplast: use the context value from the last request in the batch
* keepall: always convert the field into an array and concat all value
* keepmax: choose the greatest. This is only supported for numeric types. Behavior is undefined if not numeric type
* keepmin: choose the least value. This is only supported for numeric types. Behavior is undefined if not numeric type
If no policy is specified for a field, the keepfirst strategy is used by default.
Example. Let assume that the incoming request comes in with a context carry a timestamp
req1 = [item1, item2, item3] ctx1 = {timestamp:1751501111}
req2 = [item4, item5, item6] ctx2 = {timestamp:1751502222}
req3 = [item7, item8, item9] ctx3 = {timestamp:1751503333}
Merge policy 1: Keep first
Config:
batch:
    context:
        timestamp: keepfirst
Merged requests:
mergedRequest1 = [item1, item2, item3, item4, item5] mergedCtx1 = {timestamp:1751501111}
mergedRequest2 = [item6, item7, item8, item9] mergedCtx2 = {timestamp:1751502222}
Merge policy 2: Keep last
Config:
batch:
    context:
        timestamp: keeplast
Merged requests:
mergedRequest1 = [item1, item2, item3, item4, item5] mergedCtx1 = {timestamp:1751502222}
mergedRequest2 = [item6, item7, item8, item9] mergedCtx2 = {timestamp:1751503333}
Merge policy 3: Keep all
Config:
batch:
    context:
        timestamp: keepall
Merged requests:
mergedRequest1 = [item1, item2, item3, item4, item5] mergedCtx1 = {timestamp:[1751501111, 1751502222]}
mergedRequest2 = [item6, item7, item8, item9] mergedCtx2 = {timestamp:[1751502222, 1751503333]}
Merge policy 4: Keep max / min
Config:
batch:
    context:
        timestamp: keepmax
Merged requests:
mergedRequest1 = [item1, item2, item3, item4, item5] mergedCtx1 = {timestamp:1751502222}
mergedRequest2 = [item6, item7, item8, item9] mergedCtx2 = {timestamp:1751503333}
Error handling: not-number fields will be dropped
Out of scope: preserving context across persistent storage is not yet supported. If persistent queue is used with batching, context will be lost.