Spanner queues provide transactional messaging to help you manage asynchronous work. The feature pairs this capability with the scalability and reliability of Spanner, letting you build event-driven applications. Spanner queues use a pull model for message consumption, exposing a SQL interface for receivers to request and receive messages.
Choose between Spanner queues and change streams
The following comparison provides guidance on choosing the right mechanism for data propagation and asynchronous processing.
Spanner queuesBest for
Key characteristics
|
Spanner change streamsBest for
Key characteristics
|
Key advantages
Spanner queues offer several advantages:
- Integrated: messaging is integrated into the database. This eliminates the need to provision, manage, and exfiltrate data to separate messaging infrastructure, which simplifies application architecture and lowers overall costs.
- Transactional: you can send and acknowledge messages atomically within a Spanner transaction alongside other database writes. Enqueued messages are rolled back and not available for delivery if the transaction fails.
- Durable: messages that can't be delivered are stored in the database. Spanner continually attempts delivery with backoff until the message is explicitly acknowledged or deleted.
- Queryable: messages are built on the same primitives as Spanner tables and are stored as rows. You can query, join, or filter them like standard tables.
- Scalable: built with the same scalability as Spanner tables, message processing scales alongside the rest of your database.
- Reliable: Spanner queues inherit the high-availability primitives of Spanner. Message sending and processing are fault-tolerant and resilient to zonal failures in regional configurations and to regional failures in multi-region configurations. In dual-region configurations, writes resume after automatic or manual failover to the healthy region. For more information, see Regional, dual-region, and multi-region configurations.
- Schedulable: messages can be scheduled for future delivery, allowing you to defer task execution to a specific future timestamp.
- Atomic: message sending (
INSERTor mutation APIs) and acknowledgment (DELETEor mutation APIs) execute atomically within transactions, ensuring consistency with database state. - Extendable: message leases can be extended to support extremely long processing times by combining future delivery and manual leasing mechanisms.
Use cases
Spanner queues are useful for orchestrating deferred tasks within a transaction. Common examples include:
- Deferring computationally heavy work: a photo-sharing website might need to perform intensive image processing when a new photo is uploaded. The transaction that writes the new photo metadata can simultaneously write a queue message. A queue receiver later picks up the message, performs the processing, and updates the metadata transactionally.
- Deferring large transactional updates: in a calendar app, inviting a large group to a meeting in a single transaction can cause lock conflicts and tail latencies. Instead, the transaction that creates the calendar entry can add a queue entry for each invitee, allowing a receiver to send the invites individually.
- Scheduling tasks in the future: a software as a service (SaaS) company offering a 30-day free trial can provision the user's resources and simultaneously queue a message scheduled to be delivered in 30 days. A worker then receives the message and executes the trial expiration logic.
- Deferring work to external systems: after a user registers, an application might need to send a welcome email only if the database registration succeeds. The registration transaction can add an entry to a queue, allowing a worker to invoke an external email API later.
- Orchestrating multi-step pipelines: in an order management system, fulfilling an order involves multiple steps that could fail independently. By representing each step as a queue message, the system can checkpoint the pipeline's state and continue from the point of failure.
Workflow
A typical workflow for Spanner queues follows these steps:
- Create a queue: define a queue using DDL, similar to a table. It must
include a primary key and a
Payloadcolumn (payloadin PostgreSQL) declaredNOT NULL. Every other user-defined column in the queue must be part of the primary key. - Send messages: enqueue messages using standard DML (
INSERT) or theSendmutation, transactionally with other database operations. - Receive messages: use the
ExecuteStreamingSQLAPI to call a table-valued function (TVF) namedRECEIVE_QUEUE_NAME(). This function streams messages to your client as a long-running query. - Process messages: consume the messages received from the TVF with your application logic.
- Acknowledge messages: remove messages from the queue using DML
(
DELETE) or theAckmutation. This is typically done after processing is complete, within a transaction. - Manage leases: manage message leases to ensure that
Spanner queues don't redeliver messages on lease timeout.
The most common method is to use the
RENEWLEASE_QUEUE_NAME()TVF.
Additionally, keep in mind these core behaviors of Spanner queues:
- At-least-once delivery: common to most cloud-based queue systems, Spanner promises at-least-once delivery. Redeliveries can be mitigated by extending leases.
- At-most-once acknowledgment: because acknowledging a message happens over a transaction, Spanner ACID semantics ensure that a message is only acknowledged once. Following the methods on the Exactly-once processing and at-most-once acknowledgment page to properly implement at-most-once acknowledgment.
Limitations
Spanner queues have the following limitations:
- Recommended maximum receivers: use fewer than 1,000 active receive queries with the same arguments per queue. Spanner doesn't enforce this limit, but throughput might not scale if you exceed it.
- Concurrent receive TVF quota: there is a quota of 2,000 concurrent receive TVFs per project per region. To request a quota increase, see Increase your quotas.
- Manual splitting: the
AddSplitPointsmethod doesn't support queues. Workload distribution relies entirely on load-based splitting. To add split points, interleave the queue in a parent table and add split points to the parent table. - Geo-partitioning compliance: geo-partitioned queues are not data
residency compliant after executing a
DROP PARTITIONoperation. - Queue count limits: an instance can have 10 queues per 100 processing units, up to 100 queues. For more information, see Queue limits.
- Named schema: queues cannot be created in named schemas.
What's next
- Learn how to use Spanner queues, including best practices and monitoring.
- Explore more Spanner queues scenarios and examples.
- Learn about exactly-once processing and at-most-once acknowledgment.
- Configure access control with fine-grained access control for queues.