Back to EIP

PatternAggregator patiently collects a sequence of messages and combines them once all have been received.
How Ballerina helps

Ballerina provides convenient map and table data structures for temporary message storage. Ballerina provides robust support for distributed storage with the Redis package. Additionally, it offers seamless integration with various SQL and NoSQL databases for persistent data storage. Ballerina's cache package is an efficient in-memory cache solution that includes automatic cleanup mechanisms.

import ballerina/http;

final map<json[]> partialSurveys = {};

final http:Client formSubmitClient = check new ("");

service /api/v1 on new http:Listener(8080) {

    resource function post survey/[string id](@http:Header string userId, @http:Payload json formData) returns error? {
        json[]? surveyData = partialSurveys[userId];
        if surveyData == () {
            json[] newSurvey = [formData];
            partialSurveys[userId] = newSurvey;
        } else {
            if surveyData.length() == 3 {
                _ = check formSubmitClient->/survey/[id]/{userId: surveyData}, targetType = http:Response);
                _ = partialSurveys.remove(userId);