The end desired result is a more performant system with less resource utilisation. I want to connect a sql database to cloud functions and host a set pf api's.
Here are two alternatives...
Alternative one: We host each api as a separate cloud function.
const functions = require('firebase-functions');
exports.helloFunc1 = functions.https.onRequest(function (request, response){
response.send("Hello from Func1!");
});
exports.helloFunc2 = functions.https.onRequest(function (request, response){
response.send("Hello from Func2!");
});
Alternative two: We host a single cloud function with internal routing.
const functions = require('firebase-functions');
const express = require('express');
const router = new express.Router();
var app = express();
var helloFunc1 = function (request, response){
// After some DB OPS
response.send("Hello from Func1!");
};
var helloFunc2 = function (request, response){
// After some DB OPS
response.send("Hello from Func2!");
};
router.get('/helloFunc1',helloFunc1);
router.get('/helloFunc2',helloFunc2);
exports.root = functions.https.onRequest(router);
Help me understand the tradeoffs between the approaches and any advantages/disadvantages in either designs.
Also please consider the database connection pooling as a part of the analysis. This wont be a determining factor if we use firebase or datastore, but for native SQL Databases, i'm assuming a lot of connections will impact performance when this is auto scaled.
EDIT #1
This is a rudimentary example, but in reality the implications maybe a lot more. Is working with a framework which provides additional functionality like ORM's and shared models, Middleware for AUTH, general convention over configuration approach a real option in a server-less environment? Because we know that these frameworks are designed to run on a always-up server. How will ORM's & frameworks in general deal with being preempted and how will it affect performance?
PS. I'm new to Node and Express.
For the time being I'd likely recommend deploying an Express app, primarily to gain the advantage of cold start reduction. However if some of your endpoints have a very different memory/cpu profile it might make sense to separate them into their own Cloud Function (especially once it becomes easier to tweak the cpu/memory profile of functions deployed via Firebase).