✍️
ZiXiang-Blog
  • Jerry Wang Blog
  • 使用 TorchServe 部署 Model
  • How do AppRTC work in WebRTC mechanism ?
  • [系統設計]- 容易產生設計盲點
  • Golang
    • [Golang] Goroutine Concurrency多執行緒淺談
    • [Golang]: 進階用法
    • Golang go mod 入門
    • [Golang] 讓 Goroutine Debug 變得更簡單
  • Security
    • HTTP Token 使用方式: Basic Token v.s Bearer Token
    • 從 RFC 規格書觀點解析 OAuth 2.0
    • 區塊鏈物聯網架構 解決哪些安全性議題?
  • Broker
    • [深入淺出MQTT]: v3.1.1與v5 的差異
    • Broker 到底是什麼?
  • patterns
    • Patterns for Organizing Domain Logic
    • Domain Model: 從無到有規劃新的服務
    • 淺談CQRS
    • Backend System in Microservice Architecture: Where Does data store?
    • Ambassador pattern with Shared Database Pattern
    • Microservice Pattern 犧牲了什麼?
  • K8s
    • 分散式系統: 現代軟體架構與設計考量
    • Kubernetes 超入門
    • Kubernetes 架構
    • K8s 在 DevOps 中的作用
  • Database
    • Relation Database Index Overview
    • Draft: RDBMS(MySQL) v.s NoSQL (Monogo)
    • [淺談]- How Do RDBMS Thread work ?
    • [淺談]-NoSQL資料庫怎麼選?
    • How do pick the database more correctly?
    • Draft: 關聯式資料庫需要知道的幾件事
  • HTTP
    • Overview The WebSocket Mechanism
Powered by GitBook
On this page
  • CQS
  • CQRS
  • 參考資料

Was this helpful?

  1. patterns

淺談CQRS

CQRS (Command Query Responsibility Segregation) 命令查詢職責分離模式,在談論這個之前必須要先談論OO(Object-Oriented)裡面的CQS(Command Query Separation)因為兩者的概念一樣,只是應用層級不太一樣。

CQS

CQS 使用於寫程式層級,將Object主要分為兩種動作command、query:

  • Command: 指的是改變物件狀態的動作,ex: Create, Update, Delete

  • Query: 指的是不會改變物件狀態的動作,ex: Read.

舉個例子:

function Array() {
    this.arr = []
}

// Insert 屬於 "Command" 因為所執行的事情是將element 存入 arr 中,改變了狀態
Array.prototype.Insert = function (element) {
    this.arr.push(element)
}

// Read 屬於 "Query" 因為只是讀取並未改變Array 整個物件的狀態
Array.prototype.Read = function (index) {
    return this.arr[index]
}

其中CQS 有一個特性,會改變狀態的方法不會回應物件狀態,反之不會改變狀態的回應物件狀態。換句話說command 因為是改變狀態的動作,則會不會有Return ; Query 則會有Return。

CQRS

也別於CQS,CQRS 則是將 Command、Query的概念拉到系統層級,以資料庫中Create, Read, Update, Delete (CRUD) 四個動作為例,大多時候Read才是大量被使用的。而使用CQRS 就能夠輕易的增加Read 用的伺服器,為此增加彈性,並且能夠輕易的做出針對性的效能教調。

舉個例子:

如果將資料庫的存取拆分成Command、Query,資料庫的選擇上會有一些變化,如Command 換成寫入較快的,Query 則換成讀取較快的。透過資料庫本身的Master-slaves 模式就可以進而提昇效能。如下示意圖。

master-slave pattern 指的是主動或者被動的方式將資料同步到其他資料庫

參考資料

PreviousDomain Model: 從無到有規劃新的服務NextBackend System in Microservice Architecture: Where Does data store?

Last updated 3 years ago

Was this helpful?

[1] [2]

https://martinfowler.com/bliki/CQRS.html
https://martinfowler.com/bliki/CommandQuerySeparation.html