cancel
Showing results for 
Search instead for 
Did you mean: 
Subscribe

I am making use of SAP Cloud Functions. My use case is very straightforward - User calls the function, function connects to MongoDB Atlas with the connection string ( not using SAP CP Mongo service as it is not supported yet), performs CRUD operations and return results.

I have defined my cloud function as follows - index.js -

  const mongoose = require('mongoose');
  constAddress= require('./models/Address')
  module.exports =asyncfunction(event, context)
  {
   const mongoConfig = context.getSecretValueJSON('mongodb','config.json');
   try{
   await mongoose.connect(mongoConfig.config.uri,
   { useNewUrlParser:true,useCreateIndex:true}); 
   console.log("Connected to database") 
   let address =new Address({"city":"Berlin"}) 
   await address.save() 
   console.log("Successfully saved address info") 
   return address; 
   }catch(error){ 
   console.log("Error - "+ error) 
   return error }
  };
  

The corresponding schema and model is defined in a different file - Address.js

const mongoose = require("mongoose");

const addressSchema = new mongoose.Schema({
    city: {
        type: String,
        required: true,
        trim: true
    }
}, { strict: false });

const Address = mongoose.model('Address', addressSchema);

module.exports = Address

Now the issue is, every time the function is called, mongoose opens up a new connection to the database, which to me seems like a very inefficient way of doing things. What I would like to know is, how to organize the code so that the connection takes place only once (preferably during the initial deployment of the function).

What I have tried is - defined the connection details in a different file called connection.js -

const mongoose = require('mongoose');

mongoose.connect('mongodb:..........',{
    useNewUrlParser:true,
    useCreateIndex:true});

But now the issue is, I have to hardcode the connection details in the file - connection.js. In the first code snippet you would see that I made use of a property called -

mongoConfig.config.uri

This property is available only inside the function through the variable context which is passed to the cloud function. This variable picks the connection details from the environment variable that we define during deployment.

I want to make use of environment variable and not hard code any sensitive information. At the same time make sure, the connection - mongoose.connect() is called only once and not every time the function is invoked.

So, I have 2 options -

1. Access the context variable outside the main function (say right after the require statements) and then call the mongoose.connect (outside the main function), thereby re-using the mongoose connection for all the incoming function calls. OR

2. Continue with the current implementation and place the mongoose.connect inside the main function. I'll be able to access the environment variables using the context variable now but the mongoose connection will be created for every new function call.

Thanks

0 Likes
View Entire Topic
mariusobert
Developer Advocate
Developer Advocate

Hi Boudhayan,

very good question! I don't have a definite answer to this, I would say this is more opinion.

The business model behind FaaS is that you only pay when your function is executed. This mean, your function might not be "active" at all during certain time intervals. The provider will only "start" your function when you try to access it. E.g. keeping a connection open across multiple requests is imo not possible as the function could be terminated.

If you need to call this function so often, that the overhead of creating the connections is too expensive. It might be worth considering if you need FaaS in the first place.

boudhayan-dev
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Marius Obert

FaaS is definitely suited for our use-case. The delay due to the connection is not very much but I would still like to keep it at minimum. I understand that FaaS uses kubernetes container for deployment. So when the function is re-started after a long period of time, is the same container used ? If it is the same container, I can do a conditional connect.

mariusobert
Developer Advocate
Developer Advocate
0 Likes

Hi boudhayan_dev,

ok. I'm not sure how FaaS is run in the background as afaik there are no options to go to a "deeper level" and have less abstraction. But that'd be interesting to know.