Microservices - Polyglot persistence
By choosing to decentralize your data stores, you promote polyglot persistence among your microservices and identify your data storage technology based on the data access patterns and other requirements of your microservices. Each microservice has its data store and can be independently scaled with low-impact schema changes, and data is gated through the microservice’s API. Decentralized polyglot persistence also typically results in eventual data consistency, and other potential challenges that require a thorough evaluation include data synchronization during transactions, transactional integrity, data duplication, and joins and latency.
Also, for development and test environments, having your databases running as containers is convenient, because you don't have any external dependency and simply running the docker-compose up command starts the whole application. Here we will look at the following -
- MS SQL Server
- Mongo DB
- CosmosDB emulator
MS SQL Server
When you start this SQL Server container for the first time, the container initializes SQL Server with the password that you provide. Once SQL Server is running as a container, you can update the database by connecting through any regular SQL connection, such as from SQL Server Management Studio, Visual Studio, or C# code.
Create a file called mssql-docker-compose.yml in your project directory and paste the following:
version: '3.2'
services:
sql-server-db:
container_name: sql-server-db
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "password#123"
ACCEPT_EULA: "Y"
volumes:
- sql_vol:/var/opt/mssql
volumes:
sql_vol:
Once the container is started, you can connect using SQL Server Management studio. The application connection string would be -
"ConnectionString": "Server=tcp:127.0.0.1,1433;Initial Catalog=xyz;User Id=sa;Password=password#123"
Note: /var/opt/mssql is location within the conatainer where data files are stored. This is being mapped to the sql_vol of the host.
MongoDB Server
If you have an application and a MongoDB container both running on the same machine, you can use Docker Compose to start and stop them together. Docker Compose is better suited for development or testing environments where you don’t need the full functionalities of MongoDB Enterprise or MongoDB Atlas.
In a docker-compose.yaml file, describe all of your containers that are part of the application. One of your containers could be a MongoDB server.
Create a file called mongo-docker-compose.yml in your project directory and paste the following:
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- mongodb_vol:/data/db
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- mongodb
volumes:
mongodb_vol:
Note: /data/db is a location within the container where data files are stored. This is being mapped to the sql_vol of the host.
CosmosDB emulator
Create a file called cosmos-docker-compose.yml in your project directory and paste the following:
version: '3.4'
services:
azure.cosmosdb:
image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
container_name: azure.cosmosdb
environment:
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=5
- AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
- AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=192.168.0.107
ports:
- 8081:8081
- 10251:10251
- 10252:10252
- 10253:10253
- 10254:10254
Install Certificate
After the simulator is running you need to download the simulator self-signed certificate and install it in your machine's trusted store. To download the certificate just open in the browser following https://localhost:8081/_explorer/emulator.pem link and save the certificate file, but make sure you gave it .crt extension. Or you can run the following command:
curl -k https://localhost:8081/_explorer/emulator.pem > azure.cosmosdb.crt
NOTE: if you open this link in your browser it will show you a warning that you are trying to access a suspicious site, which is expected because we did not install the certificate yet.
Because this is a self-signed certificate we need to add it to our trusted certificates.
On Windows
- Double-click on the certificate.
- Click Install certificate….
- Select Current User and click Next.
- Select Place all certificates in the following store and click Browse.
- Select Trusted Root Certification Authority and click Ok, then Next and Finish.
After the certificate was installed you may test it by navigating to the emulator portal using the following link https://localhost:8081/_explorer/index.html.
Reference:
https://libertus.dev/posts/connect-to-cosmosbd-emulator-on-docker/part1/
https://libertus.dev/posts/connect-to-cosmosbd-emulator-on-docker/part2/