An example architecture used for managing product inventory which highlights the use of database transactions.
The milk problem first surfaced while working with a well-known grocery store to track product inventory in real time. The choice of database was largely driven by a non-trivial performance requirement. The initial solution used an eventually consistent database which was available and partition tolerant. Read about the CAP theorem to learn more about the relationship between consistency, availability, and partition tolerance.
The challenge is that high availability comes at the cost of consistency. High availability databases are eventually consistent, and thus are notorious for dirty reads: allowing uncommitted changes from one transaction to affect a read in another transaction. As a result, the grocery chain was unable to produce an accurate count of milk on the shelves.
The below exercise introduces the reader to transactions while highlighting the challenges of dirty reads.
Get the tests to pass!
- Remove dirty reads.
- Ensure the correct product quantities.
Look for todo items in the codebase to get started.
The below steps walk through the environment setup necessary to run the application in both local and production environments.
-
Install PostgreSQL.
brew install postgresql brew services run postgres
-
Install Flyway.
brew install flyway
-
Create a PostgreSQL database.
createdb
-
Create the milk_test database.
psql -c "create database milk_test;" psql -c "create user milk with password 'milk';"
-
Migrate the database with Flyway.
FLYWAY_CLEAN_DISABLED=false flyway -user=milk -password=milk -url="jdbc:postgresql://localhost:5432/milk_test" -locations=filesystem:databases/milk clean migrate
Use Gradle to run tests. You'll see a few failures at first.
./gradlew build
-
Create the milk_development database.
psql -c "create database milk_development;"
-
Migrate the database with Flyway.
FLYWAY_CLEAN_DISABLED=false flyway -user=milk -password=milk -url="jdbc:postgresql://localhost:5432/milk_development" -locations=filesystem:databases/milk clean migrate
-
Source the
.env
file for local development.source .env
-
Populate development data with a product scenario.
psql -f applications/products-server/src/test/resources/scenarios/products.sql milk_development
-
Use Gradle to run the products server
./gradlew applications:products-server:run
-
Use Gradle to run the simple client
./gradlew applications:simple-client:run
Hope you enjoy the exercise!
Thanks,
The IC Team
© 2023 by Initial Capacity, Inc. All rights reserved.