HLD 4: Microservice Design Pattern

  1. Strangler Pattern.
  2. SAGA Pattern
  3. CQRS
  1. Strangler Pattern (ghotna i.e monolithic ko dhire dhire ghot rahe hai)
    used for Refactoring to microservice
    Strangler means ghotna i.e monolithic ko dhire dhire ghot rahe hai.
    i.e starting me 10 % traffic deye microservices ko and 90 monolithic ko
    let’s say ko but aa gya microservice pai then microservices pai traffic 0% kar deye and fix karne kai baad fir traffic badha deye. and once we are confident about microservice dhire dhire traffic badhate jayege and finally monolithic pai 0% traffic jane lagega …
  2. SAGA Pattern
    Database management
    1. Database for each service (SAGA and CQRS)
    2. Shared Database : ( not successful because it is hard to scale)
    easy-to-do transaction property means ACID property
    easy to query join
    jo easy hai shared database kai leye wo complicated ho jata hai for individual database
    i.e 1. local transaction karna easy hoga, but aak order 3 service kai DB ko touch kar raha hai, now our transaction is spanning over a distributed DB this problem is solved by SAGA Pattern
    2. joining problem ko CQRS ne solve kiya hai ..

Database per service


  1. each service have own DB
  2. one service can not query on other DB, agar data chahiye to s3 ko bolega or api call karega and related data lega …
  3. apne apne need kai according apne apne DB ko use kar sakte hai. now benefit is one can use SQL or other other using NoSQL. jab bhi modification karne ki jarurat hai to apne kai bare mai sirf sochna hai so maintain karna easy ho gya
  4. jis DB mai query jada aa rha to sirf ussi ko scale kar sakte hai i.e jada transaction aa rha hai to usi ko scale kar sakte hai hence scaling is easy

SAGA Pattern


SAGA is Sequence of Local transaction
aak event trigger karta hai for success for next process and failure event trigger karta hai for previous service local transaction if it’s local transaction get failed

Type of SAGA Pattern

  1. choreography-based: sab apne event ko sun rahe hai and apna work kar rahe hai ..
    drawback:
    cycle dependency ban sakti, s1 s2 ko event chora, s2 s1 ko chora is tarah ki cycle dependency ban sakti hai

2. orchestration-based:
orchestrator sab kuch manage karta hai s1, s2, s3 event kai through listen nhi karta hai. balji jab s1 success hoga tab orchestrator s2 ko call karega s2 sucess hoga to s3 ko call karega let’s say s3 fail kar gya then again orchestrator s2 ko call karega for roll-back and then again orchestrator s1 ko call karega for call back.

Interview Question
1. for payment transaction you have 2 database a) balance b) payment let’s say your system fail during payment record after deducting the money from balance how will you fix this.
ans: we will use SAGA here

CQRS

command qyery Request Segragattion

command: CUD (create, update, delete)
Query: Select

create update delete operation apne apne DB pai karege but jitna bhi read operation hoga wo new DB pai hoga
challege: view wale database ko kaise update rakhega
1. event kai through (jab bhi local database mai change hoga event trigger karega and view wala apne mai bhi update kar lega
2. DB trigger or procedure run ho

Leave a Comment