吞吐量优化型写入

本页面介绍如何配置最长提交(写入)延迟时间,以优化 Spanner 中的写入吞吐量。

概览

为确保数据一致性,Spanner 会向数据库中的所有投票副本发送写入请求。此复制过程可能会产生计算开销。如需了解详情,请参阅复制

吞吐量优化型写入提供了通过同时执行一组写入来分摊这些计算费用的选项。为此,Spanner 引入了短暂的延迟,并收集需要发送给同一投票参与者的一组写入操作。以这种方式执行写入可以大幅提高吞吐量,但代价是延迟时间会略有增加。

默认行为

如果您未设置提交延迟时间,如果 Spanner 认为延迟可分摊您的写入费用,则可能会为您设置一个较小的延迟。

常见使用场景

您可以根据应用需求手动设置写入请求的延迟时间。此外,您还可以通过将最长提交延迟时间设置为 0 毫秒,为对延迟时间高度敏感的应用停用提交延迟。

如果您的应用具有容忍延迟的应用并希望优化吞吐量,那么设置较长的提交延迟时间可以显著提高吞吐量,同时每次写入都会增加延迟时间。例如,如果您要批量加载大量数据,并且应用并不在意 Spanner 写入任何单独数据的速度,那么您可以将提交延迟时间设置为较长的值,例如 100 毫秒。我们建议您从 100 毫秒的值开始,然后上下调整,直到延迟时间和吞吐量权衡满足您的需求。在大多数应用中,20 毫秒到 100 毫秒之间的值效果最佳。

如果您有一个对延迟时间敏感的应用,默认情况下,Spanner 的应用也对延迟时间敏感。如果您的工作负载激增,Spanner 可能会设置短暂的延迟。您可以尝试将值设为 0 毫秒,以确定以增加吞吐量为代价缩短延迟时间的功能是否适合您的应用。

设置混合提交延迟时间

您可以为写入的子集配置不同的最大提交延迟时间。如果这样做,Spanner 会使用为这组写入配置的最短延迟时间。但是,我们建议为大多数用例选择单个值,因为这样会导致行为的可预测性更高。

限制

您可以将提交延迟时间设置为 0 到 500 毫秒。如果设置超过 500 毫秒的提交延迟时间,会导致错误。

为提交请求设置提交延迟时间上限

最大提交延迟参数是 CommitRequest 方法的一部分。您可以使用 RPC APIREST API 或 Cloud Spanner 客户端库访问此方法。

Go


import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/spanner"
)

// maxCommitDelay sets the maximum commit delay for a transaction.
func maxCommitDelay(w io.Writer, db string) error {
	// db = `projects/<project>/instances/<instance-id>/database/<database-id>`
	ctx := context.Background()
	client, err := spanner.NewClient(ctx, db)
	if err != nil {
		return fmt.Errorf("maxCommitDelay.NewClient: %w", err)
	}
	defer client.Close()

	// Set the maximum commit delay to 100ms.
	// This is the amount of latency this request is willing to incur in order
	// to improve throughput. If this field is not set, Spanner assumes requests
	// are relatively latency sensitive and automatically determines an
	// appropriate delay time. You can specify a batching delay value between 0 and 500 ms.
	// The transaction will also return the commit statistics.
	commitDelay := 100 * time.Millisecond
	resp, err := client.ReadWriteTransactionWithOptions(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
		stmt := spanner.Statement{
			SQL: `INSERT Singers (SingerId, FirstName, LastName)
					VALUES (111, 'Virginia', 'Watson')`,
		}
		rowCount, err := txn.Update(ctx, stmt)
		if err != nil {
			return err
		}
		fmt.Fprintf(w, "%d record(s) inserted.\n", rowCount)
		return nil
	}, spanner.TransactionOptions{CommitOptions: spanner.CommitOptions{MaxCommitDelay: &commitDelay, ReturnCommitStats: true}})
	if err != nil {
		return fmt.Errorf("maxCommitDelay.ReadWriteTransactionWithOptions: %w", err)
	}
	fmt.Fprintf(w, "%d mutations in transaction\n", resp.CommitStats.MutationCount)
	return nil
}

Node.js

// Imports the Google Cloud client library.
const {Spanner, protos} = require('@google-cloud/spanner');

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client.
const spanner = new Spanner({
  projectId: projectId,
});

async function spannerSetMaxCommitDelay() {
  // Gets a reference to a Cloud Spanner instance and database.
  const instance = spanner.instance(instanceId);
  const database = instance.database(databaseId);

  database.runTransaction(async (err, transaction) => {
    if (err) {
      console.error(err);
      return;
    }
    try {
      const [rowCount] = await transaction.runUpdate({
        sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (111, @firstName, @lastName)',
        params: {
          firstName: 'Virginia',
          lastName: 'Watson',
        },
      });

      console.log(
        `Successfully inserted ${rowCount} record into the Singers table.`
      );

      await transaction.commit({
        // The maximum amount of time to delay the transaction to improve
        // throughput.
        maxCommitDelay: protos.google.protobuf.Duration({
          seconds: 0, // 0 seconds
          nanos: 100000000, // 100,000,000 nanoseconds = 100 milliseconds
        }),
      });
    } catch (err) {
      console.error('ERROR:', err);
    } finally {
      // Close the database when finished.
      database.close();
    }
  });
}
spannerSetMaxCommitDelay();

Python

# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def insert_singers(transaction):
    row_ct = transaction.execute_update(
        "INSERT Singers (SingerId, FirstName, LastName) "
        " VALUES (111, 'Grace', 'Bennis')"
    )

    print("{} record(s) inserted.".format(row_ct))

database.run_in_transaction(
    insert_singers, max_commit_delay=datetime.timedelta(milliseconds=100)
)

Ruby

require "google/cloud/spanner"

##
# This is a snippet for showcasing how to pass max_commit_delay in  commit_options.
#
# @param project_id  [String] The ID of the Google Cloud project.
# @param instance_id [String] The ID of the spanner instance.
# @param database_id [String] The ID of the database.
#
def spanner_set_max_commit_delay project_id:, instance_id:, database_id:
  # Instantiates a client
  spanner = Google::Cloud::Spanner.new project: project_id
  client  = spanner.client instance_id, database_id

  records = [
    { SingerId: 1, AlbumId: 1, MarketingBudget: 200_000 },
    { SingerId: 2, AlbumId: 2, MarketingBudget: 400_000 }
  ]
  # max_commit_delay is the amount of latency in millisecond, this request
  # is willing to incur in order to improve throughput.
  # The commit delay must be at least 0ms and at most 500ms.
  # Default value is nil.
  commit_options = {
    return_commit_stats: true,
    max_commit_delay: 100
  }
  resp = client.upsert "Albums", records, commit_options: commit_options
  puts "Updated data with #{resp.stats.mutation_count} mutations."
end

监控写入请求延迟时间

您可以使用 Google Cloud 控制台监控 Spanner CPU 利用率和延迟时间。如果为写入请求设置较长的延迟时间,预计 CPU 利用率可能会降低,而延迟时间会增加。如需了解 Spanner 请求的延迟,请参阅捕获并直观呈现 Spanner API 请求延迟