Rethinking "State Management"

Rethinking "State Management"

Persisting the State is an integral part of any application, and it profoundly influences how we architect the application. But do we need to store the state in the first place? Is there any alternative?

Join me to experience the difference perspective of State Management.

C6dbbb399f7658917f60bd1e2a2663ed?s=128

Tamizhvendan S

July 10, 2016
Tweet

Transcript

  1. Rethinking State Management currentState = foldl(pastEvents, initialState) @tamizhvendan http://blog.tamizhvendan.in www.ajira.tech

  2. Rethinking State ??

  3. Rethinking State ??

  4. Rethinking State ??

  5. Rethinking State ??

  6. Rethinking State ??

  7. F# CafeApp Courtesy: http://cqrs.nu/

  8. F# CafeApp Courtesy: http://cqrs.nu/

  9. Old School Backend Data Store

  10. Old School Backend Data Store Imperative Data Modeling!

  11. { "tables" : [{"id" : 1, "status" : "available"}] }

    { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } { "tables" : [{"id" : 1, "status" : "in_service"}] }
  12. A Different Perspective

  13. t0

  14. t0

  15. t0 t1 Open Tab

  16. t0 t1 Open Tab

  17. t0 t1 Open Tab t2 Place Order

  18. t0 t1 Open Tab t2 Place Order

  19. t0 {"status" : "available"}

  20. t0 {"status" : "available"}

  21. t0 {"status" : "available"} t1 {"status" : "waiting_for_order"}

  22. t0 {"status" : "available"} t1 {"status" : "waiting_for_order"}

  23. t0 {"status" : "available"} t1 {"status" : "waiting_for_order"} t2 {"status"

    : "in_service"}
  24. t0 {"status" : "available"} t1 {"status" : "waiting_for_order"} t2 {"status"

    : "in_service"}
  25. STATE is Transient

  26. Closed Tab

  27. Closed Tab Open Tab

  28. Closed Tab Open Tab

  29. Closed Tab Opened Tab Open Tab

  30. Closed Tab Opened Tab Open Tab Place Order

  31. Closed Tab Opened Tab Open Tab Place Order

  32. Closed Tab Placed Order Opened Tab Open Tab Place Order

  33. Closed Tab Placed Order Opened Tab Open Tab Place Order

    Serve Drink
  34. Closed Tab Placed Order Opened Tab Open Tab Place Order

    Serve Drink Prepare Food
  35. Closed Tab Placed Order Opened Tab Open Tab Place Order

    Serve Drink Prepare Food
  36. Closed Tab Placed Order Opened Tab Order InProgress Open Tab

    Place Order Serve Drink Prepare Food
  37. Serve Drink Closed Tab Placed Order Opened Tab Order InProgress

    Open Tab Place Order Serve Drink Prepare Food
  38. Serve Drink Prepare Food Closed Tab Placed Order Opened Tab

    Order InProgress Open Tab Place Order Serve Drink Prepare Food
  39. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Open Tab Place Order Serve Drink Prepare Food
  40. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Open Tab Place Order Serve Drink Prepare Food
  41. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Open Tab Place Order Serve Drink Prepare Food
  42. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Served Order Open Tab Place Order Serve Drink Prepare Food
  43. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Served Order Open Tab Place Order Serve Drink Prepare Food
  44. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Served Order Open Tab Place Order Serve Drink Prepare Food Close Tab
  45. Serve Drink Prepare Food Serve Food Closed Tab Placed Order

    Opened Tab Order InProgress Served Order Open Tab Place Order Serve Drink Prepare Food Close Tab
  46. State X

  47. State X Command

  48. State X Command

  49. State X State Y Command

  50. State X State Y Command // State -> Command ->

    State let evolve state command = let newState = // ... newState
  51. State X State Y Command // State -> Command ->

    State let evolve state command = let newState = // ... newState
  52. State X State Y Command // State -> Command ->

    State let evolve state command = let newState = // ... newState let state = ??
  53. None
  54. Closed Tab

  55. Closed Tab initial state

  56. Closed Tab Open Tab initial state

  57. Closed Tab Open Tab initial state

  58. Closed Tab Opened Tab Open Tab initial state

  59. Closed Tab Opened Tab Open Tab initial state

  60. Closed Tab Opened Tab Open Tab initial state Tab Opened

  61. Closed Tab Opened Tab Open Tab initial state Tab Opened

    event
  62. Closed Tab Opened Tab Open Tab initial state Tab Opened

    event // State -> Command -> Event list let execute state command = let events = // ... events
  63. Closed Tab Placed Order Opened Tab Order InProgress Served Order

  64. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened
  65. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed
  66. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared
  67. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served
  68. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Order Served
  69. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  70. Events Are Facts Closed Tab Opened Tab Tab Opened event

    current state new state
  71. Events Are Facts It’s like a Bank Statement! Closed Tab

    Opened Tab Tab Opened event current state new state
  72. // State -> Event -> State let apply state event

    = let newState = // ... newState Events Are Facts It’s like a Bank Statement! Closed Tab Opened Tab Tab Opened event current state new state
  73. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  74. Closed Tab Placed Order Opened Tab Order InProgress Served Order

    Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  75. Tab Opened Closed Tab Placed Order Opened Tab Order InProgress

    Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  76. Tab Opened Order Placed Closed Tab Placed Order Opened Tab

    Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  77. Tab Opened Order Placed Drink Served Closed Tab Placed Order

    Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  78. Tab Opened Order Placed Drink Served Food Prepared Closed Tab

    Placed Order Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  79. Tab Opened Order Placed Drink Served Food Prepared Food Served

    Closed Tab Placed Order Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  80. Tab Opened Order Placed Drink Served Food Prepared Food Served

    Order Served Closed Tab Placed Order Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  81. Tab Opened Order Placed Drink Served Food Prepared Food Served

    Order Served Tab Closed Closed Tab Placed Order Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  82. Tab Opened Order Placed Drink Served Food Prepared Food Served

    Order Served Tab Closed It’s append only! Closed Tab Placed Order Opened Tab Order InProgress Served Order Tab Opened Order Placed Drink Served Food Prepared Drink Served Food Prepared Food Served Tab Closed Order Served
  83. past events Closed Tab initial state // State -> Event

    -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  84. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  85. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  86. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  87. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  88. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress Food Served Order InProgress Order InProgress // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  89. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress Food Served Order InProgress Order InProgress Order Served Served Order Order InProgress // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  90. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress Food Served Order InProgress Order InProgress Order Served Served Order Order InProgress Tab Closed Closed Tab Served Order // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  91. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress Food Served Order InProgress Order InProgress Order Served Served Order Order InProgress Tab Closed Closed Tab Served Order // State -> Event list -> State let computeState initialState pastEvents = List.fold apply initialState pastEvents // State -> Event -> State let apply state event = let newState = // ... newState Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  92. Closed Tab Tab Opened Opened Tab past events Closed Tab

    initial state Opened Tab Order Placed Placed Order Placed Order Drink Served Order InProgress Food Prepared Order InProgress Order InProgress Food Served Order InProgress Order InProgress Order Served Served Order Order InProgress Tab Closed Closed Tab Served Order // State -> Event list -> State let computeState initialState pastEvents = List.fold apply initialState pastEvents // State -> Event -> State let apply state event = let newState = // ... newState Performance ?? Tab Opened Order Placed Drink Served Food Prepared Food Served Order Served Tab Closed
  93. None
  94. // State -> Command -> State let evolve state command

    = let newState = // ... newState
  95. // State -> Command -> State let evolve state command

    = let newState = // ... newState // State -> Command -> Event list let execute state command = let events = // ... events // State -> Event list -> State let computeState initialState pastEvents = List.fold apply initialState pastEvents // State -> Event -> State let apply state event = let newState = // ... newState
  96. // State -> Command -> State let evolve state command

    = let newState = // ... newState // State -> Command -> Event list let execute state command = let events = // ... events // State -> Event list -> State let computeState initialState pastEvents = List.fold apply initialState pastEvents // State -> Event -> State let apply state event = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command
  97. // State -> Command -> State let evolve state command

    = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command
  98. // State -> Command -> State let evolve state command

    = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command We started with this
  99. // State -> Command -> State let evolve state command

    = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command We started with this and evolved to this!
  100. // State -> Command -> State let evolve state command

    = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command Current state is a left-fold of past events Event Sourcing - Greg Young We started with this and evolved to this!
  101. // State -> Command -> State let evolve state command

    = let newState = // ... newState // Event list -> Command -> Event list let evolve pastEvents command = let state = computeState TabClosed pastEvents execute state command currentState = foldl(pastEvents, initialState) Current state is a left-fold of past events Event Sourcing - Greg Young We started with this and evolved to this!
  102. // Event list -> Command -> Event list let evolve

    = // ... Handling Commands
  103. // Event list -> Command -> Event list let evolve

    = // ... // Command -> int let getTableId command = // ... // int -> Event list let getPastEvents tableId = // ... Handling Commands
  104. // Event list -> Command -> Event list let evolve

    = // ... // Command -> int let getTableId command = // ... // int -> Event list let getPastEvents tableId = // ... // Command -> Event list let handleCommand command = let pastEvents = getTableId command |> getPastEvents evolve pastEvents command Handling Commands
  105. // Command -> Event list let handleCommand command = let

    pastEvents = getTableId command |> getPastEvents evolve pastEvents command // HttpRequest -> HttpResponse let commandHandler httpRequest = let command = // get Command from HTTP request let newEvents = handleCommand command saveEvents newEvents // return OK response Handling Commands
  106. Serve Food Open Tab Place Order Serve Drink Prepare Food

    Close Tab Commands
  107. Table Status Food(s) To Prepare Item(s) To Serve Amount To

    Pay
  108. Table Status Food(s) To Prepare Item(s) To Serve Amount To

    Pay Queries!!
  109. Old School Backend Data Store

  110. Old School Backend Data Store Imperative Data Modeling!

  111. { "tables" : [{"id" : 1, "status" : "available"}] }

    { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } { "tables" : [{"id" : 1, "status" : "in_service"}] } Data Model
  112. { "tables" : [{"id" : 1, "status" : "available"}] }

    { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } { "tables" : [{"id" : 1, "status" : "in_service"}] } Data Model Mutate during Commands
  113. { "tables" : [{"id" : 1, "status" : "available"}] }

    { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } { "tables" : [{"id" : 1, "status" : "in_service"}] } Data Model Mutate during Commands Fetch during Queries
  114. Single Responsibility Commands Queries Command Query Responsibility Separation

  115. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

  116. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish
  117. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  118. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish Listen Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  119. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  120. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  121. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  122. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  123. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  124. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read Pull Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  125. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read Pull Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  126. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read Listen Pull Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  127. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Pull Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  128. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  129. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Listen Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  130. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  131. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  132. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  133. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  134. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc.,
  135. Event Projection Closed Tab Opened Tab Open Tab Tab Opened

    Publish ReadModel Projection Client Listen Populate Query API Server Read WebSocket Client Listen Push Pull Data Analytics Client Listen Populate Message Queue Tab Opened Kafka, RabbitMQ, etc., Uni directional Data flow (Flux?)
  136. Read Model Projection

  137. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] }
  138. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened
  139. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] }
  140. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } Order Placed
  141. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } Order Placed { "tables" : [{"id" : 1, "status" : "in_service"}] }
  142. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } Order Placed { "tables" : [{"id" : 1, "status" : "in_service"}] } Order Served
  143. Read Model Projection { "tables" : [{"id" : 1, "status"

    : "available"}] } Tab Opened { "tables" : [{"id" : 1, "status" : "waiting_for_order"}] } Order Placed { "tables" : [{"id" : 1, "status" : "in_service"}] } Order Served
  144. Error Handling

  145. Error Handling User may post an invalid command Fetching Events

    may fail Saving Events may fail
  146. Error Handling User may post an invalid command Fetching Events

    may fail Saving Events may fail type Result<'T,'E> = | Success of 'T | Error of 'E
  147. User may post an invalid command // Event list ->

    Command -> Event list let evolve = // ... // Command -> Event list let handleCommand command = let pastEvents = getTableId command |> getPastEvents evolve pastEvents command // State -> Command -> Event list let execute state command = let events = // ... events
  148. User may post an invalid command // Event list ->

    Command -> Event list let evolve = // ... // Command -> Event list let handleCommand command = let pastEvents = getTableId command |> getPastEvents evolve pastEvents command // State -> Command -> Event list let execute state command = let events = // ... events // State -> Command -> Result<Event list,string> let execute state command = match command with | OpenTab -> match state with | ClosedTab -> Success [TabOpened] | _ -> Error "Tab already opened" // ...
  149. User may post an invalid command // Event list ->

    Command -> Event list let evolve = // ... // Command -> Event list let handleCommand command = let pastEvents = getTableId command |> getPastEvents evolve pastEvents command // State -> Command -> Event list let execute state command = let events = // ... events // State -> Command -> Result<Event list,string> let execute state command = match command with | OpenTab -> match state with | ClosedTab -> Success [TabOpened] | _ -> Error "Tab already opened" // ... // Event list -> Command -> Result<Event list, string> let evolve = // ...
  150. User may post an invalid command // Event list ->

    Command -> Event list let evolve = // ... // Command -> Event list let handleCommand command = let pastEvents = getTableId command |> getPastEvents evolve pastEvents command // State -> Command -> Event list let execute state command = let events = // ... events // State -> Command -> Result<Event list,string> let execute state command = match command with | OpenTab -> match state with | ClosedTab -> Success [TabOpened] | _ -> Error "Tab already opened" // ... // Event list -> Command -> Result<Event list, string> let evolve = // ... // Command -> Result<Event list, string> let handleCommand command = // ...
  151. User may post an invalid command // HttpRequest -> HttpResponse

    let commandHandler httpRequest = let command = // get Command from HTTP request let newEvents = handleCommand command saveEvents newEvents // return OK response
  152. User may post an invalid command // HttpRequest -> HttpResponse

    let commandHandler httpRequest = let command = // get Command from HTTP request let newEvents = handleCommand command saveEvents newEvents // return OK response // HttpRequest -> HttpResponse let commandHandler httpRequest = let command = //... match handleCommand command with | Success newEvents -> saveEvents newEvents // return OK response | Error message -> // return BAD_REQUEST
  153. User may post an invalid command // HttpRequest -> HttpResponse

    let commandHandler httpRequest = let command = // get Command from HTTP request let newEvents = handleCommand command saveEvents newEvents // return OK response // HttpRequest -> HttpResponse let commandHandler httpRequest = let command = //... match handleCommand command with | Success newEvents -> saveEvents newEvents // return OK response | Error message -> // return BAD_REQUEST Fetching Events may fail Saving Events may fail
  154. User may post an invalid command // HttpRequest -> HttpResponse

    let commandHandler httpRequest = let command = // get Command from HTTP request let newEvents = handleCommand command saveEvents newEvents // return OK response // HttpRequest -> HttpResponse let commandHandler httpRequest = let command = //... match handleCommand command with | Success newEvents -> saveEvents newEvents // return OK response | Error message -> // return BAD_REQUEST Fetching Events may fail Saving Events may fail https://fsharpforfunandprofit.com/rop/
  155. Performance Tab Opened Order Placed Drink Served Food Prepared Food

    Served Order Served Tab Closed
  156. Performance Tab Opened Order Placed Drink Served Food Prepared Food

    Served Order Served Tab Closed Do I need to fetch all the events to compute the state every time?
  157. Performance Tab Opened Order Placed Drink Served Food Prepared Food

    Served Order Served Tab Closed Do I need to fetch all the events to compute the state every time? Need not to be!
  158. Performance Tab Opened Order Placed Drink Served Food Prepared Food

    Served Order Served Tab Closed Do I need to fetch all the events to compute the state every time? Need not to be! Snapshots
  159. Parallel Update

  160. Parallel Update Optimistic Locking

  161. Parallel Update Optimistic Locking Tab Opened 0 Order Placed 1

    Drink Served 2 Food Prepared 3 Food Served 4
  162. It’s not a “Silver Bullet” Thinks To Consider

  163. It’s not a “Silver Bullet” Thinks To Consider CQRS and

    Event Sourcing are not top-level Architectures
  164. It’s not a “Silver Bullet” Thinks To Consider A whole

    system based on Event Sourcing is an Anti-Pattern CQRS and Event Sourcing are not top-level Architectures
  165. - ThoughtWorks Tech Radar 2012 F# is excellent at concisely

    expressing business and domain logic - http://fsharp.org/testimonials/
  166. bit.ly/fsapplied https://github.com/tamizhvendan/CafeApp http://fsharp.org