We are currently initiating a cloud function with an HTTPS request to:
- Provide a quick response (<10s) to the caller with a 200 Response in order to prevent additional retries from our caller.
- THEN carry out two other time-sensitive POST requests before concluding the function.
What are some optimal solutions to address this issue? We have explored:
- PubSub as a way for subscribers to complete the POST requests asynchronously after sending the 200 response.
- Utilizing Cloud Tasks and enqueueing them to a task queue. However, we are unsure if these tasks are impacted by the HTTPS termination, similar to our previous attempts.
- Implementing an Event-Based Trigger such as Cloud Firestore to write to a document with a specified ID that triggers an
onCreate
event to finalize the POST requests.
Additional information includes the fact that we currently experience low traffic due to being a smaller app (less than 1000 QPS), which may eliminate Pub Sub as an option. We aim to create the background task within <1-2 seconds to maintain a positive user experience (specifically for a messaging app).
Initially, our code was structured like this:
exports.example = functions
.https
.onRequest(async (request, response) => {
const authHeader = request.get("authorization");
const {statusCode, httpsResponseMessage} =
verifyAuthHeader(authHeader, token);
// Respond quickly to the caller within 10 seconds to avoid additional retries.
response.status(statusCode).send(
{
message: httpsResponseMessage,
}
);
// Perform other POST requests
}
We also attempted the following:
exports.example = functions
.https
.onRequest(async (request, response) => {
const authHeader = request.get("authorization");
const {statusCode, httpsResponseMessage} =
verifyAuthHeader(authHeader, token);
// Provide a 200 response to the caller within 10 seconds to prevent further retries.
response.write(
JSON.stringify(
{
message: httpsResponseMessage
}
)
);
// Carry out additional POST requests
res.end()
}
However, we encountered inconsistent behavior with our code AFTER
response.status(statusCode).send()
, as mentioned in the tips & tricks section of Firebase docs. Sending a partial result with response.write
did not prevent future retries from the caller.