1
00:00:00,120 --> 00:00:06,300
In this video, we are going to do Hands on up, suck up a ten four, orchestrate distributed transactions

2
00:00:06,300 --> 00:00:08,280
using a step functions.

3
00:00:08,520 --> 00:00:15,390
So we are going to design a step functions state machine for implementing place order use cases.

4
00:00:15,420 --> 00:00:16,980
You can see the image on the slide.

5
00:00:17,010 --> 00:00:19,020
This is the place, the use cases.

6
00:00:19,200 --> 00:00:26,190
So we will create an a step function state machine for implementing order fulfillment process with creating

7
00:00:26,190 --> 00:00:34,470
these steps will be placed order after that update inventory, perform payment and if everything goes

8
00:00:34,470 --> 00:00:36,360
well, we can succeed to order.

9
00:00:36,390 --> 00:00:42,390
If something goes wrong in any step, we will revert back to payment inventory, remove order and go

10
00:00:42,390 --> 00:00:43,470
to face statement.

11
00:00:43,470 --> 00:00:46,470
For that purpose, please open the pseudocode code.

12
00:00:46,470 --> 00:00:50,400
I have created separate lecture and section folder in the command text.

13
00:00:50,400 --> 00:00:54,150
I have designed our architecture which will be state machine.

14
00:00:54,150 --> 00:00:59,520
You can see that we are starting here and there is a task which is the place order accommodate the incoming

15
00:00:59,520 --> 00:01:06,000
place order request and after that we will have a choice that explaining the is place order and we have

16
00:01:06,000 --> 00:01:07,050
yes or no.

17
00:01:07,080 --> 00:01:10,890
If yes we have separate task which is the update inventory.

18
00:01:10,920 --> 00:01:14,970
If inventory updated, we can update the make the payment.

19
00:01:14,970 --> 00:01:19,710
If payment successful, we can succeed to order with incoming request.

20
00:01:19,710 --> 00:01:21,150
So this is the pivot.

21
00:01:21,150 --> 00:01:28,980
But in every step we have, if an yes and no statement, if we can't place an order, we should go to

22
00:01:28,980 --> 00:01:30,540
the paid order statement.

23
00:01:30,540 --> 00:01:36,690
If we cannot update the inventory, we can revert back to inventory and remove the order.

24
00:01:36,690 --> 00:01:42,690
If we cannot make the payment, we can revert back the payment, revert inventory, remove order and

25
00:01:42,700 --> 00:01:43,890
fail order status.

26
00:01:43,890 --> 00:01:48,330
So now I am going to open a console and design our workflow.

27
00:01:48,360 --> 00:01:53,250
Go to step functions and state machines and click the create state machine.

28
00:01:53,250 --> 00:01:56,190
We will create standard state machines.

29
00:01:56,190 --> 00:02:01,500
So for that purpose, I'm going to select the standard and click the next button.

30
00:02:01,500 --> 00:02:07,560
Here you can see that we can design our workflow with workflow to so now we will implement our logic.

31
00:02:07,560 --> 00:02:11,250
So basically I will start with adding the lambda.

32
00:02:11,250 --> 00:02:14,640
So this is the task that we can place order.

33
00:02:14,640 --> 00:02:18,210
So that's why I'm going to say place order.

34
00:02:18,780 --> 00:02:24,360
By this way, if you open the architecture, you can see that every lambda function is a microservices.

35
00:02:24,360 --> 00:02:31,680
So we will perform distributed transaction across multiple microservices, which are the order inventory

36
00:02:31,680 --> 00:02:33,090
payment microservices.

37
00:02:33,090 --> 00:02:39,810
So these are the path from our order fulfillment process for the distributed transaction and implementing

38
00:02:39,810 --> 00:02:40,770
the setup pattern.

39
00:02:40,770 --> 00:02:44,940
So that's why we will select the lambda function as a task off to our every step.

40
00:02:44,940 --> 00:02:50,040
And these lambda functions can be interacted on databases and also payment can be interact with the

41
00:02:50,040 --> 00:02:53,220
third party services in order to perform payment operation.

42
00:02:53,220 --> 00:02:57,000
So if you come back to our designer, the first step is place order.

43
00:02:57,000 --> 00:02:59,910
So this will be placed order with lambda function.

44
00:02:59,910 --> 00:03:03,990
And after that we will go to follow section and add the choice.

45
00:03:03,990 --> 00:03:08,970
So this choice is confirmed with the is order placed or not?

46
00:03:10,160 --> 00:03:11,210
This will be checked.

47
00:03:11,570 --> 00:03:18,770
Response from the lambda function and according to response it is accommodate and redirect to this event

48
00:03:18,770 --> 00:03:23,420
to the update inventory or the fail of the order place operation.

49
00:03:23,510 --> 00:03:25,940
For that purpose we have defined two rules.

50
00:03:25,940 --> 00:03:32,420
So you can come here and now we can edit the rule one if you open the rule one so a the condition,

51
00:03:32,420 --> 00:03:39,290
we can get a condition from the dollar sign and that so is order placed.

52
00:03:40,110 --> 00:03:46,770
And we can choose that the actual 2 billion construct and true is order placed.

53
00:03:46,780 --> 00:03:49,810
Then we can continue our save this condition.

54
00:03:49,960 --> 00:03:50,690
Okay.

55
00:03:50,710 --> 00:03:53,110
And also, I have one more rule.

56
00:03:53,110 --> 00:03:58,980
We can add the new rule, add the condition, and we can copy from the condition from this state.

57
00:03:58,990 --> 00:04:07,000
You can see that we have checked the is order placed and if you come to rule two a the condition so

58
00:04:07,000 --> 00:04:12,970
what will should be is order placed and again is equal to and the value is the building constant.

59
00:04:12,970 --> 00:04:14,350
And you can see the force.

60
00:04:15,840 --> 00:04:20,890
Look, we have to enforce is order placed and is also placed to enforce.

61
00:04:20,910 --> 00:04:28,140
So now I am going to add the required steps which we can continue our support pattern implementation.

62
00:04:28,140 --> 00:04:34,380
So if you add the additional lambda function, that means microservices, this will be the ah event

63
00:04:34,380 --> 00:04:36,570
or database event or microservices.

64
00:04:36,570 --> 00:04:40,830
For that purpose I'm going to say update inventory.

65
00:04:42,600 --> 00:04:43,210
Okay.

66
00:04:43,230 --> 00:04:50,500
So this is going to update eventually and if the order placed equal force, so that means we got exception.

67
00:04:50,520 --> 00:04:54,980
I have the flow statement which is the fail I drag and drop the fail statement.

68
00:04:54,990 --> 00:04:56,790
This will be a failed order.

69
00:04:57,150 --> 00:04:59,720
Okay, so now I'm going to continue with the happy.

70
00:05:00,210 --> 00:05:01,320
Is order placed?

71
00:05:01,350 --> 00:05:01,940
Yes.

72
00:05:01,950 --> 00:05:04,280
Now we have updated the inventory.

73
00:05:04,290 --> 00:05:08,400
So now, again, I need to check the inventory update or not for that purpose.

74
00:05:08,530 --> 00:05:09,660
The choice statement.

75
00:05:09,660 --> 00:05:12,480
In the choice statement, we can add a rule definition.

76
00:05:12,480 --> 00:05:19,560
So in the rule definition we have at the assignment condition, if you condition we can we have a dollar

77
00:05:19,560 --> 00:05:21,390
sign and the.

78
00:05:23,380 --> 00:05:24,010
Is.

79
00:05:26,480 --> 00:05:27,770
Update inventory.

80
00:05:28,980 --> 00:05:34,770
You can copy paste and choose the operator equal to and again volume constant and to the end post.

81
00:05:35,160 --> 00:05:39,600
If the update inventory true, that means we will continue happy bet.

82
00:05:39,600 --> 00:05:47,620
And again I'm going to add a new choice and condition is the same and this time I'm going to set a given

83
00:05:47,790 --> 00:05:49,740
constant and say the false.

84
00:05:50,750 --> 00:05:53,060
So we have two conditions.

85
00:05:53,060 --> 00:05:56,900
Of course, we should put the dollar sign and that notation.

86
00:05:57,140 --> 00:06:01,570
By this way, we can reach the output data from the previous task set.

87
00:06:01,800 --> 00:06:06,590
Okay, click the save condition and now I'm going to remove the default result.

88
00:06:06,650 --> 00:06:08,100
We can clear this one.

89
00:06:08,120 --> 00:06:08,810
Yes.

90
00:06:09,020 --> 00:06:12,600
As you can see that we have to choose after the update inventory.

91
00:06:12,620 --> 00:06:18,620
Basically, we will use the happy pet which is the is updated one, two, three, four for the happy.

92
00:06:18,620 --> 00:06:24,260
But I'm going to add an additional Microsoft lambda function, which is the payment microservices and

93
00:06:24,260 --> 00:06:28,070
we can set the state name as a make payment.

94
00:06:29,940 --> 00:06:35,380
This will be power from payment operations and if there is any problem for that action.

