Skip to content

State: what a system remembers

Some requests need only what they carry. What is 2 + 2? needs nothing else. Others depend on what happened before: what is in my cart? is a question about an earlier request, in which you added something.

The word for this is state: what a system remembers between requests, in a way that changes how it answers later ones. The contents of a cart, who is logged in, and the stock of a product are all state.

Two words describe a service by where its state lives:

  • A stateful service keeps state inside itself, in its own memory or on its own disk. A request is only answered correctly by the instance that holds the state.
  • A stateless service keeps nothing between requests. Everything it needs arrives with the request, or is read from somewhere shared.

Mental model. If you can swap one instance of a service for another in the middle of a conversation and nobody notices, the service is stateless.

A stateless service has a property that makes it easy to run: its instances are interchangeable. Any instance can answer any request. You can add a tenth instance with no preparation, remove one that is failing, restart one to update it, and move work between them freely. A load balancer can send each request anywhere.

A stateful service has no such freedom. The cart lives in one instance’s memory, so that customer’s requests must go to that instance. If it restarts, the cart is gone. If it is overloaded, you cannot move that customer’s work to a quiet instance without moving the state too.

Roy Fielding makes the same case for the web’s design: when each request carries everything needed to understand it, a server needs no stored context, which makes it simpler to recover from partial failure and to scale. He also names the price, which is that the same information is sent again in every request.

Take the cart as the example. There are three places to keep it, and each is a trade-off.

Where How it works What it costs
In the instance’s memory The server keeps a table of carts. The client sends a session id, and must reach the same instance every time Instances are no longer interchangeable. A restart loses the carts. Routing must be sticky
In a shared store Instances keep nothing. The session lives in a database or cache that all of them read, found by a session id One more dependency on every request, and a store that must be fast and available
In the client The cart travels in the request, for example in a cookie or a signed token, and the server checks it Larger requests, and the server cannot easily change or cancel what a client holds, so what is stored must be safe to leave with the user

The Twelve-Factor App method states the rule directly: processes are stateless and share nothing, anything that must last goes in a backing service such as a database, and “sticky sessions” are a violation of it, because session data belongs in a store with expiry, not in a process’s memory.

Notice what each choice did. None of them removed the state. The cart still exists, and someone has to keep it correct. They moved it: out of the service and into a store, or out to the client.

Check what you have understood about moving state around.

Check your understanding: state

This quiz needs JavaScript to check your answers.

Question 1 of 3

A shop keeps each cart in the memory of the web instance that created it. Four instances sit behind a load balancer that spreads requests evenly. A customer adds a product to the cart, then refreshes the page.

What can happen, and why?

Choose one answer.

Question 2 of 3

Which of these make a service stateless in the way that matters for scaling?

Choose every answer that applies.

Question 3 of 3

To scale the shop, the team moves carts from the memory of each web instance to one shared Redis server that all instances use.

Which statement is true?

Choose one answer.

What happens if the instance that holds your cart restarts?

Section titled “What happens if the instance that holds your cart restarts?”

The shop keeps carts in each web instance’s memory. There are four instances, and a customer with eight items in the cart is sent to instance 3.

  1. A deploy restarts instance 3. The customer’s next request goes to instance 3 again, or to a different one, and either way the cart is gone. They are shown an empty basket.
  2. The team makes the load balancer send each customer to the same instance every time. That fixes the empty basket until an instance fails, and then it makes things worse: the failed instance’s carts are lost, and the customers of the other instances see nothing wrong.
  3. Autoscaling adds an instance at peak and removes it afterwards. Every cart on the removed instance disappears.

Each fix leaves the cause in place: the state is in a place that can go away. Putting carts in a shared store removes all three problems at once. It adds a different one: a store that every request depends on, which must now be as available as the shop.

How much room does that take? A rough estimate: 100,000 active carts at about 5 kB each is 500 MB, which is small enough for one in-memory store. The size is rarely the problem. The availability of the store is.

  • State is what a system remembers between requests: carts, logins, stock.
  • A stateless service keeps nothing between requests, so its instances are interchangeable: add, remove, restart and route freely.
  • A session can live in an instance’s memory, in a shared store, or in the client, and each choice costs something different.
  • Making a service stateless does not remove the state. It moves it, and the hard problem moves with it.
  • Losing an instance should lose nothing that cannot be recovered. That is the test.

Next: dependencies, the things a request cannot do without, and what they do to availability.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.