This demo shows how to use Valkey as an in-memory cache to accelerate data retrieval in a Spring Boot application. By storing hot data in Valkey, you can reduce the number of queries to your PostgreSQL database, improving performance and scalability.
You can use docker compose to run the application locally. Run the following:
cd api
docker-compose up --buildYou can also run with sample data. Run the following:
cd sample-data
docker-compose up --buildYou can also run with a web application. Run the following:
cd web
docker-compose up --buildOnce the application is running, you can access it at http://localhost:8080 for local development, or the URL provided by Cloud Run for production.
-
You can use Terraform to deploy the infrastructure to Google Cloud. Run the following:
cd api terraform init terraform applyIt should fail to ceate the Google Cloud Run service, but don't worry, we'll fix that in the next series of steps. The message you get might look like this:
Error waiting to create Service: Error waiting for Creating Service: Error code 13, message: Revision 'caching-app-service-00001-9zj' is not ready and cannot serve traffic. Image 'gcr.io/cloudannot serve traffic. Image 'gcr.io/cloud-memorystore-demos/caching-app:latest' not found.
-
Once the infrastructure is created, you'll need to run the
init.sqlscript in the Cloud SQL instance to create the necessary tables. You can use the Cloud Shell to do this. Run the following command in the Cloud Shell:gcloud sql connect <instance_name> --database=caching-app-db --user=admin # The admin and database were created in the Terraform script
Note: Ensure that the instance name is the same as the one you used in the Terraform script.
a. When prompted to enable the Cloud SQL Admin API, type
Yand pressEnter. b. When prompted to enter the password, type the password you set in the Terraform script and pressEnter. c. Once you're connected to the Cloud SQL instance, run the following command to run theinit.sqlscript:\i init.sql
-
Finally, redeploy the Cloud Run service using the local source code. Run the following command:
gcloud run deploy <instance_name> \ --source=. \ --region=<region>
Note: Ensure that the instance name and region are the same as the ones you used in the Terraform script.
Now you should have the application running on Google Cloud.
-
Push your Docker images for the Web App and API to Google Container Registry. Run the following:
cd web docker build -t gcr.io/<project_id>/caching-app-web . docker push gcr.io/<project_id>/caching-app-web cd ../api docker build -t gcr.io/<project_id>/caching-app-api . docker push gcr.io/<project_id>/caching-app-api
-
You can use Terraform to deploy the infrastructure to Google Cloud. Run the following:
cd web terraform init terraform apply -
Once the infrastructure is created, you'll need to run the
init.sqlscript in the Cloud SQL instance to create the necessary tables. You can use the Cloud Shell to do this. Run the following command in the Cloud Shell:gcloud sql connect <instance_name> --database=caching-app-db --user=admin # The admin and database were created in the Terraform script
Note: Ensure that the instance name is the same as the one you used in the Terraform script.
a. When prompted to enable the Cloud SQL Admin API, type
Yand pressEnter. b. When prompted to enter the password, type the password you set in the Terraform script and pressEnter. c. Once you're connected to the Cloud SQL instance, run the following command to run theinit.sqlscript:\i init.sql
Now you should have the application running on Google Cloud.
GET /item/{id}: Get an item by IDPOST /item/create: Create a new itemDELETE /item/delete/{id}: Delete an item by ID