扳手中的下一个数字
Next Number in Spanner
在 Spanner 中实现下一个数字例程的最佳方法是什么?
背景:
我们需要生成用于分配的序号。多个并发用户可以请求下一个号码。我们要确保没有用户收到相同的号码。
当前的设计是 table 包含最后使用的数字。该列将被读取,增加一个数量然后写出。事务中的读取是否会锁定正在读取的行直到事务完成?
在 Spanner 中实现下一个数字例程的最佳方法是什么?
就像您在任何数据库中一样,确保读写事务是隔离的、原子的。
事务中的读取是否锁定正在读取的行直到事务完成?
Locking read-write. This type of transaction is the only transaction type that supports writing data into Cloud Spanner.
Properties
A read-write transaction in Cloud Spanner executes a set of reads and writes atomically at a single logical point in time.
Atomicity, Consistency, Durability In addition to the Isolation
property, Cloud Spanner provides Atomicity (if any of the writes in
the transaction commit, they all commit), Consistency (the database
remains in a consistent state after the transaction) and Durability
(committed data stays committed.)
直接引用自their documentation.
Performance >> Locking
Cloud Spanner allows multiple clients to simultaneously interact with the same database. In order to ensure the
consistency of multiple concurrent transactions, Cloud Spanner uses a
combination of shared locks and exclusive locks to control access to
the data. When you perform a read as part of a transaction, Cloud
Spanner acquires shared read locks, which allows other reads to still
access the data until your transaction is ready to commit. When your
transaction is committing and writes are being applied, the
transaction attempts to upgrade to a exclusive lock. It blocks new
shared read locks on the data, waits for existing shared read locks to
clear, then places a exclusive lock for exclusive access to the data.
所以...我认为,如果您像本文档末尾的读写示例一样这样做,您就可以完全冷静下来。
在 Cloud Spanner 中处理序列时要非常小心,因为这可能是设计主键的 anti-pattern that can lead to hotspots (i.e. performance problems). Are you really sure you absolutely need sequential numbers? If you just need to assign people unique identifiers, there are ways to do this without requiring them to be sequential. For example, see this answer 。
在您的问题的应用程序描述中,您说多个并发用户可能正在请求下一个号码。这个值 (next_number
) 在整个应用程序中是否是全局唯一的?如果是这样,那么这个值将成为一个热点并且可能会限制您的数据库(和应用程序)的可扩展性,因为这意味着您的数据库的性能将受到单台机器处理单行事务的速度的限制。您能否以某种方式为数据库中的不同users/entities设置不同的next_number
?例如,每个用户都可以有一个 next_number
值吗?或者您可以 "shard" 您的应用程序,以便 next_number
有成百上千个值?例如
CREATE TABLE MyTable(
ShardNum INT64 NOT NULL,
NextNumber INT64 NOT NULL
.. etc..
) PRIMARY KEY (ShardNum, NextNumber)
请注意,我只是在讨论您的应用程序的性能。就正确性而言(即确保 next_value
是唯一的),Cloud Spanner ReadWrite 事务确保两个读者不会读取 next_number
的相同值(或者更准确地说,两个读者会不允许两者都提交)。因此,只要您将 ReadWrite 事务用于读取-修改-提交流程,就应该没问题(从正确性的角度来看)。
在 Spanner 中实现下一个数字例程的最佳方法是什么?
背景: 我们需要生成用于分配的序号。多个并发用户可以请求下一个号码。我们要确保没有用户收到相同的号码。
当前的设计是 table 包含最后使用的数字。该列将被读取,增加一个数量然后写出。事务中的读取是否会锁定正在读取的行直到事务完成?
在 Spanner 中实现下一个数字例程的最佳方法是什么?
就像您在任何数据库中一样,确保读写事务是隔离的、原子的。
事务中的读取是否锁定正在读取的行直到事务完成?
Locking read-write. This type of transaction is the only transaction type that supports writing data into Cloud Spanner.
Properties A read-write transaction in Cloud Spanner executes a set of reads and writes atomically at a single logical point in time.
Atomicity, Consistency, Durability In addition to the Isolation property, Cloud Spanner provides Atomicity (if any of the writes in the transaction commit, they all commit), Consistency (the database remains in a consistent state after the transaction) and Durability (committed data stays committed.)
直接引用自their documentation.
Performance >> Locking
Cloud Spanner allows multiple clients to simultaneously interact with the same database. In order to ensure the consistency of multiple concurrent transactions, Cloud Spanner uses a combination of shared locks and exclusive locks to control access to the data. When you perform a read as part of a transaction, Cloud Spanner acquires shared read locks, which allows other reads to still access the data until your transaction is ready to commit. When your transaction is committing and writes are being applied, the transaction attempts to upgrade to a exclusive lock. It blocks new shared read locks on the data, waits for existing shared read locks to clear, then places a exclusive lock for exclusive access to the data.
所以...我认为,如果您像本文档末尾的读写示例一样这样做,您就可以完全冷静下来。
在 Cloud Spanner 中处理序列时要非常小心,因为这可能是设计主键的 anti-pattern that can lead to hotspots (i.e. performance problems). Are you really sure you absolutely need sequential numbers? If you just need to assign people unique identifiers, there are ways to do this without requiring them to be sequential. For example, see this answer
在您的问题的应用程序描述中,您说多个并发用户可能正在请求下一个号码。这个值 (next_number
) 在整个应用程序中是否是全局唯一的?如果是这样,那么这个值将成为一个热点并且可能会限制您的数据库(和应用程序)的可扩展性,因为这意味着您的数据库的性能将受到单台机器处理单行事务的速度的限制。您能否以某种方式为数据库中的不同users/entities设置不同的next_number
?例如,每个用户都可以有一个 next_number
值吗?或者您可以 "shard" 您的应用程序,以便 next_number
有成百上千个值?例如
CREATE TABLE MyTable(
ShardNum INT64 NOT NULL,
NextNumber INT64 NOT NULL
.. etc..
) PRIMARY KEY (ShardNum, NextNumber)
请注意,我只是在讨论您的应用程序的性能。就正确性而言(即确保 next_value
是唯一的),Cloud Spanner ReadWrite 事务确保两个读者不会读取 next_number
的相同值(或者更准确地说,两个读者会不允许两者都提交)。因此,只要您将 ReadWrite 事务用于读取-修改-提交流程,就应该没问题(从正确性的角度来看)。