concept state in category akka

This is an excerpt from Manning's book Reactive Applications with Akka .NET.
The first component that a potential customer is likely to encounter is the shopping cart, analogous to the shopping cart in a physical store, which they can use when browsing to collect the items they intend to purchase. Thousands of users may browse the site at once, so your application must support the simultaneous use of thousands of shopping carts. To design this, you’ll create a shopping cart, which is nothing more than a list of items and the quantity of items. Each shopping cart is accessed through a unique identifier, which is stored in the user’s session. One of the core attributes of actors is the ability to store state, which varies per type of actor. In the case of the shopping cart, you could store a dictionary of the user’s session identifier, along with a list of items and quantities. This is how you might model the component if you were to use a database. But actors serialize all incoming messages; only one message is processed at any one time, in order. This means that if lots of users are trying to access their shopping carts at the same time, then they’ll have to sit through long queues. This defeats the aim of providing a responsive UX.
While a customer is browsing the store, they’ll hopefully add items to their cart. There are a number of states a shopping cart can exist in. For example, the two key states are browsing, where the customer continues to browse the store, and purchase-completion, where the user is completing a purchase. When a user is browsing, they should be able to add more items to their cart; but once they start the process of completing a purchase, you typically don’t allow the cart to be modified. This is for a number of reasons, such as the need to reserve items while the purchase is being completed, and computing the cost of the shopping cart contents. You therefore want to prevent new items from being added to the cart while it’s in the purchase-completion state.
You saw in the previous section how the shopping cart can be represented as an actor and as part of the actor model. Actors can change their state on demand to respond differently to subsequent messages. Given how this is such a fundamental component of designing actors within actor systems, Akka.NET provides the functionality of switching between states to invoke different behavior and simplify the process of responding to messages. In figure 2.2, you can see how the shopping cart actor can switch between multiple different states.

This is an excerpt from Manning's book Akka in Action.
The traditional approach starts with a simple in-memory application, which turns into an application that relies completely on a database for both concurrency and mutating state. Once the application needs to be more interactive, we’ll have no choice but to poll this database. When more network services are added, we’ll show that the combination of working with the database and the RPC-based network increases complexity significantly. We’ll also show that isolating failure in this application becomes very hard as we go along. We think that you’ll recognize a lot of this.
Previous chapters have advanced many reasons for using stateless components when implementing a system to avoid all kinds of problems, like restoring state after an error. But in most cases, there are components within a system that need state to be able to provide the required functionality. You’ve already seen two possible ways to keep state in an actor. The first is to use class variables, which we showed in our aggregator example (section 8.4.2). This is the simplest way. The second solution is to use the become/unbecome functionality, which we used in our state-dependent router (section 9.3.2). These two mechanisms are the more basic ways to implement state. But in some situations, these solutions are insufficient.
In this chapter we’ll show you two other solutions for dealing with state. We’ll start with how to design dynamic behavior, depending on the actor’s state, using finite-state machine modeling. We’ll create an example model that will be implemented in the second section, where we’ll show that Akka has support for easily implementing a finite-state machine. In the last section, we’ll show how you can share state between different threads by using Akka agents. Using these agents eliminates the need to use locking mechanisms, because the state of the agents can be changed only asynchronously using events; but the state can be read synchronously, without any significant performance penalty.
The simplest example of a finite-state machine is a device whose operation proceeds through several states, transitioning from one to the next as certain events occur. The washing machine is usually the classic example used to explain FSMs: there’s a process that requires initiation steps, and then once the machine takes over, it progresses through a sequence of specific states (filling the tub, agitation, draining, spinning). The transitions in the washing machine are all triggered by a program that wants a certain amount of time for each stage based on the user’s desires (light/heavy loads, prewash, and so forth). The machine is only ever in one state at a time. The purchase order process mentioned earlier is a similar example from business: there’s an established protocol for two parties to define an exchange of goods or services. With the example of the business documents, you can see that for each stage of the FSM there’s a state representation (a purchase order, a quote, or a request for a quote). Modeling software this way allows you to deal with state in an atomic, isolated way, which is a core principle of the actor model.
An FSM is called a machine because it can only be in one of a finite number of states. Changing from one state to another is triggered by an event or condition. This state change is called a transition. A particular FSM is defined by a number of states and the different triggers for all the possible transitions. There are a lot of different ways to describe the FSM, but most of the time it’s described with some kind of a diagram. Figure 11.1 shows a simple diagram to illustrate how we describe the FSM, because there are a number of different notations when creating an FSM diagram.
In this example we show an FSM with two states, State1 and State2. When instantiating the machine, we start in State1, transitioning from the initial state, which is shown in the diagram by the black dot. State1 has two different actions: an entry action and an exit action. (Although we won’t use the exit action in this chapter, we show it so you’ll understand how the model works.) Just as the name says, the first action is executed when the machine sets the state to State1, and the second when the machine changes from State1 to another state. In this example we have only two states, so this action is only executed when it goes to State2. In the next examples, we’ll only use the entry actions, because this is a simple FSM. Exit actions can do some cleaning or restore some state, so they don’t embody part of the logic of the machine. It can be seen more like a finally clause in a try-catch statement, which must always be executed when exiting the try block.
Changing state, transition, can only happen when the machine is triggered by an event. In the diagram this transition is shown by the arrow between State1 and State2. The arrow indicates the event and optionally a state condition (for instance, we might only transition to the spin cycle when the tank is empty). The events in an Akka FSM are the messages the actor receives. That’s it for the introduction; now let’s see how an FSM can help you implement a solution to a real problem.
To start implementing an FSM model using Akka, we’ll create an Actor with the FSM trait. (The FSM trait may only be mixed into an actor.) Akka has chosen this approach instead of extending Actor to make it obvious that an actor is actually created. When implementing an FSM, we need to take several steps before we have a complete FSM actor. The two biggest steps are defining the state and then the transitions. So let’s get started creating our inventory FSM, by making an actor with the FSM trait mixed in:
The state definition process starts with a single trait (appropriately named State), with cases for each of the specific states our object can be in (note: this helps make the FSM code self-documenting):
sealed trait State case object WaitForRequests extends State case object ProcessRequest extends State case object WaitForPublisher extends State case object SoldOut extends State case object ProcessSoldOut extends StateThe defined states represent the states shown in figure 11.3. Next we have to create our state data:

This is an excerpt from Manning's book Reactive Web Applications: With Play, Akka, and Reactive Streams.
Those low-level technologies all offer basic tools for building asynchronous and event-driven applications, but it takes a bit more work to get to the state of a full-blown web application that also has to deal with concerns such as code organization, view templates, inclusion and organization of client-side resources such as stylesheets and JavaScript files, database connectivity, security, and so on. Many so-called full-stack web application frameworks exist, but few of them also include reactive technologies, and very few are built from the ground up using reactive technologies, embracing reactive principles at their core. Full-stack frameworks concern themselves with all the layers required to build and deploy an application: client-side UI technology (or a means to integrate it), server-side business logic, authentication, integration of database access, and various libraries for the most common tasks (such as remote web service calls). In a reactive application, all these layers must furthermore cooperate by following the same principles of asynchronous communication and error recovery.
One of the biggest practical hurdles of switching from a traditional application-server development model to a scalable model such as Play is solving the problem of working with state in a stateless architecture. The server-side deployment of a Play application is meant to be stateless—it doesn’t keep any state in memory other than that for the requests currently being processed. This is in keeping with the philosophy of reactive web applications: