Câu hỏi phỏng vấn Microservices
Câu hỏi

Why would one use sagas ...

Câu trả lời

When deciding between using Sagas and Two-Phase Commit (2PC) for managing distributed transactions in a microservices architecture, it's essential to understand the strengths and weaknesses of each approach and the specific requirements of your system.

Sagas

Advantages:

  1. Scalability: Sagas are inherently more scalable than 2PC because they break down transactions into smaller, independent local transactions. Each service commits its transaction and publishes an event to trigger the next step, which allows the system to handle a higher volume of transactions concurrently[2][3][5].
  2. Resilience: Sagas are more resilient to failures. If a step in the transaction fails, compensating transactions are executed to undo the changes made by previous steps, ensuring eventual consistency[2][3][5].
  3. Asynchronous Processing: Sagas operate asynchronously, which can lead to better performance and reduced latency compared to the synchronous nature of 2PC[9][10].
  4. No Global Locking: Sagas do not require global locks, which can be a significant advantage in distributed systems where locking can lead to performance bottlenecks and deadlocks[2][3][5].

Disadvantages:

  1. Complexity: Implementing and maintaining Sagas can be complex, especially the compensating transactions that need to be carefully designed to handle failures[7][8].
  2. Eventual Consistency: Sagas provide eventual consistency rather than immediate consistency, which might not be suitable for all applications, especially those requiring strict consistency guarantees[2][3][5].

Two-Phase Commit (2PC)

Advantages:

  1. Strong Consistency: 2PC ensures strong consistency by making sure that all participants in a transaction either commit or roll back together, maintaining atomicity and isolation[2][3][5].
  2. Simplicity in Implementation: For the application developer, 2PC can be simpler to implement because the transaction manager handles the coordination and rollback of transactions[3][5].

Disadvantages:

  1. Performance Bottlenecks: 2PC can introduce significant performance bottlenecks due to its synchronous nature and the need to lock resources until the transaction is completed[2][3][5][9].
  2. Single Point of Failure: The coordinator in 2PC can become a single point of failure. If the coordinator fails, participants might be left in an uncertain state[2][3][5].
  3. Scalability Issues: 2PC is less scalable because it requires all participants to be available and to lock resources during the transaction, which can lead to bottlenecks and reduced system throughput[2][3][5][9].
  4. Not Suitable for NoSQL Databases: ...
expert

expert

Gợi ý câu hỏi phỏng vấn

expert

Provide an example of "smart pipes" and "dumb endpoint"

middle

What are the features of Microservices?

senior

Mention some benefits and drawbacks of an API Gateway

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào