Cloud Spanner is a scalable, enterprise-grade, globally-distributed, and strongly consistent database service built for the cloud specifically to combine the benefits of relational database structure with non-relational horizontal scale. It delivers high-performance transactions and strong consistency across rows, regions, and continents with an industry-leading 99.999% availability SLA, no planned downtime, and enterprise-grade security. Cloud Spanner revolutionizes database administration and management and makes application development more efficient.
This example creates a new regional instance (i.e., spanning across zones within the same region). Cloud Spanner supports multi-regional configuration to span across multiple regions for highest availability.
Create a Database
A Cloud Spanner instance can host multiple databases, that each contains its own tables.
There is no interactive CLI to Cloud Spanner. You can create table and execute SQL statements from the Cloud Console, or from gcloud command line. Following are some common operations:
CREATETABLEorders ( order_id STRING(36) NOT NULL,description STRING(255), creation_timestamp TIMESTAMP,) PRIMARY KEY (order_id);CREATETABLEorder_items ( order_id STRING(36) NOT NULL, order_item_id STRING(36) NOT NULL,description STRING(255), quantity INT64,) PRIMARY KEY (order_id, order_item_id), INTERLEAVE IN PARENT orders ON DELETE CASCADE;
Cloud Spanner differs from traditional RDBMS in a several ways:
No server-side automatic ID generation.
Avoid monodically increasing IDs - I.e., no auto incremented ID, because it may create hot spots in partitions.
No foreign key constraints.
Cloud Spanner, being horizontally scalable and shards data with your keys, prefers the following:
UUIDv4 as IDs - It's random and can be partitioned easily.
Parent-Children relationship encoded using a compose primary key.
Colocate Parent-Children data using Interleave tables - If accessing parent means children data is also likely to be read, interleaving allows children data to be colocated with parent row in the same parition.
The easiest way to access Cloud Spanner is using Spring Cloud GCP's Spring Data Spanner starter. This starter provides full Spring Data support for Cloud Spanner while implementing idiomatic access patterns.
Notice that there is no explicit configuration for username/password. Cloud Spanner authentication uses the GCP credential (either your user credential, or Service Account credential), and authorization is configured via Identity Access Management (IAM).
Order is the parent and has only a primary key. Spring Data repository's ID parameter type can be the type for the single key. OrderItem, however, has a composite key. Spring Data repository's ID parameter type must be Cloud Spanner's Key type for a composite key.
In a business logic service, you can utilize the repositories:
@ServiceclassOrderService {privatefinalOrderRepository orderRepository;OrderService(OrderRepository orderRepository,OrderItemRepository orderItemRepository) {this.orderRepository= orderRepository; } @TransactionalOrdercreateOrder(Order order) {// Use UUID String representation for the IDorder.setId(UUID.randomUUID().toString());// Set the creation timeorder.setCreationTimestamp(LocalDateTime.now());// Set the parent Order ID and children ID for each item.if (order.getItems() !=null) {order.getItems().stream().forEach(orderItem -> {orderItem.setOrderId(order.getId());orderItem.setOrderItemId(UUID.randomUUID().toString()); }); }// Children are saved in cascade.returnorderRepository.save(order); }}
Rest Repository
Spring Data Rest can expose a Spring Data repository directly on a RESTful endpoint, and rendering the payload as JSON with HATEOS format. It supports common access patterns like pagination.