[Microservice theory] Practical application of CQRS

Stratification

The so-called architecture is one layer plus one layer.

When our layer is too redundant, add another layer.
When the coupling between our two layers is too severe, add another layer.
When we don’t want everyone to do this, we add a layer to do it for us.

CQRS

CQS (Command Query Separation, command query separation)

In many cases, we hope to access faster and reduce the impact of the query on the interface, and this data is refreshed regularly. How to do?

Separate commands and queries. Use a command to periodically generate data to the cache, and then query the cache directly.

The same logic applies to asynchronous queues.

Return to the interface when the things to be done immediately are completed, and then throw the unimportant things into the queue for processing slowly.

Example 1: Activities

The activity is continuous, and the activity data is relatively large, so the query is more time-consuming, and the problem of the first personal card cannot be avoided (people later will read the cache).

Then use a command in the background to calculate once a minute to update the data, and the problem of the first personal card when reading is no longer present.

Example 2: Manuscript and review:

A manuscript is a data with a huge amount of data and many fields.
And the audit has background permissions, audit time, executor, etc. fields.

There is only one attribute related to the two: whether to display .

So, do we need to make them pay attention to each other's fields? No need!

So use a queue, extract the attributes we want into a new table, and then do the background logic.

Insert picture description here

Example 3: Still active

If there is an event, we want to get the ranking of certain kinds of gifts received during this period.

The easiest is to directly check the gift record summary table.

But we only need to care about a few kinds of gifts, and we can also take a part of the gift record and make special activity ranking. The calculation will be faster and more flexible.

For example, if the same gift adds different activity points in each time period, this method is very useful.

Pros and cons

advantage:

  • Speed ​​up the access speed.
  • Simplified the logic. Write only write, read only read.
    Disadvantages:
  • If no one visits, there will be consumption.
  • Some scenes are delayed, after all, they are asynchronous.

to sum up

Strip the unimportant things.

This unimportant thing can be:

  • Unimportant data-extract data.
  • Unimportant processing-take the asynchronous queue.

Guess you like

Origin blog.csdn.net/happy_teemo/article/details/113140525