> This page location: /docs.md > /docs/development.md > /docs/current/connect-to-cluster.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Manage Connections with Google Cloud

You can interact with your Atlas clusters by using [Google Cloud Functions](https://cloud.google.com/functions/docs/concepts/overview) and [Google Cloud Run.](https://cloud.google.com/run/docs/overview/what-is-cloud-run)

## Best Practices

Use the following best practices to properly manage connections between Google Cloud functions and Atlas:

- Create your Cloud Function with a globally-scoped database connection rather than a function-scoped database connection.

  Don't define a new client object each time that you invoke your function. Doing so causes the driver to create a new database connection with each function call. This can be expensive and can result in your application exceeding database connection limits. For best performance, follow these guidelines:

  1. Create the client object once.

  2. Store the object so your function can reuse the client across function invocations.

  The [connection example](https://www.mongodb.com/docs/atlas/manage-connections-google-cloud.md#std-label-google-cloud-function-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.

  If you have a function that connects to a sharded cluster with many shards, you might experience performance issues. For example, with a ten shard Atlas cluster, the driver connects to all thirty [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances by default. You can use the `srvMaxHosts` option in your connection string to set the maximum number of hosts that the driver connects to. To improve driver performance, set `srvMaxHosts=3`. For example:

  ```text
  mongodb+srv://<db_username>:<db_password>@<clusterName>.mongodb.net/?retryWrites=true&w=majority&srvMaxHosts=3
  ```

  To learn more, see [Connection Options.](https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/connection-options/)

- Restrict network access to your Atlas cluster.

  Connect to your Atlas cluster over private networking using a [Network Peering connection](https://www.mongodb.com/docs/atlas/security-vpc-peering.md#std-label-vpc-peering) between your Atlas cluster and your Google Cloud function, or, alternatively, a [private endpoint](https://www.mongodb.com/docs/atlas/security-private-endpoint.md#std-label-private-endpoint-overview), so that you can allow only private IP addresses from your [IP access list.](https://www.mongodb.com/docs/atlas/security/ip-access-list.md#std-label-access-list)

  If you don't use private networking, consider connecting to your Atlas cluster via a [NAT gateway](https://cloud.google.com/nat/docs/overview). Otherwise, you must allow all IP addresses (0.0.0.0/0) to access your Atlas cluster.

  **Warning:**

  Adding the `/0` CIDR (Classless Inter-Domain Routing), such as `0.0.0.0/0`, allows access from anywhere. This configuration can expose your deployment to unauthorized access, data exfiltration, and other malicious activity. Restrict access to trusted IP addresses or CIDR (Classless Inter-Domain Routing) ranges whenever possible, and use strong credentials for all database users when allowing access from the public Internet.

  **Note:**

  If you add to a project access list any IPv4 `/0` CIDR, such as `0.0.0.0/0` or `10.0.0.0/0`, Atlas sends an alert email to all users directly granted a role to the project, or indirectly granted a role through team membership (if the team is granted a role to the project).

* Set [maxIdleTimeMS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.maxIdleTimeMS) to `60000` to automatically close your connections after 1 minute of idle time. Tuning your `maxIdleTimeMS` can help reduce the occurrence of timeout errors from your serverless functions.

- Configure concurrency. When you create a new Google Cloud function:

  - Select the 2nd gen environment, which can handle multiple concurrent requests. 2nd gen also reduces the connection load on the server by allowing the function to share a single `MongoClient` with many concurrent invocations.

  - Increase the concurrency setting to minimize cold starts and improve latency.

  **Note:**

  If you increase the concurrency setting, you may need to increase the CPU for best performance. To learn more, see [Concurrency.](https://cloud.google.com/functions/docs/configuring/concurrency#console)

## Google Cloud Connection Example

The following example connects a Node.js Google Cloud function to an Atlas deployment. Replace `<YOUR-ATLAS-CONNECTION-STRING>` with your Atlas connection string.

```javascript
const { MongoClient } = require('mongodb');

let client;
async function getConnection() {
    if (!client) {
      
        const client = new MongoClient('<YOUR-ATLAS-CONNECTION-STRING>');
        client.on('connectionCreated', () => {
            console.log('New connection created successfully.');
        });

        // Connect to the database in the global scope
        await client.connect();
    }

    return client;
}
```
