Docs Menu

Best Practices Connecting from Vercel

On this page

  • Connection Example

Vercel is a cloud platform for static frontends and serverless functions. Applications deployed to Vercel can connect to Atlas clusters using serverless functions that use the MongoDB Node.js driver or the Mongoose ODM library.

Use the following best practices to properly manage connections between Vercel and Atlas. Following these best practices will help you avoid accumulating connections on the database, preventing you from potentially exceeding connection limits.

  • Define the client to the MongoDB server outside of the handler function in its own JavaScript module.
  • Don't define a new MongoClient object each time you invoke your function. Doing so causes the MongoDB Node.js driver to create a new database connection with each function call. Instead, do the following:

    1. Create the MongoClient object once.
    2. Store the object so that your function can reuse the MongoClient across function invocations and across functions.

    The Connection Example reuses existing database connections to speed up communication with the database and keep connection counts to the database at a reasonable level with respect to application traffic.

  • Use a global variable to hold the MongoClient object so that in development mode it persists across module reloads initiated by Vercel's HMR (hot module replacement) capabilities.
Note

It is not possible to determine the IP addresses of Vercel deployments. To connect to Atlas, you must add all IP addresses (0.0.0.0/0) to the IP access list of your Atlas cluster.

In the following Node.js example, mongodb-client.js and index.js show these best practices:

mongodb-client.js
1"use strict";
2
3// Import the dependency.
4const { MongoClient } = require('mongodb');
5
6const uri = process.env.MONGODB_URI;
7const options = {
8 useUnifiedTopology: true,
9 useNewUrlParser: true,
10};
11
12let client;
13let clientPromise;
14
15if (process.env.NODE_ENV === "development") {
16
17 // In development mode, use a global variable so that the value
18 // is preserved across module reloads caused by HMR (hot module replacement).
19 if (!global._mongoClientPromise) {
20 client = new MongoClient(uri, options);
21 global._mongoClientPromise = client.connect();
22 }
23
24 clientPromise = global._mongoClientPromise;
25} else {
26
27 // In production mode, it's best to not use a global variable.
28 client = new MongoClient(uri, options);
29 clientPromise = client.connect()
30}
31
32 // Export a module-scoped MongoClient promise. By doing this in a
33 // separate module, the client can be shared across functions.
34module.exports = clientPromise;
index.js
1"use strict";
2
3// Import the dependency.
4const clientPromise = require('./mongodb-client');
5
6// Handler
7module.exports = async (req, res) => {
8
9 // Get the MongoClient by calling await on the promise.
10 // Because it is a promise, it will only resolve once.
11 const client = await clientPromise;
12
13 // Use the client to return the name of the connected database.
14 res.status(200).json({ dbName: client.db().databaseName });
15}

On this page

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.