concept database in category microservice

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform.
An individual microservice might be exposed to the outside world so our customers can interact with it or it might be purely an internal service and not externally accessible. It typically has access to a database, file storage or some other method of state persistence. A microservice’s internal and external relationships are illustrated in figure 1.3.
When building any application, typically we’ll need to deal with data or files and sometimes both. Microservices are no different. We need a database to store dynamic data being generated and updated by the application and we need a place to store assets that are served by the application or uploaded to it.
In this chapter, we are adding both file storage and a database to our example application FlixTube. First, we’ll be adding file storage so FlixTube has a location to store its videos. We’d like to have distinct areas of responsibility in our application for streaming and storage of videos. That implies that we’ll need to add another microservice to our application and in this chapter, we will indeed create our second microservice.
Then we’ll be adding a database. At this point, we are adding a database so that we have a place to record the path to each video, but this is really just an excuse to get a database in place. Because once we have it we can easily use it to store all metadata for our videos and to provide for the ongoing data storage needs of all our microservices.
Listing 4.5 - Adding a MongoDB database to our Docker Compose file (chapter-4/example-3/docker-compose.yaml)
version: '3' services: db: #A image: mongo:4.2.8 #B container_name: db #C ports: - "4000:27017" #D restart: always #E azure-storage: image: azure-storage build: context: ./azure-storage dockerfile: Dockerfile container_name: video-storage ports: - "4001:80" environment: - PORT=80 - STORAGE_ACCOUNT_NAME=<your Azure storage account name here> - STORAGE_ACCESS_KEY=<your Azure storage account key here> restart: “no” video-streaming: image: video-streaming build: context: ./video-streaming dockerfile: Dockerfile container_name: video-streaming ports: - "4002:3000" environment: - PORT=80 - DBHOST=mongodb://db:27017 #F - DBNAME=video-streaming #G - VIDEO_STORAGE_HOST=video-storage - VIDEO_STORAGE_PORT=80 restart: “no”

This is an excerpt from Manning's book Enterprise Java Microservices.
For the most part, you should never concern yourself with the specifics of what each Spring Boot starter dependency provides. Generally, it’s enough to know that the web starter enables you to build a web application, the Thymeleaf starter enables you to use Thymeleaf templates, and the Spring Data JPA starter enables data persistence to a database by using Spring Data JPA.
Integration testing covers the entirety of your service, in addition to the way it interacts with external services, such as a database.
Integration testing is similar to unit testing, and uses the same frameworks, but it’s also used to test a microservice interaction with external systems. This could include databases, messaging systems, other microservices, or pretty much anything it needs to talk to that isn’t internal code to the microservice. If you had unit tests that used mocks or stubs to integrate with external systems, as part of integration testing the mocks and stubs are replaced with calls to the actual systems instead. Removing mocks or stubs opens your code to execution paths that haven’t been tested before, as well as introducing more test scenarios as you need to test handling of errors in those external systems.

This is an excerpt from Manning's book Microservices in .NET Core: with examples in Nancy.
Let’s consider a Recommendation microservice. It implements and runs the algorithms that drive recommendations for your e-commerce site. It also has a database that stores the data needed to provide recommendations. The algorithms run in one process, and the database runs in another. Often, a microservice needs two or more processes so it can implement everything (such as data storage and background processing) it needs in order to provide a capability to the system.
Whereas a more traditional ORM writes all the SQL required to read data from and write it to the database, Dapper expects you to write your own SQL. I find this to be liberating when dealing with a database with a simple schema.
In the spirit of choosing lightweight technologies for microservices, I choose to use Dapper over a full-fledged ORM like Entity Framework or NHibernate. Often the database for a microservice is simple, and in such cases I find it easiest to add a thin layer—like Dapper—on top of it for a simpler solution overall. I could have chosen to use any of the other micro ORMs, but I like Dapper. In this chapter, you’ll use Dapper to talk to SQL Server, but Dapper also works with other SQL databases like PostgreSQL and MySQL.
Once you’ve decided which data a microservice owns, storing that data is relatively straightforward. The details of how it’s done depend on your choice of database. The only difference specific to microservices is that the data store is solely owned and accessed by the microservice itself.
With the database in place, you can implement the code in the Shopping Cart microservice that reads, writes, and updates the database. You’ll install the Dapper NuGet package into the microservice. Remember that you do this by adding Dapper to the project.json file and running the dotnet restore command from PowerShell. The dependencies section of the project.json file should look like this:
![]()
In chapter 2, the Shopping Cart microservice was expecting an implementation of an IShoppingCart interface. You’ll change that interface slightly to allow the implementation of it to make asynchronous calls to the database. This is the modified interface:
public interface IShoppingCartStore { Task<ShoppingCart> Get(int userId); Task Save(ShoppingCart shoppingCart); }Now it’s time to look at the implementation of the IShoppingCartStore interface. First, let’s consider the code for reading a shopping cart from the database.