Create a Sample Application That Supports Microservice Transactions
This tutorial describes how to create a sample application that supports microservice transactions in ScalarDB.
Overview
The sample e-commerce application shows how users can order and pay for items by using a line of credit. The use case described in this tutorial is the same as the basic ScalarDB sample but takes advantage of transactions with a two-phase commit interface when using ScalarDB.
The sample application has two microservices called the Customer Service and the Order Service based on the database-per-service pattern:
- The Customer Service manages customer information, including line-of-credit information, credit limit, and credit total.
- The Order Service is responsible for order operations like placing an order and getting order histories.
Each service has gRPC endpoints. Clients call the endpoints, and the services call each endpoint as well.
The databases that you will be using in the sample application are Cassandra and MySQL. The Customer Service and the Order Service use Cassandra and MySQL, respectively, through ScalarDB.
As shown in the diagram, both services access a small Coordinator database used for the Consensus Commit protocol. The database is service-independent and exists for managing transaction metadata for Consensus Commit in a highly available manner.
In the sample application, for ease of setup and explanation, we co-locate the Coordinator database in the same Cassandra instance of the Order Service. Alternatively, you can manage the Coordinator database as a separate database.
Since the focus of the sample application is to demonstrate using ScalarDB, application-specific error handling, authentication processing, and similar functions are not included in the sample application. For details about exception handling in ScalarDB, see How to handle exceptions.
Additionally, for the purpose of the sample application, each service has one container so that you can avoid using request routing between the services. However, for production use, because each service typically has multiple servers or hosts for scalability and availability, you should consider request routing between the services in transactions with a two-phase commit interface. For details about request routing, see Request routing in transactions with a two-phase commit interface.
Service endpoints
The endpoints defined in the services are as follows:
-
Customer Service
getCustomerInfo
payment
prepare
validate
commit
rollback
repayment
-
Order Service
placeOrder
getOrder
getOrders
What you can do in this sample application
The sample application supports the following types of transactions:
- Get customer information through the
getCustomerInfo
endpoint of the Customer Service. - Place an order by using a line of credit through the
placeOrder
endpoint of the Order Service and thepayment
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service.- Checks if the cost of the order is below the customer's credit limit.
- If the check passes, records the order history and updates the amount the customer has spent.
- Get order information by order ID through the
getOrder
endpoint of the Order Service and thegetCustomerInfo
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service. - Get order information by customer ID through the
getOrders
endpoint of the Order Service and thegetCustomerInfo
,prepare
,validate
,commit
, androllback
endpoints of the Customer Service. - Make a payment through the
repayment
endpoint of the Customer Service.- Reduces the amount the customer has spent.
The getCustomerInfo
endpoint works as a participant service endpoint when receiving a transaction ID from the coordinator.
Prerequisites
- One of the following Java Development Kits (JDKs):
- Oracle JDK LTS version (8, 11, or 17)
- OpenJDK LTS version (8, 11, or 17)
- Docker 20.10 or later with Docker Compose V2 or later
We recommend using the LTS versions mentioned above, but other non-LTS versions may work.
In addition, other JDKs should work with ScalarDB, but we haven't tested them.
Set up ScalarDB
The following sections describe how to set up the sample application that supports microservices transactions in ScalarDB.
Clone the ScalarDB samples repository
Open Terminal, then clone the ScalarDB samples repository by running the following command:
$ git clone https://github.com/scalar-labs/scalardb-samples
Then, go to the directory that contains the sample application by running the following command:
$ cd scalardb-samples/microservice-transaction-sample
Start Cassandra and MySQL
Cassandra and MySQL are already configured for the sample application, as shown in database-cassandra.properties
and database-mysql.properties
, respectively. For details about configuring the multi-storage transactions feature in ScalarDB, see How to configure ScalarDB to support multi-storage transactions.
To start Cassandra and MySQL, which are included in the Docker container for the sample application, run the following command:
$ docker-compose up -d mysql cassandra
Starting the Docker container may take more than one minute depending on your development environment.
Load the schema
The database schema (the method in which the data will be organized) for the sample application has already been defined in customer-service-schema.json
for the Customer Service and order-service-schema.json
for the Order Service.
To apply the schema, go to the ScalarDB Releases page and download the ScalarDB Schema Loader that matches the version of ScalarDB that you want to use to the scalardb-samples/microservice-transaction-sample
folder.
MySQL
To load the schema for customer-service-schema.json
into MySQL, run the following command, replacing <VERSION>
with the version of the ScalarDB Schema Loader that you downloaded:
$ java -jar scalardb-schema-loader-<VERSION>.jar --config database-mysql.properties --schema-file customer-service-schema.json
Cassandra
To load the schema for order-service-schema.json
into Cassandra, run the following command, replacing <VERSION>
with the version of the ScalarDB Schema Loader that you downloaded:
$ java -jar scalardb-schema-loader-<VERSION>.jar --config database-cassandra.properties --schema-file order-service-schema.json --coordinator
Schema details
As shown in customer-service-schema.json
for the sample application, all the tables for the Customer Service are created in the customer_service
namespace.
customer_service.customers
: a table that manages customers' informationcredit_limit
: the maximum amount of money a lender will allow each customer to spend when using a line of creditcredit_total
: the amount of money that each customer has already spent by using their line of credit
As shown in order-service-schema.json
for the sample application, all the tables for the Order Service are created in the order_service
namespace.
order_service.orders
: a table that manages order informationorder_service.statements
: a table that manages order statement informationorder_service.items
: a table that manages information of items to be ordered
The Entity Relationship Diagram for the schema is as follows:
Load the initial data by starting the microservices
Before starting the microservices, build the Docker images of the sample application by running the following command:
$ ./gradlew docker