The SAM (State - Action - Model) pattern - General Discussion, Blog Posts, Wiki

SAM is a new reactive/functional pattern that simplifies Front-End architectures by clearly separating the business logic from the view and, in particular, strictly decoupling back-end APIs from the Front-End. SAM is technology independent and as such can be used to build Web Apps or Native Apps. It is also protocol independent and can be implemented over HTTP, WebSockets…

http://sam.js.org/

Creator: @jdubray
Gitter chat: https://gitter.im/jdubray/sam

SAM is a pattern that aims at replacing the MVC pattern. If you are already familiar with MVC, you should be able to make an easy transition to SAM.

There are two main differences between MVC and SAM. First, SAM is reactive, while MVC is generally implemented in an interactive way. Some attempts have been made at implementing it in a reactive way, however, SAM is not just a reactive implementation of MVC. Second, SAM introduces the concept of State as a mechanism to further decouple the View from the Model and treats the view merely as a “State Representation”.

Articles

Examples

  • sam-tic-tac-toe
  • SAM Lambda — SAM Lambda is a boiler plate project that implements the SAM pattern in AWS Lambda using DynamoDB to manage application/session state

Temporal logic of actions (TLA)

Articles

  • Temporal logic of actions (TLA) is a logic developed by Leslie Lamport, which combines temporal logic with a logic of actions. It is used to describe behaviours of concurrent systems.

  • TLA+

    … is a formal specification language developed by Leslie Lamport. It is used to design, model, document, and verify concurrent systems. TLA+ has been described as exhaustively-testable pseudocode and blueprints for software systems.

    TLA+ is based on the idea that the best way to describe things formally is with simple mathematics, and that a specification language should contain as little as possible beyond what is needed to write simple mathematics precisely. TLA+ is especially well suited for writing high-level specifications of concurrent and distributed systems.

  • Use of Formal Methods at Amazon Web Services

  • Why We Should Build Software Like We Build Houses

INDIRECT

Presentations

Academic Papers

See The Writings of Leslie Lamport for a full list of publications from this researcher.

“Programming languages provide no well-defined notion of a program step”

“State machines provide a framework for much of computer science. They
can be described and manipulated with ordinary, everyday mathematics”

Books

Related architectures

8 Likes

Interesting. This is the first time I heard it called SAM. I first heard about it as the Reactor model, or even “The Elm Architecture”

7 Likes

Because Dubray has some critique on elm and says that an alignment with SAM would make it stronger I posted a question in an elm discussiongroup: https://groups.google.com/forum/#!topic/elm-discuss/r8sA57H7HEw

4 Likes

What’s missing in modern frameworks like Elm is the notion of a “Step” you just cannot arbitrarily process events, that is the problem. SAM suggests that a “step” (in response to an event) is composed of three phases: propose, accept and learn. When Elm issues a series of tasks to run some effects, it misses the essence of state mutation.

