What is the best practice to implement CQRS with elasticsearch in microservices style?

Viewed 563

I'm trying to implement CQRS architecture in Microservices style, what I've decided is to use elastic search and READ and Postgresql as our WRITE database.

By the ‌assumption that we have 3 microservices and every microservice has 1 database

     user table 
                    
 id |   name   | age 
----+----------+------
  1 | Robert   |  30
  2 | Jessica  |  40
  3 | Jennifer |  50
  4 | Jack     |  600
       event

 id | user_id | event_name 
----+---------+------
  1 |    3    |  python
  2 |    4    |  elasticsearch
  3 |    2    |  postgres
  4 |    2    |  cqrs

      finantial

 id | user_id |  cash 
----+---------+------
  1 |     1   |  30
  2 |     2   |  40
  3 |     3   |  50
  4 |     4   |  600

I don't know,but I think the structure below for elasticsearch is good

PUT test
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age":{
        "type": "integer"
      },
      "events": {
        "type": "nested"
     },
      "cash": {
        "type": "integer"
      }
    }
  }
  
}

Now what is the best practice to implement data structure in elasticsearch index ?

1 Answers

Elasticsearch can be easily used in your use-case and its very common use-case, although there are some troubles around nested field as show-cases in Go-jek engineering blog, but if you have less scale and properly configured ES cluster, you will not face significant issues.

Regarding write performance again, if you have very high write QPS than it might impact but again there are a lot of ways to optimize the write operation, which I covered in my short tips to improve indexing rate and reindexing performance.

Related