How can I overcome scaling issues with serverless and MongoDB?

孤人 提交于 2020-12-15 05:22:26

问题


I'm familiar with serverless and MongoDB and would like to know if there are any scalable ways to connect the two worlds. As far as I know, you can't interact with MongoDB in a RESTful way. Instead, opening a connection which is supposed to be reused.

Let's say I'm using AWS Lambda as part of a serverless API. When the lambda is cold you have to open a new connection to MongoDB and while the lambda is still hot, the connection remains open. This solution is fine, but when you have a surge of traffic on the API you will hit a connection limit imposed by MongoDB.

Is there a way to overcome this connection limit in a stateless world such as lambda?


回答1:


Lambda functions are stateless and asynchronous, and by using the database connection pool, you will be adding a state to it, and defeating the purpose.

If you want connection pooling, running your server over EC2 is best suitable option on AWS.

However, if you still want connection pooling with lambda you can create a pool normally using MongoClient.connect(uri, { poolSize: 10 }) and keep your lambda warm using cloud-watch events by creating a dummy trigger.

You will definitely see some performance improvement.




回答2:


The idea to think of: You can use Kinesis to trigger you Lambda, It will limit the number of concurrent executions to the shards count, for example, 8 shards for 8 concurrently.

In this way, you can keep your connection open, and earn a limit of your open connection to avoid scaling issues.

This type of thing (For a single invocation, whether the connection is new or re-used) you can monitor by some monitoring tools that will help you to keep you scaling status over time, its will help to manage MongoDB and Kinesis iterator age. Such as:

  • AWS X-Ray
  • AWS Cloudwatch metrics
  • Datadog
  • Lumigo

As a disclosure I work for a company called Lumigo, We have a distributed monitoring tool that can help you to track your production scaling issues



来源:https://stackoverflow.com/questions/65061136/how-can-i-overcome-scaling-issues-with-serverless-and-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!