Immortal system
as a piece of cake
By Nickolay Tsyb
Javameetup
My experience
Commerce software development over 8 years
My experience
Now I’m working in HYS-Enterprise as a teamlead
My experience
My experience
My experience
My experience
Hillel coaching
Call center
Call center
Call center
Call center
Starting Waiting Talking On hold Finishing
Starting Waiting Talking On hold Finishing
Starting Waiting Talking On hold Finishing
Handling
Zero downtime deployment
Zero downtime deployment
Fault-tolerance
FINITE STATE MACHINE
Chapter 1
What is finite state machine?
Chapter 2
How to build fault tolerance system?
Chapter 3
Deep dive into spring state machine
Abstract mathematical model
or
graph with states and transitions
Finite-state machine
Examples from real life...
Conveyor manufacturing
AI in computer games
Animal behavior
Example: coin-operated turnstile
Example: coin-operated turnstile
States
1. Locked
2. Unlocked
Transitions
1. Push ( -> )
2. Push ( -> )
3. Coin ( -> )
4. Coin ( -> )
State’s handling
void coin(State state) {
...
if (state.equals(LOCKED)) {
//coin implementation
state = UNLOCKED;
}
...
}
State’s handling
void push(State state) {
...
if (state.equals(UNLOCKED)) {
//push implementation
state = LOCKED;
}
...
}
What about real project?
How can we just handle new state in project?
One of the main problems
Coupling
business logic
state’s handling
Complexity increasing
Hard-readable code
High barrier to entry for new members of
team
More time for testing
What could we do?
Decoupling
business logic state’s handling
Spring state machine
Spring state machine
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
Sitting
Eating
Sleeping
Transition’s configuration
Sitting
Eating
Sleeping
Action’s configuration
Sitting
Eating
Sleeping
meow...
Guard’s configuration
Sitting
Eating
Sleeping
meow...
Is hungry?
Support of extended state
Sitting
Eating
Sleeping
meow...
Is hungry?
Support of internal state machine
Sitting
Eating
meow...
Is hungry?
Listener
Sitting
Eating
meow...
Is hungry?
Persisting
Sitting
Eating
meow...
Is hungry?
Error handling and testing
Code example
public enum States {
SITTING,
EATING,
SLEEPING;
}
public enum Events {
SIT,
EAT,
SLEEP;
}
public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
states.withStates()
.initial(States.SITTING)
.state(States.EATING)
.end(States.SLEEPING);
}
public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
transitions
.withExternal().source(States.SITTING).target(States.EATING).event(Events.EAT).and()
.withExternal().source(States.EATING).target(States.SITTING).event(Events.SIT).and()
.withExternal().source(States.SITTING).target(States.SLEEPING).event(Events.SLEEP).and()
.withExternal().source(States.EATING).target(States.SLEEPING).event(Events.SLEEP);
}
Data storage and persisting
Data storages
States persisting
Events
States
Transitions
States persisting
Events
States
Transitions
Intercept and update
State
machine
context
State machine context
State
machine
context
Serialization
(Cryo by default)
Redis
Mongo
JPA
Add or update
State machine context
State
machine
context
Deserialization
(Cryo by default)
Redis
Mongo
JPA
Restore
Immortality
(fault-tolerance)
Master-slave
SLAVE
Requests
MASTER
MASTER
Master-slave
Requests
SLAVE
Master-slave
Master
Requests
SLAVE
MASTERM
ASTER
Master-slave
Master
Requests
MASTER
Master-slave
Requests
MASTER
Master-slave
Requests
MASTER
SLAVE
Deployment
procedure
Build&Deployment process
Push
Build&Deployment process
Push Trigger (pull&build)
Build&Deployment process
Push Trigger (pull&build)
Create and store image
Build&Deployment process
Push Trigger (pull&build)
Create and store image
Get image
Build&Deployment process
Push Trigger (pull&build)
Create and store image
Get image
Deploy
Build&Deployment process
Push Trigger (pull&build)
Create and store image
Get image
Deploy
Conclusions
Conclusions
Conclusions
Useful links
Quick start
Useful links
Documentation
Useful links
GitHub
Contacts
Thank you for
your attention!
Any Questions?

Nikolay Tsyb (HYS Enterprise) "Immortal system as a piece of cake."