Here, I will discuss some common problems faced by the Developer while integrating Spring Boot with Google Cloud Spanner

Q-1). How can we connect Spanner Database from Spring Boot application?

A-1). There are 2 approaches:

  • 1st Approach
    • Use the com.google.cloud.spanner.DatabaseClient to create the connection with the Spanner Databse by providing the Database URL, Username, and Password. And then fire the query necessary CRUD queries.
    • After that, use com.google.cloud.spanner.Mutation object, fire the query to the spanner database and then capture the return type to an Iterable<Mutation> object.
  • 2nd Approach
    • You can use the spring-cloud-gcp-starter-data-spanner dependency you can create a Repository interface which will extends SpannerRepository<Entity Class Name, Data type of the Primary Key of that Entity> and define the CRUD methods.
    • While defining the name of the CRUD methods in the Repository interface, you have to use the @Query annotation where you will provide the native query as follows:
@Repository
public interface SpannerCustomerRepository extends SpannerRepository<User, Long> {

  @Query("SELECT user.user_id, user.name, user.mobile_no "
      + "FROM USER user WHERE user.user_id= @arg1")
  List<Customer> findByCustomerId(@Param("arg1") Long customerId);
}
Q-2). Very often people overcome with this situation that after providing appropriate necessary conditions still the Spring Boot app not able to connect to Spanner database locally?

A-2). In this case, check the dependencies used in the application and whether you are using the updated versions.

Another point, verify that your local machine is authenticated with the necessary Google account that you are using in your Google cloud console to connect with the various GCP services.

Last, but not least, whether the Google account you use to connect to a Spanner has the necessary IAM roles or not.

Q-3). Why does the Spanner database fail to create pooled database connections?

A-3). The main reason is the command we run to create the Spanner database. If we run the below command, this problem will not be revisited anymore.

During that time you will face this type of exception as provided below:

java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30088ms.

The appropriate command is:

gcloud spanner databases create springcavajspanner --instance=test-instance
Q-4). How to resolve Spring Boot Spanner latency-related problems?

A-4). The resolutions are provided below:

  • First, you create a Cloud Spanner instance and execute a query, the client will internally create a session pool and set up a connection to Cloud Spanner. This operation is considered as a heavy operation which can take roughly a long time of tens of ms (milliseconds). This initialization happens asynchronously. The query will have to wait for this to finish if it yet has not been completed.
  • But after, from the second query onwards which will execute on the Cloud Spanner instance will use the same already created session pool and connection to Cloud Spanner, making the query execution faster.
Q-5). What if we found any ClassCastException while integrating Spring Boot with Cloud Spanner?

A-5). Make sure that you use the right dependencies with the right version. This will resolve the ClassCastException related errors.

Examples are provided below:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner-spring-data-r2dbc</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
Q-6). Incorrect configurations can prevent sometime to connect to Spanner instance?

A-6). Sometimes incorrect configurations can lead to prevent to connect to Spanner instance.

PFB the configurations required to connect to Spanner instance.

spring.r2dbc.url=r2dbc:cloudspanner://${GCP_PROJECT}:${INSTANCE}:${DATABASE}
spring.r2dbc.username=<your-username>
spring.r2dbc.password=<your-password>
spring.cloud.gcp.spanner.instance-id=<instance-id>
spring.cloud.gcp.spanner.database=<database-id>
spring.cloud.gcp.project-id=<project-id>
Q-7). Why I am not able to connect to Spanner Instance from Spring Boot application locally?

A-7). For this problem statement the solution is as provided below:

  • You need to have a proper Gmail ID.
  • With that Gmail Id you will be able to content to Google Cloud.
  • That Gmail Id is the user by which you want to connect to various GCP Services.
  • Install google cloud SDK in your local machine.
  • Use the same Gmail Id to validate your account locally.
  • Check that the Gmail Id is having the correct IAM role to access Cloud Spanner.
  • After all these above troubleshooting steps you will be able to connect to Cloud Spanner instance locally from your Spring Boot Application.
Q-8). What are the Data Mapping issues in case of Cloud Spanner connection?

A-8). If there is a mismatch of data type between your entity with the data type of Spanner then this type of issue arises.

Q-9). What is the limited R2DBC Support?

A-9). If you encounter limitations, consider using Spring Data JDBC with Cloud Spanner instead of R2DBC. The setup will be slightly different, and you might need to include different dependencies and configurations.

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner-spring-data-jdbc</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
spring.datasource.url=jdbc:cloudspanner://${GCP_PROJECT}:${INSTANCE}:${DATABASE}
spring.datasource.username=<your-username>
spring.datasource.password=<your-password>
spring.cloud.gcp.spanner.instance-id=<instance-id>
spring.cloud.gcp.spanner.database=<database-id>
spring.cloud.gcp.project-id=<project-id>
Q-10). It will be challenging in case of multiple schema changes in a distributed environment?

A-10). You have to use Flyway or Liquibase for database migrations. These tools can help manage schema changes effectively and ensure consistency across different environments.

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>
flyway:
  url: jdbc:cloudspanner://${GCP_PROJECT}:${INSTANCE}:${DATABASE}
  user: <your-username>
  password: <your-password>
  locations: classpath:db/migration
Q-11). It can happen resource exhaustion or leaks due to mismanagement of database?

A-11). There are Spring Boot’s built-in connection pooling capabilities or a third-party library like HikariCP for managing connections efficiently. You have to configure the pool size and timeout settings according to your application’s needs.

spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.connection-timeout=20000
Q-12). How to handle the error handling and retries mechanism?

A-12). In this case, use Spring Retry or Google Cloud Spanner’s built-in retry mechanism to handle transient errors and retries.

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
@EnableRetry
public class Application {
    // Retry configuration
}
Q-13). What will be the problem statements if we don’t monitor and log the flow of the application?

A-13). Spring Boot Actuator, Google Stackdriver Logging, and Monitoring tool are the industry the best standard tools to overcome the above problem statement.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Q-14). How one will handle the network latency and connectivity issues while connecting with the Cloud Spanner instance?

A-14). There are certain steps that one can follow to reduce network latency and connectivity issues. These steps are considered as the best practices while connecting to Cloud Spanner instance.

  • Network Configuration – Check the proper network configurations, such as firewall rules, VPC Settings, to maintain stable connectivity.
  • Regional Instances – It is a good practice to use the regional instances of Cloud Spanner that are geographically closer to your application.
  • Multi-region instances – Another good practice to use multiple region instances for higher availability and reduced latency.
  • Retry Logic – To handle the transient connectivity in your applications, try implementing a robust retry logic.
Q-15). Do we need Query Optimization?

A-15). Query Optimization is required whenever you are using a database.

Some factors need to be followed for Query Optimization.

  • Indexing – Create appropriate secondary indexes on frequently used columns.
  • Query Execution Plan – Cloud Spanner has provided the query execution plan to analyze and optimize query performance.
  • Denormalization – To reduce the complexity of the queries, try to use denormalized data.
  • Read/Write Split – Separate the read and write workloads to optimize performance.
Q-16). Did archiving right balance of data consistency and availability can be challenging as it is a distributed database?

A-15). Yes, it can be challenging in a distributed database, but you can follow the below factors which will help to reduce the above problem.

  • Staleness Settings – To control the read consistency levels, use Cloud Spanner’s Staleness settings like strong, exact staleness, or bounded staleness.
  • Transaction Management – Ensure that transactions are correctly managed to maintain consistency without improving performance. You can use Spanner’s support for distributed transactions if needed.
Q-17). In a Live environment, it is a problem to change the database schema. How to handle that?

A-17). It can be handled by using the below factors:

  • Schema Migration Tools – Use Flyway or Liquibase the schema migration tools to manage schema changes seamlessly.
  • Online DDL changes – Cloud Spanner’s support for online DDL changes to modify the schema downtime.
Q-18). It is cumbersome to for important data into Cloud Spanner or exporting the data from it specially for large datasets. How to handle that?

A-18). Different tools are there to support this. PFB, the process as mentioned.

  • Dataflow – Use Google Cloud Dataflow for efficient data import/export operations.
  • Cloud Storage – Use Google Cloud Storage as an intermediary for bulk data transfers.
  • Dumps and Backups – Use Spanner’s backups and restore capabilities for data export/import and recovery.
Q-19). How to monitor and debug the distributed transactions?

A-19). Debugging and monitoring distributed transactions can always be challenging.

To avoid this type of problem please find below, the solutions as provided.

  • OpenTelemetry – Integrate OpenTelemetry for distributed tracing.
  • Stackdriver Trace – Use Stackdriver trace to monitor and troubleshoot distributed transactions.
Q-20). What are the Connection limits and quotas using Cloud Spanner?

A-20). We all know that if we exceed the connection limits and quotas it can lead to application errors and downtime.

As such there are no restrictions from Cloud Spanner but if you don’t follow safeguarding then you have to pay more amount to Google. So for this you can follow the below procedures.

  • Connection Pooling – Use connection pooling to manage database connections efficiently.
  • Quota Management – Monitor and manage Cloud Spanner quotas to avoid hitting limits.