Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.
This repository was archived by the owner on Feb 7, 2026. It is now read-only.

reuse connections on table.insert #41

@alexander-fenster

Description

@alexander-fenster

TL;DR: the current implementation of BigQuery establishes a new connections on each table.insert() call. This causes problems for Cloud Functions because of connection quota that our users hit. Please update the client libraries to reuse connections.

Why new connections cause problems:

Cloud Functions is a serverless platform for executing snippets of code. Many our customers use function to store events in Big Query. GCF has a quota on connections, and this quota is relatively low because each connection requires a NAT port to be allocated and maintained until TCP packet timeout; and there are only 32k ports per server.

Why BigQuery is worse than other client libraries:

I found three types of clients:

  1. Clients that always reuse connections - even if we construct the client object in a local scope - @google-cloud/storage
  2. Clients that reuse connectoins only if we declare the client object at global scope - @google-cloud/pubsub, @google-cloud/language, @google-cloud/spanner
  3. Clients that never reuse connectoins - @google-cloud/bigquery

It would be nice if all libraries worked as 1.

How to reproduce the problem:

Every call to the function exported by the code below creates a new connection.

******************* function.js ****************

const bigquery = require('@google-cloud/bigquery')();
const table = bigquery.dataset('Tests').table('RandomNumbers');

exports.function = function(req, res) {
  var r = Math.floor(Math.random() * 100);
  console.log("Generated random number " + r);
  var row = {number: r};
  table.insert(row, function(err, apiResponse) {
    if (err) {
      result = 'Error inserting data to bigquery';
      console.log(result + ": " + JSON.stringify(err, null, 2));
      res.status(500).send("Error" + err);
    } else {
      res.status(200).send("" + r);
    }
  });
};

************* package.json ****************

{
  "dependencies": {
    "@google-cloud/bigquery": "^0.10.0"
  }
}

We also found that BigQuery uses IPv4, while other libraries prefer IPv6. This should also be fixed.

[Googlers: internal tracking id b/68240537]

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/nodejs-bigquery API.perftype: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions