Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.6.0+

Amazon DynamoDB: Batch Read Items and Retry Unprocessed Keys

See more Amazon DynamoDB Examples

Read three unique keys, correlate unordered results by their primary key, and retry only UnprocessedKeys with bounded exponential backoff and jitter. Missing items are not returned. They are reported as missing only after all unprocessed keys have completed. A repeated unprocessed key is normal and is not treated as a pagination cycle.

BatchGetItem supports at most 100 keys and 16 MB per response; partial results can occur below those limits, including a 1 MB per-partition limit. Split larger inputs into separate batches. This example uses strong reads but the entire batch is not an atomic snapshot. After eight attempts it saves the remaining request privately and reports incomplete work. Applications must also retain already returned items and any input not yet submitted.

The item examples use the ExampleItems table from Create a Table, with string partition key PK and string sort key SK. Wait for ACTIVE. DynamoDB AttributeValue JSON uses wrappers such as {"S":"text"}, {"N":"123.45"}, and {"BOOL":true}. Numeric attributes travel as strings; preserve their precision rather than converting them to int or floating point. Names and IDs remain strings.

Uses DynamoDB's regional JSON API: POST to /, application/x-amz-json-1.0, and an operation-specific X-Amz-Target: DynamoDB_20120810... header. The 20120810 API identifier is still current. Chilkat.Rest and AuthAws sign each request with AWS Signature Version 4, service dynamodb. Set the region once; the example builds the matching commercial AWS endpoint. Other AWS partitions use their own endpoint suffixes.

Read current IAM credentials from a private qa_data/aws/dynamodb.json containing string fields access_key, secret_key, and, for temporary credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. This code explicitly reads the file; it does not discover an AWS profile or refresh credentials automatically. The session token is included in the signed X-Amz-Security-Token query parameter, so keep request URLs and diagnostics private. A Socket explicitly enables TLS certificate verification and is supplied to Rest. Reuse the configured Rest object for subsequent requests.

IAM permissions for the illustrated actions: dynamodb:BatchGetItem. Limit access to the appropriate table, index, backup, and restore-target resources. Restoring customer-managed-key encrypted data can also require KMS permissions.

Checks transport completion, HTTP 200, and JSON validity separately. DynamoDB errors include __type and message. HTTP 400 can mean throttling, failed conditions, invalid input, or expired credentials. Use bounded exponential backoff with jitter for retryable failures; renew expired credentials. A failed condition requires an application decision, not an automatic retry. For writes with an uncertain outcome, read/reconcile before retrying. Only the explicitly described batch retries are automatic; other writes are not automatically replayed.

