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…

Creator: @jdubray
Gitter chat: jdubray/sam - Gitter

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”.



  • 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)


  • 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



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”


Related architectures


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”


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:!topic/elm-discuss/r8sA57H7HEw


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.


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.



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)!


I’d prefer if you call me JJ.

Disclaimer, I was one of the original author of the SDO specification (Service Data Object- 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.


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)
  • (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


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.


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”, (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 (


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.


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:

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.



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


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


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.


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:


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.