SAM is based on TLA+ semantics which introduce a new way to look at state machines, a lot closer to the way we write every day code (as opposed to traditional semantics like tuples (S0,A,S1). TLA+ provides enough structure without the burden of traditional state machines.

Happy to continue the discussion if you’d like.

4 Likes

Shouldn’t the model and the state be one? Which states you have and transitions between them is completely defined by the model so splitting them seems strange.

But I may have misinterpreted the meaning of state and model.

Robert

4 Likes

Mr. Dubray, I would love to get some more explanation about your graphql critique here: Api for an SPA.
Thanks for joining the discusion (on the elm forum also)!

3 Likes

I’d prefer if you call me JJ.

Disclaimer, I was one of the original author of the SDO specification (Service Data Object- http://www.oracle.com/technetwork/testcontent/sdo-specification-java-v2-131073.pdf). I also worked for companies like Object Design (Object and XML Database) and built some solutions with products like MetaMatrix (now part of JBoss)

So, I am certain that GraphQL can solve a class of problems. I am sure it does wonders to Facebook because they can align their entire back-end to play nicely with GraphQL but this is not true of everyone. Stitching a complex back-end to GraphQL will always be the problem like it was for SDO or MetaMatrix. The other problem is more fundamental, data is relational and not “navigational”, no matter how you look at it and as soon as you start distributing it, you’ll run into performance issues each time you’ll need to perform joins across system boundaries. There is simply no way around it.

So again, I am certain it works well for Facebook, but it will fail entirely in the enterprise. You just can’t fight the relationality of data.

2 Likes

Ok I agree that GraphQL not always be prefect match. But what about the case you can have full control of backed and you can easy write back end as GraphQL?

Another solution for complex backed is BFF.

Anyway developers are moving away to handle different endpoint REST services in client and prefer to have some simplify/more optimized solutions. Imagine if you have 20 different REST endpoints you need to handle in mobile app. This is nightmare.

For me interesting alternatives to REST:

  • GraphQL/Relay
  • pouchdb <->CouchDB (client read/writes to local database -> automatic populated to server database)
  • https://www.firebase.com/ (real time database)
1 Like

David Fall has created a SAM sample using FireBase, it’s really good. We are currently discussing it on Gitter

gitter.im/jdubray/sam

2 Likes

Yes, that was exactly the problem I was trying to solve with SAM. This problems comes mainly from templates and databinding (1 API per screen/view)

That being said, weaving API calls in React is a real nightmare, a complete after thought in their programming model.

With SAM, APIs are hidden behind the model and the actions, so that you can use coarse grained APIs, up to direct persistence of the model in a document oriented database. I wrote an AWS Lambda / DynamoDB sample if you want to take a look.

2 Likes

Sure I will check with pleasure :slight_smile:

Here is the link to the AWS Lambda sample:

1 Like

@jdubray I just read your infoq article and I must admit I’m a bit puzzled. I’m trying to find a summary of what makes SAM distinct. My poor old brain is getting bogged down in the details (I wrote my first web app in '93). This sounds more flippant than I intend, but my best summary at the moment is that “server API design is so much easier if front-end developers use a Monad-based approach.” Is that even remotely accurate? Perhaps I misread all those details of the distinction between actions, model updates, and side-effects? If I’m even remotely accurate, then I question the practicality of the approach.

1 Like

1993? my first commercial product was in 1980… on an Apple ][ :slight_smile:

To answer your question I would have to pretend I understand what a monad is, which I won’t.

Really all I am trying to say in the article is:
From M -> V
V = f(M) works great (thank you React)

from V -> M
use TLA+ semantics: propose (action), accept (model) and learn (state)

From what I can tell all the other alternatives do not make that distinction (Elm, Redux, Cycle, MobX, Vue and now VueX)… and they start fighting problems that you generally do not encounter with SAM.

One argument I always hear about the pure Reactive Functional approach and separating effects from the logic is about testability. That’s until you really understand the math behind “testing”, http://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf (the irony here, is that TLA+ makes your system far more testable, but what do I know)

I also see a lot of people thinking that being “reactive” means something other than V = f(M), you have to somehow use RxJS, Observables, Streams and what not. That’s until you need to start dealing with subscriptions and glitches.

SAM (and TLA+) focuses on getting mutation right (propose, accept, learn). Dr. Lamport got a turing award for TLA+ (Temporal Logic of Action), so it can’t be that bad, right? Some quotes from him:

“Programming languages provide no well-defined notion of a program step” (SAM does)

“Computation is a major topic of computer science, and almost every object that computes is naturally viewed as a state machine. Yet computer scientists are so focused on the languages used to describe computation that they are largely unaware that those languages are all describing state machines.” (TLA+ gives SAM enough structure, without the burden of traditional State Machine semantics)

If you have not read this article on Computation and State Machines, it’s may be too difficult to understand where SAM is coming from (http://research.microsoft.com/en-us/um/people/lamport/pubs/state-machine.pdf).

2 Likes

Thanks! I’m currently working through Lamport’s vector clock paper and I’m adding the state machine one to my reading queue now. I do have concern, for practicality’s sake, that there is not a more concise description though.

2 Likes

Please also note that SAM is “my” interpresentation of TLA+ as a programming model. I exchanged several emails with Dr. Lamport last year, as I was laying SAM’s foundation, but by no means he has validated SAM, or SAM would be the only way to translate TLA+ semantics into a programming model.

I can certainly share more specific references inside Dr. Lamport’s book to explain how I created SAM from TLA+.

The key to understand (which most people find unusual) is that TLA+ describes state machines, but not with the semantics we are all accustomed to. I try to explain it here: http://sam.js.org/#sm

Then of course the next step is to decide how TLA+ semantics apply to Front-End architectures. The traditional semantics of State Machines (Petri nets) are not a good fit, but TLA+ is very different. That’s why I believe it is applicable.

3 Likes

*bump*

I just added a bunch of info to the top post, mostly about TLA+. :024:

4 Likes

I just would like to add a couple of quotes from Lamport on TLA+

State machines provide a framework for much of computer science. They
can be described and manipulated with ordinary, everyday mathematics

and

Programming languages provide no well-defined notion of a program step

The semantics of Mathematics and hence TLA+ are quite different from the ones of the familiar Petri Nets, and a lot closer to the way we write code. I believe he has found the most fundamental way to describe state machines.

IMHO, the temporal aspects of TLA+ bring a lot of benefits when compared to observables which seem to be the way the industry is heading. Observables seem to make dealing with temporal aspects harder, in particular when you need to reason about different types of events allowed in a given step.

2 Likes

After reading a bit more from mr. Dubray I must say that I do not like a couple of his ideas and the way he tries to persuade (and hides behind jargon) in a couple of email groups.
He tries to sell the idea of isomorphic javascript. Must ring a bell for elixir devs ;-). I placed some comments below his article: https://www.infoq.com/articles/no-more-mvc-frameworks.

3 Likes

I have published a series of blog posts on Back-End architecture (Services, APIs and Microservices - SAMs) and how the SAM pattern aligns with them). As Stefan’s points out, I believe this is not just a key selling point of the pattern but a must have for a grown up programming model. If all you do is produce a programming model that works well in the browser and think of the back-end as just a bunch of HTTP calls, you would have achieved nothing. You will notice on the right end-end the great simplification isomorphism brings to the table when you can align back-end and front-end.

BTW, I go by “JJ”, no need to use any kind of formality.