SlideShare a Scribd company logo
1 of 61
Download to read offline
Flux Architecture
Jakub Kocikowski
About me
● Software engineer for over 5 years
● Fan of simple solutions to complex
problems
● I like bridges
Agenda
1. Theory Part 1: Flux and Redux
2. Theory Part 2: Context
3. Practical Considerations
1/3
Theory - Flux and Redux
MVC
The problem with MVC
Given multiple interconnected components
When an event arrives at the system
Then it causes cascading updates
The problem with MVC
On the sufficiently large code base:
1. It is hard to reason about data flow
2. New modules increase complexity exponentially
3. The code base is fragile
The solution
(state, action) => newState
Flux Architecture
View
● Listens to changes in the stores
● Dispatches actions to dispatcher
Store
● Encapsulating a whole domain
● Contains state
● Contains logic to update the state based on actions
● Notifies views about data changes
Dispatcher
● Single dispatcher per application
● Enforces one-directional synchronous data flow
● Receives actions and dispatches them to stores
● Allows you to specify dependencies between the stores
Action
● A simple object
● Has a type and some data
Action
● A simple object
● Has a type and some data
Action creators
● Factory for the action
● Makes sure the action is
formatted correctly
Flux Architecture
LET’S
TALK
ABOUT
RE-DUCKS
Redux
Flux vs Redux
Multiple stores
Dispatcher
Store updates state
Can mutate the data
Change events
One store
No Dispatcher
Reducer updates state
Immutable
Always rerender
The Redux way
Reducers
● Pure function
● Takes current state and an action
● Returns a new state
dispatch()
● A simple function
● What it does:
1. Calls reducer with current state and action
2. Replaces current state
3. Notifies listeners
Redux
Selectors
● Pure function
● Takes the state as an argument
● Returns computed value
Middleware
Redux
benefits
● Time travel
● Snapshots
● Easy hydration
● Shallow comparison
2/3
Theory II - Context
CQRS
Command Query Responsibility
Segregation [...] is the notion that
you can use a different model to
update information than the model
you use to read information
- Martin Folwer
Image: https://www.martinfowler.com/bliki/CQRS.html
Flux Architecture
Event Sourcing
elm
Elm
● Functional language for frontend
● All the side effects are managed by the runtime
● Strict, Structural typing
● Compiles to JavaScript
Reactive
programming
Reactive programming
● Declarative paradigm were time is a first-class citizen
● Two important types
○ signals - continuous function over time
○ streams - collection of discrete values over time
● We build the pipes through which we start pumping events
● Great to handle async and concurrency
Source: https://github.com/Reactive-Extensions/RxJS
How does it relate to Flux?
● You can implement it using a stream
● You can use a middleware to bridge RxJS and Redux
3/3
Practical considerations
THE DOWNSIDES
The downsides
1. Boilerplate and indirection
2. Requires immutability
3. Requires all important state to live in one place
4. Stores only serializable data
5. Performance
6. Potentially hard to learn
7. Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Are there any alternatives? A word about Mobx
● more automation, less boilerplate
● less explicit
● no time travel
● simpler
● mutable
● observable
● multiple stores
● more OOP
What about setState?
● Not an antipattern
● Probably you should start from that
● If your state maps well to component structure
● Maybe Context
So when to use Flux?
● Dependencies between data and complex update logic
● Distant leaf components need access to state
● Your data and it’s lifecycle matches event-based architecture
● You want to use benefits like easy hydration
I have this data I need to store
somewhere…
The rule of thumb is:
do whatever is less awkward.
Dan Abramov
I have this data I need to store somewhere…
● What is the convention in your team?
● Do I want to use specific Redux features?
Like cache the data between reloads in localStorage?
I have this data I need to store somewhere…
1. Why is it changing?
2. Is it causing other values to change?
I have this data I need to store somewhere…
● What components need to know about the data?
Sources / Learn more
Flux and Redux
● Facebook Flux documentation https://facebook.github.io/flux/
● Redux documentation https://redux.js.org/
● From MVC to Flux https://www.youtube.com/watch?v=p8tqhf5qKOI
● Differences between Flux and Redux https://medium.com/edge-coders/the-difference-between-flux-and-redux-71d31b118c1
● Interview with the creator of React https://devchat.tv/js-jabber/179-jsj-redux-and-react-with-dan-abramov/
● Type Of Web on Redux, Flux, CQRS, rules of thumb https://typeofweb.com/2018/03/29/flux-i-redux/
● When to use local state and when to use Redux https://typeofweb.com/2018/05/16/kiedy-uzywac-state-a-kiedy-redux/
● Redux vs Mobx https://www.youtube.com/watch?v=76FRrbY18Bs
● You may not need Redux https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367
● Downsides of Redux https://github.com/reduxjs/redux/issues/1385#issuecomment-182632703
● How to choose between Redux's store and React's state? - rule of thumb form Dan Abramov https://github.com/reduxjs/redux/issues/1287
● Comparison of some implementations of Flux https://github.com/voronianski/flux-comparison#ready
Sources / Learn more
Elm
● Elm documentation https://guide.elm-lang.org/architecture/
● Podcast on Elm https://softwareengineeringdaily.com/2015/11/03/elm-with-richard-feldman-and-srinivas-rao/
● Tutorial for sign up form in Elm https://blog.noredink.com/post/129641182738/building-a-live-validated-signup-form-in-elm
● Motivation for creating Elm https://venturebeat.com/2013/07/26/why-i-designed-a-front-end-programming-language-from-scratch/
● Talk on Elm https://www.youtube.com/watch?v=EDp6UmaA9CM
Others
● Reactive Javascript https://softwareengineeringdaily.com/2016/10/25/reactive-javascript-with-ben-lesh/
● ReactiveX Documentation http://reactivex.io/
● Reactive Programming at Netflix https://www.youtube.com/watch?v=AslncyG8whg
● CQRS https://www.martinfowler.com/bliki/CQRS.html
● Event Sourcing https://martinfowler.com/eaaDev/EventSourcing.html
Images used
1. CQRS diagram - https://www.martinfowler.com/bliki/CQRS.html
2. Duck gifs - https://giphy.com
3. Social media icons - https://flaticon.com
4. Martin Fowler photo https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)#/media/File:Webysther_20150414193208_-_Martin_Fowler.jpg
5. Dan Abramov photo https://cdn-images-1.medium.com/max/1200/1*Vko_9kRNbjQGCqyBM9OnVw.jpeg
6. Diagram of MVC gone wrong from slide 8 - https://youtu.be/nYkdrAPrdcw
7. Rest of the diagrams are by me, created in http://draw.io
Thank you
Jakub Kocikowski
Software Engineer
Contact me:
@kocikowski

More Related Content

Similar to Flux architecture and Redux - theory, context and practice

FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and PatternsFRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and PatternsLeapfrog Technology Inc.
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - TalkMatthias Noback
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Ramith Jayasinghe
 
Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"LogeekNightUkraine
 
Robust web apps with React.js
Robust web apps with React.jsRobust web apps with React.js
Robust web apps with React.jsMax Klymyshyn
 
Lagom : Reactive microservice framework
Lagom : Reactive microservice frameworkLagom : Reactive microservice framework
Lagom : Reactive microservice frameworkFabrice Sznajderman
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenchesJohannes Rudolph
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraLINAGORA
 
Daniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDaniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDevelcz
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsPiyush Katariya
 
Building Reactive webapp with React/Flux
Building Reactive webapp with React/FluxBuilding Reactive webapp with React/Flux
Building Reactive webapp with React/FluxKeuller Magalhães
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesJumping Bean
 
Bots on guard of sdlc
Bots on guard of sdlcBots on guard of sdlc
Bots on guard of sdlcAlexey Tokar
 
Tips and tricks for developing streaming and table connectors - Eron Wright,...
Tips and tricks for developing streaming and table connectors  - Eron Wright,...Tips and tricks for developing streaming and table connectors  - Eron Wright,...
Tips and tricks for developing streaming and table connectors - Eron Wright,...Flink Forward
 
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...Flink Forward
 
Flink Connector Development Tips & Tricks
Flink Connector Development Tips & TricksFlink Connector Development Tips & Tricks
Flink Connector Development Tips & TricksEron Wright
 

Similar to Flux architecture and Redux - theory, context and practice (20)

FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and PatternsFRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"
 
Robust web apps with React.js
Robust web apps with React.jsRobust web apps with React.js
Robust web apps with React.js
 
Lagom : Reactive microservice framework
Lagom : Reactive microservice frameworkLagom : Reactive microservice framework
Lagom : Reactive microservice framework
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenches
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
 
Daniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDaniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého Schizma
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
 
Building Reactive webapp with React/Flux
Building Reactive webapp with React/FluxBuilding Reactive webapp with React/Flux
Building Reactive webapp with React/Flux
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User Interfaces
 
Bots on guard of sdlc
Bots on guard of sdlcBots on guard of sdlc
Bots on guard of sdlc
 
Tips and tricks for developing streaming and table connectors - Eron Wright,...
Tips and tricks for developing streaming and table connectors  - Eron Wright,...Tips and tricks for developing streaming and table connectors  - Eron Wright,...
Tips and tricks for developing streaming and table connectors - Eron Wright,...
 
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
 
Flink Connector Development Tips & Tricks
Flink Connector Development Tips & TricksFlink Connector Development Tips & Tricks
Flink Connector Development Tips & Tricks
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
 

Recently uploaded

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 

Recently uploaded (20)

MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 

Flux architecture and Redux - theory, context and practice

  • 2. About me ● Software engineer for over 5 years ● Fan of simple solutions to complex problems ● I like bridges
  • 3.
  • 4.
  • 5. Agenda 1. Theory Part 1: Flux and Redux 2. Theory Part 2: Context 3. Practical Considerations
  • 6. 1/3 Theory - Flux and Redux
  • 7. MVC
  • 8.
  • 9. The problem with MVC Given multiple interconnected components When an event arrives at the system Then it causes cascading updates
  • 10. The problem with MVC On the sufficiently large code base: 1. It is hard to reason about data flow 2. New modules increase complexity exponentially 3. The code base is fragile
  • 13. View ● Listens to changes in the stores ● Dispatches actions to dispatcher
  • 14. Store ● Encapsulating a whole domain ● Contains state ● Contains logic to update the state based on actions ● Notifies views about data changes
  • 15. Dispatcher ● Single dispatcher per application ● Enforces one-directional synchronous data flow ● Receives actions and dispatches them to stores ● Allows you to specify dependencies between the stores
  • 16. Action ● A simple object ● Has a type and some data
  • 17. Action ● A simple object ● Has a type and some data
  • 18. Action creators ● Factory for the action ● Makes sure the action is formatted correctly
  • 21. Redux
  • 22. Flux vs Redux Multiple stores Dispatcher Store updates state Can mutate the data Change events One store No Dispatcher Reducer updates state Immutable Always rerender
  • 24. Reducers ● Pure function ● Takes current state and an action ● Returns a new state
  • 25. dispatch() ● A simple function ● What it does: 1. Calls reducer with current state and action 2. Replaces current state 3. Notifies listeners
  • 26. Redux
  • 27. Selectors ● Pure function ● Takes the state as an argument ● Returns computed value
  • 29. Redux benefits ● Time travel ● Snapshots ● Easy hydration ● Shallow comparison
  • 30. 2/3 Theory II - Context
  • 31. CQRS Command Query Responsibility Segregation [...] is the notion that you can use a different model to update information than the model you use to read information - Martin Folwer Image: https://www.martinfowler.com/bliki/CQRS.html
  • 34. elm
  • 35. Elm ● Functional language for frontend ● All the side effects are managed by the runtime ● Strict, Structural typing ● Compiles to JavaScript
  • 36.
  • 38. Reactive programming ● Declarative paradigm were time is a first-class citizen ● Two important types ○ signals - continuous function over time ○ streams - collection of discrete values over time ● We build the pipes through which we start pumping events ● Great to handle async and concurrency
  • 39.
  • 41. How does it relate to Flux? ● You can implement it using a stream ● You can use a middleware to bridge RxJS and Redux
  • 44. The downsides 1. Boilerplate and indirection 2. Requires immutability 3. Requires all important state to live in one place 4. Stores only serializable data 5. Performance 6. Potentially hard to learn 7. Nested state may cause independent views to rerender when the state changes
  • 45. Nested state may cause independent views to rerender when the state changes
  • 46. Nested state may cause independent views to rerender when the state changes
  • 47. Nested state may cause independent views to rerender when the state changes
  • 48. Nested state may cause independent views to rerender when the state changes
  • 49. Are there any alternatives? A word about Mobx ● more automation, less boilerplate ● less explicit ● no time travel ● simpler ● mutable ● observable ● multiple stores ● more OOP
  • 50. What about setState? ● Not an antipattern ● Probably you should start from that ● If your state maps well to component structure ● Maybe Context
  • 51. So when to use Flux? ● Dependencies between data and complex update logic ● Distant leaf components need access to state ● Your data and it’s lifecycle matches event-based architecture ● You want to use benefits like easy hydration
  • 52. I have this data I need to store somewhere…
  • 53. The rule of thumb is: do whatever is less awkward. Dan Abramov
  • 54. I have this data I need to store somewhere… ● What is the convention in your team? ● Do I want to use specific Redux features? Like cache the data between reloads in localStorage?
  • 55. I have this data I need to store somewhere… 1. Why is it changing? 2. Is it causing other values to change?
  • 56. I have this data I need to store somewhere… ● What components need to know about the data?
  • 57.
  • 58. Sources / Learn more Flux and Redux ● Facebook Flux documentation https://facebook.github.io/flux/ ● Redux documentation https://redux.js.org/ ● From MVC to Flux https://www.youtube.com/watch?v=p8tqhf5qKOI ● Differences between Flux and Redux https://medium.com/edge-coders/the-difference-between-flux-and-redux-71d31b118c1 ● Interview with the creator of React https://devchat.tv/js-jabber/179-jsj-redux-and-react-with-dan-abramov/ ● Type Of Web on Redux, Flux, CQRS, rules of thumb https://typeofweb.com/2018/03/29/flux-i-redux/ ● When to use local state and when to use Redux https://typeofweb.com/2018/05/16/kiedy-uzywac-state-a-kiedy-redux/ ● Redux vs Mobx https://www.youtube.com/watch?v=76FRrbY18Bs ● You may not need Redux https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367 ● Downsides of Redux https://github.com/reduxjs/redux/issues/1385#issuecomment-182632703 ● How to choose between Redux's store and React's state? - rule of thumb form Dan Abramov https://github.com/reduxjs/redux/issues/1287 ● Comparison of some implementations of Flux https://github.com/voronianski/flux-comparison#ready
  • 59. Sources / Learn more Elm ● Elm documentation https://guide.elm-lang.org/architecture/ ● Podcast on Elm https://softwareengineeringdaily.com/2015/11/03/elm-with-richard-feldman-and-srinivas-rao/ ● Tutorial for sign up form in Elm https://blog.noredink.com/post/129641182738/building-a-live-validated-signup-form-in-elm ● Motivation for creating Elm https://venturebeat.com/2013/07/26/why-i-designed-a-front-end-programming-language-from-scratch/ ● Talk on Elm https://www.youtube.com/watch?v=EDp6UmaA9CM Others ● Reactive Javascript https://softwareengineeringdaily.com/2016/10/25/reactive-javascript-with-ben-lesh/ ● ReactiveX Documentation http://reactivex.io/ ● Reactive Programming at Netflix https://www.youtube.com/watch?v=AslncyG8whg ● CQRS https://www.martinfowler.com/bliki/CQRS.html ● Event Sourcing https://martinfowler.com/eaaDev/EventSourcing.html
  • 60. Images used 1. CQRS diagram - https://www.martinfowler.com/bliki/CQRS.html 2. Duck gifs - https://giphy.com 3. Social media icons - https://flaticon.com 4. Martin Fowler photo https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)#/media/File:Webysther_20150414193208_-_Martin_Fowler.jpg 5. Dan Abramov photo https://cdn-images-1.medium.com/max/1200/1*Vko_9kRNbjQGCqyBM9OnVw.jpeg 6. Diagram of MVC gone wrong from slide 8 - https://youtu.be/nYkdrAPrdcw 7. Rest of the diagrams are by me, created in http://draw.io
  • 61. Thank you Jakub Kocikowski Software Engineer Contact me: @kocikowski