Giter VIP home page Giter VIP logo

saga's Introduction

Module Saga

Maven Central Latest snapshot Website can be found here

Add in build.gradle.kts

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.nomisrev:Saga:1.0.1")
}

The saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios.

A [Saga] is useful when you need to manage data in a consistent manner across services in distributed transaction scenarios. Or when you need to compose multiple [action]s with a [compensation] that needs to run in a transaction like style. For example, let's say that we have following domain types Order, Payment.

data class Order(val id: UUID, val amount: Long)
data class Payment(val id: UUID, val orderId: UUID)

The creation of an Order can only remain when a payment has been made.

In SQL you might run this inside a transaction, which can automatically rollback the creation of the Order when the creation of the Payment fails. When you need to do this across distributed services, or a multiple atomic references, etc you need to manually facilitate the rolling back of the performed actions, or compensating actions.

The [Saga] type, and [saga] DSL remove all the boilerplate of manually having to facilitate this with a convenient suspending DSL.

data class Order(val id: UUID, val amount: Long)

suspend fun createOrder(): Order = Order(UUID.randomUUID(), 100L)
suspend fun deleteOrder(order: Order): Unit = println("Deleting $order")

data class Payment(val id: UUID, val orderId: UUID)

suspend fun createPayment(order: Order): Payment = Payment(UUID.randomUUID(), order.id)
suspend fun deletePayment(payment: Payment): Unit = println("Deleting $payment")

suspend fun Payment.awaitSuccess(): Unit = throw RuntimeException("Payment Failed")

suspend fun main() {
  saga {
    val order = saga(action = { createOrder() }, compensation = { ::deleteOrder })
    val payment = saga(action = { createPayment(order) }, compensation = { ::deletePayment) })
    payment.awaitSuccess()
  }.transact()
}

// Deleting Payment(id=5753e9bb-248a-4385-8c9c-4a524e80c0f9, orderId=3a55ffab-a3f5-40a9-a2b3-681dc17b174e)
// Deleting Order(id=3a55ffab-a3f5-40a9-a2b3-681dc17b174e, amount=100)
// Exception in thread "main" java.lang.RuntimeException: Payment Failed
//   at io.github.nomisrev.TestKt.awaitSuccess(test.kt:11)

saga's People

Contributors

i-walker avatar nomisrev avatar renovate[bot] avatar rinkledink avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

saga's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Warning

Renovate failed to look up the following dependencies: Failed to look up maven package io.github.gradle-nexus:publish-plugin.

Files affected: libs.versions.toml


Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

  • Update gradle/gradle-build-action action to v2.4.2 [SECURITY]
  • Update all dependencies (actions/checkout, actions/setup-java, actions/upload-artifact, gradle, io.gitlab.arturbosch.detekt, org.jetbrains.kotlinx.kover, org.jetbrains.kotlinx.binary-compatibility-validator, com.gradle.enterprise, org.jetbrains.kotlin.multiplatform, io.kotest.multiplatform, io.kotest:kotest-runner-junit5, io.kotest:kotest-property, io.kotest:kotest-framework-engine, io.kotest:kotest-assertions-core, org.jetbrains.dokka, org.jetbrains.dokka:dokka-core, org.jetbrains.kotlinx:kotlinx-coroutines-test, org.jetbrains.kotlinx:kotlinx-coroutines-core, io.arrow-kt:arrow-fx-coroutines, io.arrow-kt:arrow-optics, io.arrow-kt:arrow-core)
  • Update all dependencies (major) (actions/checkout, actions/setup-java, actions/upload-artifact, gradle)
  • Click on this checkbox to rebase all open PRs at once

Detected dependencies

github-actions
.github/workflows/generate-alpha-tag.yml
  • actions/checkout v3
  • actions/setup-java v3.9.0
  • actions/checkout v3
  • actions/setup-java v3.9.0
.github/workflows/generate-tag.yml
  • actions/checkout v3
  • actions/setup-java v3.9.0
  • actions/checkout v3
  • actions/setup-java v3.9.0
.github/workflows/pr.yml
  • actions/checkout v3.2.0
  • maxim-lobanov/setup-xcode v1
  • actions/setup-java v3.9.0
  • gradle/gradle-build-action v2.3.3
  • actions/upload-artifact v3.1.1
.github/workflows/publish.yml
  • actions/checkout v3.2.0
  • actions/setup-java v3.9.0
  • maxim-lobanov/setup-xcode v1
  • gradle/gradle-build-action v2.3.3
  • actions/upload-artifact v3.1.1
  • actions/checkout v3
  • actions/setup-java v3.9.0
.github/workflows/update-docs.yml
  • actions/checkout v3
  • actions/setup-java v3.9.0
  • gradle/gradle-build-action v2.3.3
gradle
gradle.properties
libs.versions.toml
  • io.arrow-kt:arrow-core 1.1.3
  • io.arrow-kt:arrow-optics 1.1.3
  • io.arrow-kt:arrow-fx-coroutines 1.1.3
  • org.jetbrains.kotlinx:kotlinx-coroutines-core 1.6.4
  • org.jetbrains.kotlinx:kotlinx-coroutines-test 1.6.4
  • org.jetbrains.dokka:dokka-core 1.7.20
  • io.kotest:kotest-assertions-core 5.5.4
  • io.kotest:kotest-framework-engine 5.5.4
  • io.kotest:kotest-property 5.5.4
  • io.kotest:kotest-runner-junit5 5.5.4
  • io.github.gradle-nexus:publish-plugin 1.1.0
  • io.arrow-kt.arrow-gradle-config-formatter 0.11.0
  • io.arrow-kt.arrow-gradle-config-kotlin 0.11.0
  • io.arrow-kt.arrow-gradle-config-nexus 0.11.0
  • io.arrow-kt.arrow-gradle-config-publish 0.11.0
  • io.arrow-kt.arrow-gradle-config-versioning 0.11.0
  • org.jetbrains.dokka 1.7.20
  • org.jetbrains.kotlinx.kover 0.6.1
  • io.kotest.multiplatform 5.5.4
  • org.jetbrains.kotlin.multiplatform 1.7.22
  • org.jetbrains.kotlinx.binary-compatibility-validator 0.12.1
  • io.gitlab.arturbosch.detekt 1.22.0
settings.gradle.kts
  • com.gradle.enterprise 3.12
build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 7.6

  • Check this box to trigger a request for Renovate to run again on this repository

Extending saga

Hi!

Sometimes we need to attach additional steps to a Saga after we leave SagaEffect scope. For example we need something like this:

val ctxSaga = saga {
    // Do action one
}

// Some callstacks later...

with (ctxSaga) {
    // Do action two, but if it fails - please rollback action one
}

Am I missing some trick to do this or it just not implemented yet? And if so โ€“ would you consider PR for this?

Thanks for sharing this library!

Higher order sagas

Thanks for a cool library, but we often get indentation hell

saga {
    saga({
       CoroutineScope(Dispatchers.IO).run {
            retry(5) {
                async {
                    doSomething()
                }
            }
        }
    }) {
        // same levels of nesting on rollback
    }
}

It will be good to have an ability to somehow wrap saga for:

  • coroutine scopes and async calls
  • functions like retry

What do you think?

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.