TOMWELLS.ORG

Distributed FRP: taking the pattern to the backend

The Functional Reactive Programming pattern gained popularity through a tiny little web language called Elm; and because it was so good it was quickly copied and integrated into more popular frameworks such as React & Angular through libraries such as RxJS, Flux and Redux. Its probably safe to say that you're using one of these technologies right now, they are so pervasive.

The beauty of FRP can be captured in a single function signature, which is so obvious and self-explanatory (english below for those with code-o-phobia):

Java / C# / C / etc:

Java
State Update (
	State currentState, 
	Event nextEvent
)

Python (with type hints):

Python
def update(
		current_state: State, 
		next_event: Event
	) -> State:

Haskell:

Haskell
update :: State -> Event -> State

In English:

PlainText
Give me the current [State of the World] and some [Event] that is occuring and I will give you back the new [State of the World] where that event has been applied. 

Of course [State of the World] and [Event] as very specific to your particular use-case and application or system, and often where most of the work lies in designing such a system.

Application of FRP can lead to a certain beauty in it's ability to capture the simplicity of almost everything, and I would wager that almost anything can be described through this fundamental signature. Its a great mental exercise and I challenge you to find an example which can't be described this way. Give it a try—I'd love to hear your thoughts.

Hand-drawn diagram: each event and the current world state feed an update function f(), producing the next world state in a chain.

So I think the web and mobile world is convinced and we see more and more FRP bleeding slowly into building great client-side applications. The interesting shift will come as we grapple with how this idea could apply to backend systems which are fundamentally more distributed, horizontally scaled and typically stateless.

We see a glimmer of this pattern if we squint our eyes at Apache Flink's Stateful Functions, Haskell Datomic and some newer emerging frameworks such as Restate. And even the original author of Elm, Evan Czaplicki, is busy with developing his angle on it (read about it here). We might need to wait for him to figure it all out before the world copies him again :)

In the meantime it's a great pattern and one that is easy to incorporate in a crude form into our own development projects, and with so many good reasons to do so, I'll leave it to you as homework to figure out exactly how!

Article
  { slug: "distributed-frp"
  , originalUrl: Nothing
  , title: "Distributed FRP: taking the pattern to the backend"
  , date: constructDate 2023 10 27
  , keywords: ["FRP","distributed systems"]
  , cover: { alt: "Code", caption: Nothing, src: "/images/covers/code.png" }
  , summary: "Taking the state-and-event update pattern from client applications into distributed systems."
  , author: { bio: "Towards an AI-first, hyper-scaled, functional reactive universe!", email: "tom@tomwells.org", name: "Tom Wells" }
  , body:
      [ FlowRichParagraph [ InlineText "The Functional Reactive Programming pattern gained popularity through a tiny little web language called Elm; and because it was so good it was quickly copied and integrated into more popular frameworks such as React & Angular through libraries such as RxJS, Flux and Redux. Its probably safe to say that you're using one of these technologies ", InlineEmphasis "right now", InlineText ", they are so pervasive." ]
      , FlowParagraph "The beauty of FRP can be captured in a single function signature, which is so obvious and self-explanatory (english below for those with code-o-phobia):"
      , FlowParagraph "Java / C# / C / etc:"
      , FlowSourceCode { lang: Java, body: "State Update (\n\tState currentState, \n\tEvent nextEvent\n)" }
      , FlowParagraph "Python (with type hints):"
      , FlowSourceCode { lang: Python, body: "def update(\n\t\tcurrent_state: State, \n\t\tnext_event: Event\n\t) -> State:" }
      , FlowParagraph "Haskell:"
      , FlowSourceCode { lang: Haskell, body: "update :: State -> Event -> State" }
      , FlowParagraph "In English:"
      , FlowSourceCode { lang: PlainText, body: "Give me the current [State of the World] and some [Event] that is occuring and I will give you back the new [State of the World] where that event has been applied. " }
      , FlowRichParagraph [ InlineText "Of course ", InlineCode "[State of the World]", InlineText " and ", InlineCode "[Event]", InlineText " as very specific to your particular use-case and application or system, and often where most of the work lies in designing such a system." ]
      , FlowParagraph "Application of FRP can lead to a certain beauty in it's ability to capture the simplicity of almost everything, and I would wager that almost anything can be described through this fundamental signature. Its a great mental exercise and I challenge you to find an example which can't be described this way. Give it a try—I'd love to hear your thoughts."
      , FlowImage { alt: "Hand-drawn diagram: each event and the current world state feed an update function f(), producing the next world state in a chain.", caption: Nothing, src: "/images/articles/distributed-frp/frp.png" }
      , FlowParagraph "So I think the web and mobile world is convinced and we see more and more FRP bleeding slowly into building great client-side applications. The interesting shift will come as we grapple with how this idea could apply to backend systems which are fundamentally more distributed, horizontally scaled and typically stateless."
      , FlowRichParagraph [ InlineText "We see a glimmer of this pattern if we squint our eyes at ", InlineLink { link: "https://nightlies.apache.org/flink/flink-statefun-docs-stable/", title: "Apache Flink's Stateful Functions" }, InlineText ", ", InlineLink { link: "https://docs.datomic.com/pro/overview/introduction.html", title: "Haskell Datomic" }, InlineText " and some newer emerging frameworks such as ", InlineLink { link: "http://restate.dev", title: "Restate" }, InlineText ". And even the original author of Elm, Evan Czaplicki, is busy with developing his angle on it (", InlineLink { link: "https://gotoaarhus.com/2023/sessions/2529/elm-on-the-backend", title: "read about it here" }, InlineText "). We might need to wait for him to figure it all out before the world copies him again :)" ]
      , FlowParagraph "In the meantime it's a great pattern and one that is easy to incorporate in a crude form into our own development projects, and with so many good reasons to do so, I'll leave it to you as homework to figure out exactly how!"
      ]
  }