Giter Site home page Giter Site logo

spring-sagas's Introduction

https://www.youtube.com/watch?v=YPbGW3Fnmbc

Spring Sagas

Use Case:

when an user places an order, order will be fulfilled if the product's price is within
the user's credit limit/balance. Otherwise it will not be fulfilled.

Screenshot 2023-04-16 at 12 53 33 PM

In the above pic, both the applications are pointing to the single database as the system is monolithic 
and can be considered as a single transaction.

Screenshot 2023-04-16 at 12 56 56 PM

In case there's some issue with the Process Payment entire data will be rolled back. Not a single 
data is inserted to db that's how transaction handled at monolithic.


Microservices generally follow database per service pattern. As we can see in the below images, 
2 different databases are used. For monolithic the approach should be the same as monolithic.

Screenshot 2023-04-16 at 1 02 21 PM

Screenshot 2023-04-16 at 1 03 59 PM

Screenshot 2023-04-16 at 1 07 31 PM

1. It's not a good approach to fire n number of HTTP requests. 
Eveytime a new service is added, HTTP request needs to be fired.
2. It will impact on the revenue is there's a application down time.

To overcome this issue, microservices provided a pattern called saga pattern.

We can implement saga pattern using two approaches:
1. Choreography
2. Orchestration

We can use Choreography pattern with the help of event sourcing so there will be no HTTP req call.

Following pic can be used to understand how transaction is done with the help of kafka topics 
and there are no HTTP event calls:

Screenshot 2023-04-16 at 1 13 03 PM

Screenshot 2023-04-16 at 1 16 01 PM

Screenshot 2023-04-16 at 1 20 27 PM

Screenshot 2023-04-16 at 1 23 10 PM

# saga-choreography-example

request

curl --location --request POST 'http://localhost:8081/order/create' \
--header 'Content-Type: application/json' \
--data-raw '{
    "userId": 103,
    "productId": 33,
    "amount": 4000
}'

Kafka payload

Happy scenario

{"eventId":"b0e47448-eeb8-4cf4-bd29-b3a4315fc592","date":"2021-09-03T17:26:46.777+00:00","orderRequestDto":{"userId":103,"productId":33,"amount":4000,"orderId":1},"orderStatus":"ORDER_CREATED"}
{"eventId":"c48c5593-9f81-4ab4-9de8-b9fca2d2bef2","date":"2021-09-03T17:26:51.989+00:00","paymentRequestDto":{"orderId":1,"userId":103,"amount":4000},"paymentStatus":"PAYMENT_COMPLETED"}

Request

curl --location --request POST 'http://localhost:8081/orders' \
--header 'Content-Type: application/json' \
--data-raw '{
    "userId": 103,
    "productId": 12,
    "amount": 800
}'

insufficent amount

{"eventId":"fecacc77-017d-49cd-bdfa-58e47170da49","date":"2021-09-03T17:28:23.126+00:00","orderRequestDto":{"userId":103,"productId":12,"amount":800,"orderId":2},"orderStatus":"ORDER_CANCELLED"}
{"eventId":"46378bbc-5d15-4436-bed1-c6f3ddb1dc31","date":"2021-09-03T17:28:15.940+00:00","paymentRequestDto":{"orderId":2,"userId":103,"amount":800},"paymentStatus":"PAYMENT_FAILED"}
curl --location --request GET 'http://localhost:8081/orders' \
--header 'Content-Type: application/json' \
--data-raw ''

Response

[
    {
        "id": 1,
        "userId": 103,
        "productId": 33,
        "price": 4000,
        "orderStatus": "ORDER_COMPLETED",
        "paymentStatus": "PAYMENT_COMPLETED"
    },
    {
        "id": 2,
        "userId": 103,
        "productId": 12,
        "price": 800,
        "orderStatus": "ORDER_CANCELLED",
        "paymentStatus": "PAYMENT_FAILED"
    }
]

user Balance

Screen Shot 1400-06-12 at 23 01 03

Order repo

Screen Shot 1400-06-12 at 23 01 19

Screenshot 2023-04-16 at 2 01 26 PM

Screenshot 2023-04-16 at 2 08 37 PM

Screenshot 2023-04-16 at 2 09 33 PM

Screenshot 2023-04-16 at 2 14 34 PM

Screenshot 2023-04-16 at 2 14 59 PM

Screenshot 2023-04-16 at 2 20 23 PM

Screenshot 2023-04-16 at 2 21 49 PM

Screenshot 2023-04-16 at 2 22 54 PM

Screenshot 2023-04-16 at 2 25 53 PM

Screenshot 2023-04-16 at 2 28 50 PM

Screenshot 2023-04-16 at 2 31 08 PM

Screenshot 2023-04-16 at 2 32 09 PM

Screenshot 2023-04-16 at 2 33 29 PM

Screenshot 2023-04-16 at 2 37 56 PM

Screenshot 2023-04-16 at 2 38 11 PM

Screenshot 2023-04-16 at 2 42 12 PM

Screenshot 2023-04-16 at 2 42 48 PM

Screenshot 2023-04-16 at 2 48 08 PM

Screenshot 2023-04-16 at 2 49 07 PM

Screenshot 2023-04-16 at 2 49 29 PM

Screenshot 2023-04-16 at 2 50 02 PM

Screenshot 2023-04-16 at 2 50 15 PM

Screenshot 2023-04-16 at 2 50 41 PM

Screenshot 2023-04-16 at 2 51 18 PM

Screenshot 2023-04-16 at 2 51 42 PM

Screenshot 2023-04-16 at 2 51 59 PM

Screenshot 2023-04-16 at 2 52 15 PM

Screenshot 2023-04-16 at 2 52 47 PM

Screenshot 2023-04-16 at 2 53 04 PM

Screenshot 2023-04-16 at 2 53 32 PM

Screenshot 2023-04-16 at 2 53 54 PM

Uploading Screenshot 2023-04-16 at 2.54.44 PM.png…

spring-sagas's People

Contributors

saiashish9 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.