Command Query Responsibility Segregation Pattern (CQRS)

Command Query Responsibility Segregation Pattern (CQRS)

Command Query Responsibility Segregation (CQRS) là m?u thi?t k? tách bi?t ho?t ??ng ??c và ghi d? li?u trong m?t h? th?ng. Vi?c tách bi?t này giúp t?ng kh? n?ng m? r?ng, b?o m?t và hi?u su?t, khi?n CQRS tr? thành m?t l?a ch?n ph? bi?n cho các ?ng d?ng ph?c t?p.


Tìm hi?u v? CQRS

CQRS chia h? th?ng thành hai thành ph?n chính:

  • Phía Command ??: X? ly các thao tác t?o, c?p nh?t và xoá d? li?u (CUD).
  • Phía Query ??: X? ly các ho?t ??ng ??c d? li?u ??c l?p ?? t?i ?u hi?u su?t.

Vi?c tách bi?t này cho phép t?ng kh? n?ng m? r?ng c?a các ho?t ??ng ??c và ghi m?t cách ??c l?p.



L?i ích c?a CQRS

1. Kh? n?ng m? r?ng ??

  • Các ho?t ??ng ??c và ghi có th? ???c m? r?ng ??c l?p.
  • Cho phép s? d?ng caching ?? t?ng hi?u su?t.

2. B?o m?t ??

  • Tách bi?t các model giúp ??m b?o quy?n truy c?p khác nhau.
  • Gi?m nguy c? thao tác kh?ng ?úng lên d? li?u.

3. T?i ?u hi?u su?t ?

  • Query model có th? ???c t?i ?u hoá cho truy v?n nhanh.
  • Gi?m tình tr?ng t?c ngh? b?ng vi?c tách bi?t model.

4. Linh ho?t ??

  • H? tr? s? d?ng nhi?u c? s? d? li?u khác nhau.
  • D? k?t h?p v?i Event Sourcing.


Tri?n khai CQRS trong Spring Boot

1. Phía Command

@RestController
@RequestMapping("/commands")
public class CommandController {
    private final CommandService commandService;
    
    public CommandController(CommandService commandService) {
        this.commandService = commandService;
    }
    
    @PostMapping("/create")
    public ResponseEntity<String> createItem(@RequestBody CreateItemCommand command) {
        commandService.handle(command);
        return ResponseEntity.ok("T?o m?i thành c?ng");
    }
}        

2. Phía Query

@RestController
@RequestMapping("/queries")
public class QueryController {
    private final QueryService queryService;
    
    public QueryController(QueryService queryService) {
        this.queryService = queryService;
    }
    
    @GetMapping("/items")
    public List<ItemDTO> getAllItems() {
        return queryService.getAllItems();
    }
}        

Khi nào nên s? d?ng CQRS?

? H? th?ng có kh?i l??ng ??c và ghi khác nhau l?n.

? Yêu c?u b?o m?t và phan quy?n cao.

? Các ?ng d?ng ??ng b? s? ki?n.

? Các h? th?ng l?n c?n t?i ?u hi?u su?t.

? Kh?ng thích h?p v?i các ?ng d?ng CRUD ??n gi?n do t?ng ?? ph?c t?p.


K?t lu?n ??

CQRS mang l?i nhi?u l?i ích v? hi?u su?t, kh? n?ng m? r?ng và linh ho?t. Tuy nhiên, nó c?ng ?i kèm v?i ?? ph?c t?p cao h?n. H?y xem xét k? l??ng tr??c khi áp d?ng CQRS cho d? án c?a b?n! ??

??t Tr??ng Thành

? Software Developer | Java Spring boot | Database | Studies Performance Tuning at Wecommit Vi?t Nam. Let's connect!

1 个月

Hi Liam, thank you for your article. However, I think your model is not realistic. In real-world scenarios, we typically use different databases—one for writing and another for reading, for example: You can use postgres for writing and elasticsearch for reading

  • 该图片无替代文字
Bui Duc Hieu

??Full-stack Developer | Database Optimization

1 个月

R?t thi?t th?c ?. ?úng cái em ?ang tìm

要查看或添加评论,请登录

Ngo Tran Quoc Tuan (Liam)的更多文章

  • ?? JSON Web Token (JWT)

    ?? JSON Web Token (JWT)

    1. ?? Gi?i thi?u v? JWT JWT (JSON Web Token) là m?t chu?n m? (RFC 7519) cho phép truy?n th?ng tin m?t cách an toàn gi?a…

    1 条评论
  • ?? L?p trình h??ng ??i t??ng (OOP)

    ?? L?p trình h??ng ??i t??ng (OOP)

    Là m?t l?p trình viên m?i làm vi?c v?i Spring Boot, hi?u r? các nguyên t?c c?a L?p trình h??ng ??i t??ng (OOP) là r?t…

    1 条评论
  • ? Understanding JDBC: Java Database Connectivity ?

    ? Understanding JDBC: Java Database Connectivity ?

    Java Database Connectivity (JDBC) is an API that enables Java applications to interact with relational databases. It…

    2 条评论
  • ?? Understanding JPA in Java: A Comprehensive Guide to ??? Jakarta Persistence API

    ?? Understanding JPA in Java: A Comprehensive Guide to ??? Jakarta Persistence API

    ?? Introduction to JPA in Java Java Persistence API (JPA) is a standard specification for object-relational mapping…

  • Hibernate: A Comprehensive Guide to Java's Powerful ORM Framework

    Hibernate: A Comprehensive Guide to Java's Powerful ORM Framework

    What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) framework that operates at the persistence layer of…

  • Hibernate Criteria API vs JPQL

    Hibernate Criteria API vs JPQL

    Both Hibernate Criteria API and JPQL are used for querying data in a Hibernate/JPA environment, but they differ in how…

  • N+1 query problem

    N+1 query problem

    1. V?n ?? N+1 query là gì V?n ?? N+1 queries x?y ra khi m?t ORM nh? Hibernate th?c thi m?t truy v?n SQL ?? truy xu?t…

    1 条评论