Upgrade to Pro — share decks privately, control downloads, hide ads and more …

State Machines and How they Shape Mobile Architectures

Rygel Louv
November 20, 2022

State Machines and How they Shape Mobile Architectures

At Sendwave, we wanted to rebuild our data syncing mechanism on Android. That's when we came up with the idea of using a State Machine which helped the team build a much more solid and scalable data syncing system. State Machines are a very good solution for making sense of things that can be quite complex. So many companies are using State Machines as part of their architecture (like Tinder, GoDaddy, Square and more). Some popular architecture patterns like MVI or Redux were inspired by the concept of State Machines. And it has even made some language features possible (Kotlin Coroutines). We will start by understanding State Machines from the ground up and then we will go through all the examples above and will finish by showing how we used a State Machine at Sendwave

Rygel Louv

November 20, 2022
Tweet

More Decks by Rygel Louv

Other Decks in Programming

Transcript

  1. Agenda 1 2 3 4 Data Sync Problem State Machines

    What is it? Expect some maths Real World Applications of State Machines Solving the Data sync problem 3
  2. State Machines A fi nite-state machine (FSM) or fi nite-state

    automaton (FSA, plural: automata), fi nite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a fi nite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition - Wikipedia
  3. Data sync problem Sync some user information Sync countries con

    fi guration data Sync user recipients Sync remote con fi g Etc
  4. Data sync problem: constraints Is it a new session? Is

    everything synced already? What if there is a failure? Should we allow the user to interact? What if the user logs out while syncing? What if we have more constraints to add to this?
  5. Architecture pattern and feature complexity Sometimes MV* is not enough

    Some features are more complex than others Some feature have no UI Third party integration, global app component, internal reusable library, a feature with multiple states/statuses, 
 or shared data across screens Is MV* always how we should approach problems? Leverage a speci fi c technique or design pattern to solve a problem
  6. A State Machine as solution to some problems State Machines

    could be a solution to reduce code complexity Make sense of things that could be complicated to understand Self-document or visualize a speci fi c fl ow
  7. State Machines: Theory FSM: our Finite State Machine Q: fi

    nite set of States Σ: fi nite set of Inputs δ: the transition function q0: initial state F: set of fi nal states
  8. State Machines: Theory q0 is element of Q F is

    a subset or equal to Q δ: the transition function is a function that takes
 a state within Q and an Input within Σ to produce 
 a new state which should be within F The rules: q0 ∈ Q F ⊆ Q
  9. Recognizing a State Machine pattern Adding states or status to

    your program Growing complexity Having many functions in the form of isSomething Many booleans in the program could also be an indicator All of these toghether would de fi nitely be the perfect indicator
  10. Square Work fl ow https://github.com/square/work fl ow A library that

    supports writing business logic and complex UI navigation logic as state machines, thereby enabling con fi dent reasoning about state and validation of correctness.
  11. Kotlin Coroutines: Suspending State Machines Kotlin Coroutines let you create

    asynchronous code that looks like synchronous This is possible thanks to a technique called “Continuation Passing Style” Suspend functions generate a State Machine to enable the Continuation object to resume the execution
  12. Reminder: Data sync problem Sync some user information Sync countries

    con fi guration data Sync user recipients Sync remote con fi g Etc Is it a new session? Is everything synced already? What if there is a failure? Should we allow the user to interact? What if the user logs out while syncing? What if we have more constraints to add to this?
  13. What have we learned about State Machines? State Machines could

    be a solution to reduce code complexity Make sense of things that could be complicated to understand Self-document or visualize a speci fi c fl ow Not that dif fi cult to build and integrate in your app Improve testability Improve scalability Improve debuggability/maintainability Go beyond the MV* architectural pattern and give it a different shape to better solve problems.
  14. What have we NOT learned about State Machines? Not a

    solution for all your problems Not a solution to solve all types of complexity Not necessarily a replacement to your state management approach Not a dark tool/technique only for “experts”
  15. @Rygellouv @rygelouv @rygel https://sp4ghetticode.medium.com/ fi nite-state-machines-in-kotlin-part-1-57e68d54d93b https://medium.com/tinder/scaling-out-tinder-android-payment- fl ow-using-state-machine-e14ef0591b6 https://medium.com/swlh/mvi-architecture-with-android-fcde123e3c4a

    https://www.droidcon.com/2018/06/27/the-rise-of-the-state-machines/ https://www.droidcon.com/2019/08/07/square-work fl ow-android-and-ios-apps-composed-of-state-machines-de fi ning-ui-and-navigation/ Resources https://medium.com/google-developer-experts/coroutines-suspending-state-machines-36b189f8aa60 https://labs.pedrofelix.org/guides/kotlin/coroutines/coroutines-and-state-machines