You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* remove .idea
* Java sagas: use ctx.run
* Java sagas cleanup text
* Update readme
* spotless formatting
* Simplify sagas
* Simplify sagas
* Simplify sagas
* Simplify kotlin sagas
* Simplify kotlin sagas
* Format kotlin code snippets
* Simplify TS saga example
* Format TS patterns
* Add other guides
* Simplify python sagas
* Simplify go saga
* Cron job example (#289)
* Cron job example
* Cron job example
* Add cron example
* Improve readme
* Add to main readme
* Fix determinism of time calculation
* Improve readme
* Add Java cron example
* Add readme and comments to Java cron example
* Update readmes
* Restructure Java cron example to single class and rename to CronJobInitiator and CronJob
* Update cron example comments
* Clean up Java code
* Clean up TS + Java cron example code
* Move main class and adapt readme
* Fix serde TS cron
* Add guide note
* Feedback
* Adapt flights to flight
* Adapt flights to flight
* Feedback and formatting
| <aid="sync-to-async">Convert Sync Tasks to Async</a> |[<imgsrc="https://skillicons.dev/icons?i=ts"width="24"height="24">](typescript/patterns-use-cases/README.md#convert-sync-tasks-to-async)[<imgsrc="https://skillicons.dev/icons?i=go"width="24"height="24">](go/patterns-use-cases/README.md#convert-sync-tasks-to-async)[<imgsrc="https://skillicons.dev/icons?i=python&theme=light"width="24"height="24">](python/patterns-use-cases/README.md#convert-sync-tasks-to-async)[<imgsrc="https://skillicons.dev/icons?i=java&theme=light"width="24"height="24">](java/patterns-use-cases/README.md#convert-sync-tasks-to-async)|
@@ -73,7 +74,7 @@ Or have a look at the general catalog below:
73
74
| <aid="promise-as-a-service">Durable Promises as a Service</a> |[<imgsrc="https://skillicons.dev/icons?i=ts"width="24"height="24">](typescript/patterns-use-cases/README.md#durable-promises-as-a-service)|
An example of a trip reservation workflow, using the saga pattern to undo previous steps in case of an error.
120
+
When building distributed systems, it is crucial to ensure that the system remains consistent even in the presence of failures.
121
+
One way to achieve this is by using the Saga pattern.
120
122
121
-
Durable Execution's guarantee to run code to the end in the presence of failures, and to deterministically recover previous steps from the journal, makes sagas easy.
122
-
Every step pushes a compensation action (an undo operation) to a stack. In the case of an error, those operations are run.
123
+
Sagas are a way to manage transactions that span multiple services.
124
+
They allow you to run compensations when your code crashes halfway through.
125
+
This way, you can ensure that your system remains consistent even in the presence of failures.
123
126
124
-
The main requirement is that steps are implemented as journaled operations, like `restate.Run()` or RPC/messaging.
127
+
Restate guarantees that sagas run to completion. It will handle retries and failures, and ensure that compensations are executed successfully.
125
128
126
-
The example shows two ways you can implement the compensation, depending on the characteristics of the API/system you interact with.
127
-
1.**Two-phase commit**: The reservation is created and then confirmed or cancelled. The compensation executes 'cancel' and is added after the reservation is created.
128
-
2.**Idempotency key**: The payment is made in one shot and supplies an ID. The compensation is added before the payment is made and uses the same ID.
Have a look at the logs to see how the compensations run in case of a terminal error.
140
152
141
153
Start the workflow:
142
154
```shell
143
-
curl -X POST localhost:8080/BookingWorkflow/trip123/Run -H 'content-type: application/json' -d '{
144
-
"flights": {
145
-
"flight_id": "12345",
146
-
"passenger_name": "John Doe"
155
+
curl localhost:8080/BookingWorkflow/Run --json '{
156
+
"customerId": "12345",
157
+
"flight": {
158
+
"flightId": "12345",
159
+
"passengerName": "John Doe"
147
160
},
148
161
"car": {
149
-
"pickup_location": "Airport",
150
-
"rental_date": "2024-12-16"
162
+
"pickupLocation": "Airport",
163
+
"rentalDate": "2024-12-16"
151
164
},
152
-
"payment_info": {
153
-
"card_number": "4111111111111111",
154
-
"amount": 1500
165
+
"hotel": {
166
+
"arrivalDate": "2024-12-16",
167
+
"departureDate": "2024-12-20"
155
168
}
156
169
}'
157
170
```
158
171
172
+
159
173
Have a look at the logs to see the cancellations of the flight and car booking in case of a terminal error.
160
174
161
175
<details>
162
176
<summary>View logs</summary>
163
177
164
178
```
165
-
2025/01/06 16:16:02 INFO Handling invocation method=BookingWorkflow/Run invocationID=inv_17l9ZLwBY3bz6HEIybYB6Rh9SbV6khuc0N
166
-
2025/01/06 16:16:02 INFO Handling invocation method=Flights/Reserve invocationID=inv_1kNkgfEJjWp67I8WxNRHZN79XZprWqPWp3
167
-
2025/01/06 16:16:02 INFO Flight reserved: 8685229b-c219-466f-9a70-9f54b968a1b9
168
-
2025/01/06 16:16:02 INFO Invocation completed successfully method=Flights/Reserve invocationID=inv_1kNkgfEJjWp67I8WxNRHZN79XZprWqPWp3
169
-
2025/01/06 16:16:02 INFO Handling invocation method=CarRentals/Reserve invocationID=inv_1cXn5IBHJhEK7ihQnoXIX8rVLvWWAi27EB
170
-
2025/01/06 16:16:02 INFO Car reserved:2b5be5c4-944c-48a4-abb3-e0e0039151e9
171
-
2025/01/06 16:16:02 INFO Invocation completed successfully method=CarRentals/Reserve invocationID=inv_1cXn5IBHJhEK7ihQnoXIX8rVLvWWAi27EB
172
-
2025/01/06 16:16:02 ERROR This payment should never be accepted! Aborting booking.
173
-
2025/01/06 16:16:02 INFO Handling invocation method=Flights/Cancel invocationID=inv_1d4KgHFg2EFF62ccgILiNAgPwKx4tmskyl
174
-
2025/01/06 16:16:02 INFO Flight cancelled: 8685229b-c219-466f-9a70-9f54b968a1b9
175
-
2025/01/06 16:16:02 INFO Invocation completed successfully method=Flights/Cancel invocationID=inv_1d4KgHFg2EFF62ccgILiNAgPwKx4tmskyl
176
-
2025/01/06 16:16:02 INFO Handling invocation method=CarRentals/Cancel invocationID=inv_15QXMdt8GLYU18PoNhVICXbqRg0x9lQsIp
177
-
2025/01/06 16:16:02 INFO Car cancelled2b5be5c4-944c-48a4-abb3-e0e0039151e9
178
-
2025/01/06 16:16:02 INFO Invocation completed successfully method=CarRentals/Cancel invocationID=inv_15QXMdt8GLYU18PoNhVICXbqRg0x9lQsIp
179
-
2025/01/06 16:16:02 INFO Refunded payment: e4eac4a9-47c9-4087-9502-cb0fff1218c6
180
-
2025/01/06 16:16:02 INFO Invocation completed successfully method=BookingWorkflow/Run invocationID=inv_17l9ZLwBY3bz6HEIybYB6Rh9SbV6khuc0N
179
+
2025/05/29 21:23:19 INFO Handling invocation method=BookingWorkflow/Run invocationID=inv_1jDSzGn5OKSm41ndgD8QcKpfPtPUSEgEM1
180
+
2025/05/29 21:23:19 INFO Flight reserved for customer: 12345
181
+
2025/05/29 21:23:19 INFO Car reserved for customer: 12345
182
+
2025/05/29 21:23:19 ERROR [👻 SIMULATED] This hotel is fully booked!
183
+
2025/05/29 21:23:19 INFO Flight cancelled for customer:12345
184
+
2025/05/29 21:23:19 INFO Car cancelled for customer:12345
185
+
2025/05/29 21:23:19 INFO Hotel cancelled for customer:12345
186
+
2025/05/29 21:23:19 ERROR Invocation returned a terminal failure method=BookingWorkflow/Run invocationID=inv_1jDSzGn5OKSm41ndgD8QcKpfPtPUSEgEM1 err="[500] [👻 SIMULATED] This hotel is fully booked!"
0 commit comments