95
00:06:35,400 --> 00:06:42,420
So in that case, if we cannot update the inventory again, I am going to add additional lambda function.

96
00:06:42,420 --> 00:06:46,470
But this time this lambda function performed the revert back to inventory.

97
00:06:47,880 --> 00:06:55,050
So that means what that means when we update the inventory, if there is a problem, that means if it

98
00:06:55,050 --> 00:06:59,280
is cannot update the inventory, we should revert back to change from the dynamo db.

99
00:06:59,310 --> 00:07:00,060
Think about that.

100
00:07:00,060 --> 00:07:06,090
These microservices is communicate with the its database dynamo DB database table and it is updating

101
00:07:06,090 --> 00:07:06,540
the status.

102
00:07:06,540 --> 00:07:09,660
But this should be revert back to the statement.

103
00:07:09,660 --> 00:07:13,440
So that's why we should additional lambda function in the choice statement.

104
00:07:13,440 --> 00:07:19,980
We should give the name as to choice statement is inventory updated?

105
00:07:20,760 --> 00:07:21,420
Okay.

106
00:07:21,810 --> 00:07:26,850
So as you can see that we have implement the inventory part now focus on the payment part.

107
00:07:26,880 --> 00:07:32,190
This is the last step in the distributed transaction, which is the order fulfillment for that purpose.

108
00:07:32,190 --> 00:07:34,200
I am going to add a choice statement again.

109
00:07:34,200 --> 00:07:37,670
This statement name is a, is payment done?

110
00:07:39,190 --> 00:07:40,570
Immense success.

111
00:07:41,460 --> 00:07:42,060
Okay.

112
00:07:42,060 --> 00:07:48,450
So according to payment success operation, we can put a rule definition upon the rule, add the condition.

113
00:07:48,540 --> 00:07:51,030
Again, I'm going to set a dollar sign.

114
00:07:51,030 --> 00:07:53,640
That is payment.

115
00:07:54,610 --> 00:07:55,630
Success.

116
00:07:55,930 --> 00:08:02,710
And copy paste this variable we can choose is equal to and value is boolean construct and give as a

117
00:08:02,710 --> 00:08:03,370
true.

118
00:08:03,400 --> 00:08:05,110
So this is the first condition.

119
00:08:05,110 --> 00:08:13,450
And the second condition is add a new choice, add condition and choose is equal to boolean constant.

120
00:08:13,450 --> 00:08:15,640
And this time we will select the false.

121
00:08:15,640 --> 00:08:18,310
And now I'm going to remove the default one.

122
00:08:19,440 --> 00:08:19,790
Okay.

123
00:08:19,800 --> 00:08:20,430
Very good.

124
00:08:20,460 --> 00:08:24,370
So as you can see that we have set the payment success operation.

125
00:08:24,390 --> 00:08:30,000
If payment success is correct and true, we basically make it success.

126
00:08:30,000 --> 00:08:34,950
So if you drag and drop the success order, we will succeed the order.

127
00:08:34,950 --> 00:08:38,390
That means order created successfully with full operations.

128
00:08:38,400 --> 00:08:45,510
But when it comes to the fail is if the payment is the fail, we should also revert back to payment

129
00:08:45,510 --> 00:08:47,040
operation for that purpose.

130
00:08:47,500 --> 00:08:49,200
Drag and drop the lambda function.

131
00:08:49,200 --> 00:08:53,730
And so this time we are going to revert back to payment.

132
00:08:53,880 --> 00:08:59,490
So you can think about that in the payment operation, we can integrate with the third party system.

133
00:08:59,490 --> 00:09:02,940
So even we perform the operation.

134
00:09:02,970 --> 00:09:09,330
If we got an exception in our business logic or in our microservices, we should again go back to our

135
00:09:09,330 --> 00:09:11,880
third party system and revert back to payment.

136
00:09:11,880 --> 00:09:18,520
It is very important and critical when it comes to business related use cases, so that's why we add

137
00:09:18,580 --> 00:09:22,140
additional lambda function, which is revert back to payment.

138
00:09:22,410 --> 00:09:23,190
Okay.

139
00:09:23,220 --> 00:09:28,500
After that, we perform the operation of the removing to order.

140
00:09:28,500 --> 00:09:33,180
You can see that we will continue to our fail statement.

141
00:09:33,180 --> 00:09:41,310
So basically before fail statement, we will one by one revert back the payment and revert back to inventory

142
00:09:41,310 --> 00:09:42,240
for that purpose.

143
00:09:42,240 --> 00:09:45,300
I'm going to add additional step in here after the revert.