BatchGetItem reference

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkPrng.pb"
IncludeFile "CkHashtable.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkStringTable.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = 0
    ;  Private configuration, not a file in public web content or source control.
    ;  Load current IAM credentials authorized for the DynamoDB operation.
    credentials.i = CkJsonObject::ckCreate()
    If credentials.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(credentials, 1)
    success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/dynamodb.json")
    If success <> 1
        Debug "Could not load private AWS credentials."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"access_key") = "")
        Debug "Missing or invalid access_key."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (CkJsonObject::ckStringOf(credentials,"secret_key") = "")
        Debug "Missing or invalid secret_key."
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    ;  Set this once to the region containing the table.
    region.s = "us-west-2"
    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(endpoint,"dynamodb.")
    CkStringBuilder::ckAppend(endpoint,region)
    CkStringBuilder::ckAppend(endpoint,".amazonaws.com")
    aws.i = CkAuthAws::ckCreate()
    If aws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(aws, CkJsonObject::ckStringOf(credentials,"access_key"))
    CkAuthAws::setCkSecretKey(aws, CkJsonObject::ckStringOf(credentials,"secret_key"))
    CkAuthAws::setCkRegion(aws, region)
    CkAuthAws::setCkServiceName(aws, "dynamodb")
    CkAuthAws::setCkSignatureVersion(aws, 4)
    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::setCkConnectTimeoutMs(rest, 20000)
    CkRest::setCkIdleTimeoutMs(rest, 60000)
    success = CkRest::ckSetAuthAws(rest,aws)
    If success <> 1
        Debug "Could not configure AWS signing."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Temporary IAM/STS credentials require their session token on every signed request.
    ;  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
    ;  Leave session_token absent or empty only for credentials that do not have one.
    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        If CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1
            Debug "session_token must be a string."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            ProcedureReturn
        EndIf

        If CkJsonObject::ckStringOf(credentials,"session_token") <> ""
            success = CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject::ckStringOf(credentials,"session_token"))
            If success <> 1
                Debug "Could not set the session-token parameter."
                CkJsonObject::ckDispose(credentials)
                CkStringBuilder::ckDispose(endpoint)
                CkAuthAws::ckDispose(aws)
                CkRest::ckDispose(rest)
                ProcedureReturn
            EndIf

        EndIf

    EndIf

    ;  Supply a Socket to explicitly enable TLS certificate verification.
    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    CkSocket::setCkMaxReadIdleMs(connection, 60000)
    CkSocket::setCkMaxSendIdleMs(connection, 60000)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
    If success <> 1
        Debug "TLS connection failed; inspect diagnostics privately."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkRest::ckUseConnection(rest,connection,1)
    If success <> 1
        Debug "Could not attach the TLS connection."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.0")
    If success <> 1
        Debug "Could not set the content type."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    body.i = CkJsonObject::ckCreate()
    If body.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    result.i = CkJsonObject::ckCreate()
    If result.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(result, 1)
    requestBody.i = CkStringBuilder::ckCreate()
    If requestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    responseBody.i = CkStringBuilder::ckCreate()
    If responseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    errorType.i = CkStringBuilder::ckCreate()
    If errorType.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckAddHeader(rest,"X-Amz-Target","DynamoDB_20120810.BatchGetItem")
    If success <> 1
        Debug "Could not set the DynamoDB operation."
        CkJsonObject::ckDispose(credentials)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkJsonObject::ckDispose(body)
        CkJsonObject::ckDispose(result)
        CkStringBuilder::ckDispose(requestBody)
        CkStringBuilder::ckDispose(responseBody)
        CkStringBuilder::ckDispose(errorType)
        ProcedureReturn
    EndIf

    pending.i = CkJsonObject::ckCreate()
    If pending.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    random.i = CkPrng::ckCreate()
    If random.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Every requested key must be unique. This small example uses one partition.
    requestedIds.i = CkStringTable::ckCreate()
    If requestedIds.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringTable::ckAppend(requestedIds,"item-001")
    CkStringTable::ckAppend(requestedIds,"item-002")
    CkStringTable::ckAppend(requestedIds,"item-003")
    found.i = CkHashtable::ckCreate()
    If found.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    While (i < CkStringTable::ckCount(requestedIds))
        CkJsonObject::setCkI(body, i)
        CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.Keys[i].PK.S","group-1")
        CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.Keys[i].SK.S",CkStringTable::ckStringAt(requestedIds,i))
        i = i + 1
    Wend
    CkJsonObject::ckUpdateBool(body,"RequestItems.ExampleItems.ConsistentRead",1)
    CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.ProjectionExpression","#pk, #sk, #data")
    CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.ExpressionAttributeNames.#pk","PK")
    CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.ExpressionAttributeNames.#sk","SK")
    CkJsonObject::ckUpdateString(body,"RequestItems.ExampleItems.ExpressionAttributeNames.#data","Data")
    CkJsonObject::ckUpdateString(body,"ReturnConsumedCapacity","TOTAL")
    attempt.i = 0
    delayCap.i = 100
    retry.i = 1
    While (retry = 1)
        attempt = attempt + 1
        retry = 0
        CkStringBuilder::ckClear(requestBody)
        success = CkJsonObject::ckEmitSb(body,requestBody)
        If success <> 1
            Debug "Could not encode the batch request."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            CkJsonObject::ckDispose(pending)
            CkPrng::ckDispose(random)
            CkStringTable::ckDispose(requestedIds)
            CkHashtable::ckDispose(found)
            ProcedureReturn
        EndIf

        success = CkRest::ckFullRequestSb(rest,"POST","/",requestBody,responseBody)
        If success <> 1
            Debug "Batch transport failed; partial progress or the write outcome may be unknown. Reconcile before replay."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            CkJsonObject::ckDispose(pending)
            CkPrng::ckDispose(random)
            CkStringTable::ckDispose(requestedIds)
            CkHashtable::ckDispose(found)
            ProcedureReturn
        EndIf

        success = CkJsonObject::ckLoadSb(result,responseBody)
        If success <> 1
            Debug "Expected a JSON batch response."
            CkJsonObject::ckDispose(credentials)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkJsonObject::ckDispose(body)
            CkJsonObject::ckDispose(result)
            CkStringBuilder::ckDispose(requestBody)
            CkStringBuilder::ckDispose(responseBody)
            CkStringBuilder::ckDispose(errorType)
            CkJsonObject::ckDispose(pending)
            CkPrng::ckDispose(random)
            CkStringTable::ckDispose(requestedIds)
            CkHashtable::ckDispose(found)
            ProcedureReturn
        EndIf

        If CkRest::ckResponseStatusCode(rest) = 200
            If CkJsonObject::ckJsonTypeOf(result,"Responses") <> 3
                Debug "Expected Responses object."
                CkJsonObject::ckDispose(credentials)
                CkStringBuilder::ckDispose(endpoint)
                CkAuthAws::ckDispose(aws)
                CkRest::ckDispose(rest)
                CkSocket::ckDispose(connection)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkStringBuilder::ckDispose(requestBody)
                CkStringBuilder::ckDispose(responseBody)
                CkStringBuilder::ckDispose(errorType)
                CkJsonObject::ckDispose(pending)
                CkPrng::ckDispose(random)
                CkStringTable::ckDispose(requestedIds)
                CkHashtable::ckDispose(found)
                ProcedureReturn
            EndIf

            If CkJsonObject::ckHasMember(result,"Responses.ExampleItems") = 1
                If CkJsonObject::ckJsonTypeOf(result,"Responses.ExampleItems") <> 4
                    Debug "Expected response item array."
                    CkJsonObject::ckDispose(credentials)
                    CkStringBuilder::ckDispose(endpoint)
                    CkAuthAws::ckDispose(aws)
                    CkRest::ckDispose(rest)
                    CkSocket::ckDispose(connection)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkStringBuilder::ckDispose(requestBody)
                    CkStringBuilder::ckDispose(responseBody)
                    CkStringBuilder::ckDispose(errorType)
                    CkJsonObject::ckDispose(pending)
                    CkPrng::ckDispose(random)
                    CkStringTable::ckDispose(requestedIds)
                    CkHashtable::ckDispose(found)
                    ProcedureReturn
                EndIf

                i = 0
                While (i < CkJsonObject::ckSizeOfArray(result,"Responses.ExampleItems"))
                    CkJsonObject::setCkI(result, i)
                    If (CkJsonObject::ckJsonTypeOf(result,"Responses.ExampleItems[i].PK.S") <> 1) OR (CkJsonObject::ckStringOf(result,"Responses.ExampleItems[i].PK.S") = "")
                        Debug "Missing or invalid Responses.ExampleItems[i].PK.S."
                        CkJsonObject::ckDispose(credentials)
                        CkStringBuilder::ckDispose(endpoint)
                        CkAuthAws::ckDispose(aws)
                        CkRest::ckDispose(rest)
                        CkSocket::ckDispose(connection)
                        CkJsonObject::ckDispose(body)
                        CkJsonObject::ckDispose(result)
                        CkStringBuilder::ckDispose(requestBody)
                        CkStringBuilder::ckDispose(responseBody)
                        CkStringBuilder::ckDispose(errorType)
                        CkJsonObject::ckDispose(pending)
                        CkPrng::ckDispose(random)
                        CkStringTable::ckDispose(requestedIds)
                        CkHashtable::ckDispose(found)
                        ProcedureReturn
                    EndIf

                    If (CkJsonObject::ckJsonTypeOf(result,"Responses.ExampleItems[i].SK.S") <> 1) OR (CkJsonObject::ckStringOf(result,"Responses.ExampleItems[i].SK.S") = "")
                        Debug "Missing or invalid Responses.ExampleItems[i].SK.S."
                        CkJsonObject::ckDispose(credentials)
                        CkStringBuilder::ckDispose(endpoint)
                        CkAuthAws::ckDispose(aws)
                        CkRest::ckDispose(rest)
                        CkSocket::ckDispose(connection)
                        CkJsonObject::ckDispose(body)
                        CkJsonObject::ckDispose(result)
                        CkStringBuilder::ckDispose(requestBody)
                        CkStringBuilder::ckDispose(responseBody)
                        CkStringBuilder::ckDispose(errorType)
                        CkJsonObject::ckDispose(pending)
                        CkPrng::ckDispose(random)
                        CkStringTable::ckDispose(requestedIds)
                        CkHashtable::ckDispose(found)
                        ProcedureReturn
                    EndIf

                    If CkJsonObject::ckStringOf(result,"Responses.ExampleItems[i].PK.S") <> "group-1"
                        Debug "Unexpected partition key."
                        CkJsonObject::ckDispose(credentials)
                        CkStringBuilder::ckDispose(endpoint)
                        CkAuthAws::ckDispose(aws)
                        CkRest::ckDispose(rest)
                        CkSocket::ckDispose(connection)
                        CkJsonObject::ckDispose(body)
                        CkJsonObject::ckDispose(result)
                        CkStringBuilder::ckDispose(requestBody)
                        CkStringBuilder::ckDispose(responseBody)
                        CkStringBuilder::ckDispose(errorType)
                        CkJsonObject::ckDispose(pending)
                        CkPrng::ckDispose(random)
                        CkStringTable::ckDispose(requestedIds)
                        CkHashtable::ckDispose(found)
                        ProcedureReturn
                    EndIf

                    returnedId.s = CkJsonObject::ckStringOf(result,"Responses.ExampleItems[i].SK.S")
                    CkHashtable::ckAddInt(found,returnedId,1)
                    Debug "Found SK: " + returnedId
                    If CkJsonObject::ckHasMember(result,"Responses.ExampleItems[i].Data.S") = 1
                        Debug "Data: " + CkJsonObject::ckStringOf(result,"Responses.ExampleItems[i].Data.S")
                    EndIf

                    i = i + 1
                Wend
            EndIf

            If CkJsonObject::ckHasMember(result,"UnprocessedKeys") = 1
                If CkJsonObject::ckJsonTypeOf(result,"UnprocessedKeys") <> 3
                    Debug "Expected UnprocessedKeys object."
                    CkJsonObject::ckDispose(credentials)
                    CkStringBuilder::ckDispose(endpoint)
                    CkAuthAws::ckDispose(aws)
                    CkRest::ckDispose(rest)
                    CkSocket::ckDispose(connection)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkStringBuilder::ckDispose(requestBody)
                    CkStringBuilder::ckDispose(responseBody)
                    CkStringBuilder::ckDispose(errorType)
                    CkJsonObject::ckDispose(pending)
                    CkPrng::ckDispose(random)
                    CkStringTable::ckDispose(requestedIds)
                    CkHashtable::ckDispose(found)
                    ProcedureReturn
                EndIf

                success = CkJsonObject::ckObjectOf2(result,"UnprocessedKeys",pending)
                If success <> 1
                    Debug "Could not read unprocessed entries."
                    CkJsonObject::ckDispose(credentials)
                    CkStringBuilder::ckDispose(endpoint)
                    CkAuthAws::ckDispose(aws)
                    CkRest::ckDispose(rest)
                    CkSocket::ckDispose(connection)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkStringBuilder::ckDispose(requestBody)
                    CkStringBuilder::ckDispose(responseBody)
                    CkStringBuilder::ckDispose(errorType)
                    CkJsonObject::ckDispose(pending)
                    CkPrng::ckDispose(random)
                    CkStringTable::ckDispose(requestedIds)
                    CkHashtable::ckDispose(found)
                    ProcedureReturn
                EndIf

                If CkJsonObject::ckSize(pending) > 0
                    ;  Retry exactly the unprocessed map; do not resend already completed entries.
                    CkJsonObject::ckClear(body)
                    success = CkJsonObject::ckAppendObjectCopy(body,"RequestItems",pending)
                    If success <> 1
                        Debug "Could not prepare remaining requests."
                        CkJsonObject::ckDispose(credentials)
                        CkStringBuilder::ckDispose(endpoint)
                        CkAuthAws::ckDispose(aws)
                        CkRest::ckDispose(rest)
                        CkSocket::ckDispose(connection)
                        CkJsonObject::ckDispose(body)
                        CkJsonObject::ckDispose(result)
                        CkStringBuilder::ckDispose(requestBody)
                        CkStringBuilder::ckDispose(responseBody)
                        CkStringBuilder::ckDispose(errorType)
                        CkJsonObject::ckDispose(pending)
                        CkPrng::ckDispose(random)
                        CkStringTable::ckDispose(requestedIds)
                        CkHashtable::ckDispose(found)
                        ProcedureReturn
                    EndIf

                    CkJsonObject::ckUpdateString(body,"ReturnConsumedCapacity","TOTAL")
                    retry = 1
                EndIf

            EndIf

        Else
            ;  These documented batch throttling failures mean the batch could not be processed.
            ;  Do not apply this rule to timeouts, HTTP 500, or arbitrary errors.
            CkStringBuilder::ckClear(errorType)
            If CkJsonObject::ckJsonTypeOf(result,"__type") = 1
                CkStringBuilder::ckAppend(errorType,CkJsonObject::ckStringOf(result,"__type"))
            EndIf

            If (CkRest::ckResponseStatusCode(rest) = 400) AND ((CkStringBuilder::ckEndsWith(errorType,"ProvisionedThroughputExceededException",1) = 1) OR (CkStringBuilder::ckEndsWith(errorType,"ThrottlingException",1) = 1))
                retry = 1
            Else
                Debug "Batch failed. HTTP: " + Str(CkRest::ckResponseStatusCode(rest)) + "; type: " + CkStringBuilder::ckGetAsString(errorType)
                CkJsonObject::ckDispose(credentials)
                CkStringBuilder::ckDispose(endpoint)
                CkAuthAws::ckDispose(aws)
                CkRest::ckDispose(rest)
                CkSocket::ckDispose(connection)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkStringBuilder::ckDispose(requestBody)
                CkStringBuilder::ckDispose(responseBody)
                CkStringBuilder::ckDispose(errorType)
                CkJsonObject::ckDispose(pending)
                CkPrng::ckDispose(random)
                CkStringTable::ckDispose(requestedIds)
                CkHashtable::ckDispose(found)
                ProcedureReturn
            EndIf

        EndIf

        If retry = 1
            If attempt = 8
                success = CkJsonObject::ckWriteFile(body,"dynamodb-batch-get-pending.json")
                If success <> 1
                    Debug "Could not save pending work; retain the current request privately."
                    CkJsonObject::ckDispose(credentials)
                    CkStringBuilder::ckDispose(endpoint)
                    CkAuthAws::ckDispose(aws)
                    CkRest::ckDispose(rest)
                    CkSocket::ckDispose(connection)
                    CkJsonObject::ckDispose(body)
                    CkJsonObject::ckDispose(result)
                    CkStringBuilder::ckDispose(requestBody)
                    CkStringBuilder::ckDispose(responseBody)
                    CkStringBuilder::ckDispose(errorType)
                    CkJsonObject::ckDispose(pending)
                    CkPrng::ckDispose(random)
                    CkStringTable::ckDispose(requestedIds)
                    CkHashtable::ckDispose(found)
                    ProcedureReturn
                EndIf

                Debug "Retry limit reached. Incomplete work saved to dynamodb-batch-get-pending.json; later input chunks, if any, were not sent."
                CkJsonObject::ckDispose(credentials)
                CkStringBuilder::ckDispose(endpoint)
                CkAuthAws::ckDispose(aws)
                CkRest::ckDispose(rest)
                CkSocket::ckDispose(connection)
                CkJsonObject::ckDispose(body)
                CkJsonObject::ckDispose(result)
                CkStringBuilder::ckDispose(requestBody)
                CkStringBuilder::ckDispose(responseBody)
                CkStringBuilder::ckDispose(errorType)
                CkJsonObject::ckDispose(pending)
                CkPrng::ckDispose(random)
                CkStringTable::ckDispose(requestedIds)
                CkHashtable::ckDispose(found)
                ProcedureReturn
            EndIf

            ;  Full jitter, exponential growth, capped at five seconds.
            delayMs.i = CkPrng::ckRandomInt(random,0,delayCap)
            CkSocket::ckSleepMs(connection,delayMs)
            delayCap = delayCap * 2
            If delayCap > 5000
                delayCap = 5000
            EndIf

        EndIf

    Wend
    ;  Only now can omitted keys be reported as not found: all retries completed.
    i = 0
    While (i < CkStringTable::ckCount(requestedIds))
        If CkHashtable::ckContains(found,CkStringTable::ckStringAt(requestedIds,i)) = 0
            Debug "Not found SK: " + CkStringTable::ckStringAt(requestedIds,i)
        EndIf

        i = i + 1
    Wend
    Debug "Batch read complete."


    CkJsonObject::ckDispose(credentials)
    CkStringBuilder::ckDispose(endpoint)
    CkAuthAws::ckDispose(aws)
    CkRest::ckDispose(rest)
    CkSocket::ckDispose(connection)
    CkJsonObject::ckDispose(body)
    CkJsonObject::ckDispose(result)
    CkStringBuilder::ckDispose(requestBody)
    CkStringBuilder::ckDispose(responseBody)
    CkStringBuilder::ckDispose(errorType)
    CkJsonObject::ckDispose(pending)
    CkPrng::ckDispose(random)
    CkStringTable::ckDispose(requestedIds)
    CkHashtable::ckDispose(found)


    ProcedureReturn
EndProcedure