Building micro services through Event Driven Architecture part14 : Query API.
Building micro services through Event Driven Architecture part14 : Query API
This tutorial is the 14th part of a series : Building microservices through Event Driven Architecture.
The previous step is about Building micro services through Event Driven Architecture part13 : Consume events from Apache KAFKA and project streams into ElasticSearch
In this tutorial, I will show how to build a query API that implement queries in a microservice architecture using the Command Query Responsibility Segregation (CQRS) Pattern.
The goal of the Read Model is to build a model specialized for reads, by modeling the different scenarios to present information to the users of the system.
I cannot query the event streams because they are not easy to query. I cannot also rebuild the state of the aggregate and query it in memory to get informations for the user for performance reasons..
So I will use projections to create a structural representation of the events. This is done by precalculating the current state of the aggregate and finally store it to a no-sql database. I can also precalculate and store information as needed by the end user to improve performance .
Streams has been projected into ElasticSearch, now I have to build views to display queries that retrieve data from a no sql database.
Read Model projection
I create a base class Entity that should be inherited by all views, it contains the version of the aggregate and the identifier of the aggregate.
SpeechView contains the fields that I want to be displaye for the end user.
Application
SpeechUseCase use the repository that retrieves data from elasticsearch and returns a (or list of) SpeechView
Infrastructure
Infrastructure use ElasticSearch client and Nest to perform search requests.
Presentation
Presentation is a web api core that use speechUseCase to search queries
Testing
- Start ElasticSearch
To start ElasticSearch, locate the installation of kibana and run the following command :
bin/kibana
Launch a browser and connect to http://localhost:5601
Start the following project :
- LogCorner.EduSync.Speech.Presentation
Start Postman and post a new command
You should see the following output
Code source is available here :
https://github.com/logcorner/LogCorner.EduSync.Speech.Query
Thanks for reading, if you have any feedback, feel free to post it
Regards