144
00:09:46,300 --> 00:09:48,970
Eventually we should remove the order.

145
00:09:50,290 --> 00:09:52,420
As you know, that made me place an order.

146
00:09:52,440 --> 00:09:55,290
We are creating an order in the automotive table.

147
00:09:55,320 --> 00:09:58,680
By this way, if there is an exception, we should remove the order.

148
00:09:58,680 --> 00:10:04,800
But after the payment, actually, we should not go to the directly end statement because this will

149
00:10:04,800 --> 00:10:08,540
be also update the inventory for that purpose.

150
00:10:08,550 --> 00:10:11,190
Select this task, which is the word payment.

151
00:10:11,220 --> 00:10:17,990
If you go to the scroll down and next date, the next step should be the revert inventory.

152
00:10:18,000 --> 00:10:20,760
You can scroll down and select the revert inventory.

153
00:10:20,970 --> 00:10:21,880
Okay, very good.

154
00:10:21,900 --> 00:10:26,310
So this time we are also design our worst case.

155
00:10:26,310 --> 00:10:31,800
So if we over with our architecture, we can come to our first place.

156
00:10:31,800 --> 00:10:34,200
And if I make it zoom out.

157
00:10:34,620 --> 00:10:35,830
Okay, very good.

158
00:10:35,850 --> 00:10:41,400
So basically we are accommodate to place order in the lambda function and these will be interact with

159
00:10:41,400 --> 00:10:47,070
the Dynamo DB and insert a new order item in our database which is status of the creating.

160
00:10:47,070 --> 00:10:54,100
So after that we are looking to is order placed correct or true or false according to these value.

161
00:10:54,120 --> 00:10:59,940
If there is a force and that means the exception, we directly go to face statement and closed and to

162
00:10:59,940 --> 00:11:00,870
this workflow.

163
00:11:00,900 --> 00:11:04,560
If order placed successfully, that means create order successfully.

164
00:11:04,590 --> 00:11:07,200
We go to the update inventory part.

165
00:11:07,200 --> 00:11:11,820
We will update the inventory and deduct the item in Dynamo DV database.

166
00:11:11,820 --> 00:11:15,210
And after that we will check that is matter updated.

167
00:11:15,360 --> 00:11:22,500
If there is a problem when we updating inventory, then we will go to the full statement and go to revert

168
00:11:22,500 --> 00:11:23,460
inventory face.

169
00:11:23,490 --> 00:11:30,300
We will revert back to the our database and consistent make consistent data and after that remove order

170
00:11:30,300 --> 00:11:33,660
because in the first place we will create an order.

171
00:11:33,690 --> 00:11:37,620
In this phase we will create and remove an order and finish the workflow.

172
00:11:37,860 --> 00:11:44,280
And if the inventory update successfully, we go for the payment operation and perform the payment.

173
00:11:44,280 --> 00:11:50,130
If the payment is failed, we will revert payment and after the revert inventory, remove order.

174
00:11:50,130 --> 00:11:55,890
And these are the compensated steps in the step function that we design and implement such a pattern.

175
00:11:55,890 --> 00:12:00,180
If the payment is done, we will go to the success statement and finish the order.

176
00:12:00,180 --> 00:12:07,020
So basically we will implement and design our order or according to our architecture that we see on

177
00:12:07,020 --> 00:12:07,740
the image.

178
00:12:07,740 --> 00:12:13,140
So as you can see that I have designed the workflow with the implementing saga pattern, but we don't

179
00:12:13,140 --> 00:12:16,980
develop our order, inventory and payment microservice lambda functions.

180
00:12:16,980 --> 00:12:23,340
So please take these as an assignment that will connect to your Dynamo DV databases and perform record

181
00:12:23,340 --> 00:12:25,110
actions to complete this flow.

182
00:12:25,140 --> 00:12:28,830
For now, I am not going to proceed this flow to create state machine.

183
00:12:28,830 --> 00:12:34,470
Before that we should develop these order monetary and payment microservice lambda functions with rollback

184
00:12:34,470 --> 00:12:35,070
methods.

185
00:12:35,070 --> 00:12:41,130
So for that purpose please take an assignment and try to implement this logic before closing section.

186
00:12:41,130 --> 00:12:46,770
Make sure that you have the all resources, AWG resources on your Atlas Cloud environment.

187
00:12:46,770 --> 00:12:49,920
So as you can see that we don't create any state machine.

188
00:12:49,920 --> 00:12:53,970
So that's why I don't expect any state mission or state function.

189
00:12:53,970 --> 00:12:58,830
Please go to state function and make sure that you don't have any state machine on your environment.
