NewsInsightsDevelopment

What Is State in Programming, and Why Does It Matter?

Illustration of state in programming: code brackets surrounded by on/off toggles connected by arrows

Every app keeps track of things while you use it: the values in its variables, what’s in your cart, if you’re logged in, which tab is open. In programming, that remembered information is called state, and it describes the condition a program is in at a given moment. Dictionary.com defines state as “the particular condition that something is in at a specific time,” and that’s exactly what it means in software. When state changes, what the user sees and what the app can do changes with it.

State can still be a nebulous term. In computer science we talk about a plethora of state: application state, resource state, session state, router state, stateless servers, and so on. Why should you care? Because how developers think about state and use it in building your web applications affects your user experience. State decisions shape how fast resources load, how invalid form input errors appear and whether product listings are up to date, to name a few. This post explains state as it applies to web applications, so you and your developers can make the choices that deliver the experience you want in the next version of your site.

What is state in programming? A simple example

Think of a light switch. At any moment it’s either on or off, and that condition is its state. Flipping it is an event that changes the state, and the room changes in response. Software works the same way, with more switches.

let cartCount = 0;          // the state

function addToCart() {
  cartCount = cartCount + 1; // an event changes the state
  render();                  // the screen updates to match
}

Here cartCount is state. It persists between clicks, and the interface reads from it to decide what to show. Every web app is built from many pieces of state like this, and the hard questions are always where each piece lives, who can change it and how long it should last.

What is application state?

Application state (also called program state) is the totality of everything needed to keep your application running. When we talk about application state, we usually mean the state of the program as it exists in memory.

What does that mean in practice? It helps to think in extremes. What happens to the information and functionality core to your application if a server goes down and restarts? Whatever was in memory is lost. That’s one reason web developers often use stateless resource controllers, which handle the information your application needs without relying on holding data in the server’s memory for later retrieval.

What is resource state? REST and GraphQL

Resource state is the state of the resources stored on the server: files, images, database records and so on. It changes as resources are added, modified or deleted.

One of the great advances in web development since the early 2000s has been the adoption of the REST architecture, a stateless way of working with web resources. Each REST request explicitly states its intended action on a resource and carries the information needed to complete it. Say you want all the stories in a database, want to create a new one, then want to retrieve it:

GET  /stories     // list all stories
POST /stories     // create a new story
GET  /stories/1   // retrieve story 1

You get the drift. But even REST is being stretched. Applications now carry so much information that making multiple HTTP requests to traditional REST endpoints can hurt performance.

Enter GraphQL. GraphQL moves away from the “resources” model that dominated web development for decades and uses an entity graph as its core concept instead. As filtering and relationships between data matter more and more to the user experience, that shift pays off. With GraphQL you can retrieve very specific information in one request, using arguments to your GraphQL endpoint, that would take multiple round trips with traditional REST.

What is session state?

Session state keeps track of a user’s interaction during a browser session. It holds the status of communication between the client and the server, and today the term broadly describes state tied to a session ID and HTTP cookies.

Session state is what remembers the page you were last on, holds your cart items so they’re still there if you close your browser and come back to the store, and tells the website if you’re authorized to view a specific page. Because it’s tied to identity, it’s also where state decisions meet security.

What are the types of state in a web app?

Modern front-end development, which we touched on in Shifting states: modular JavaScript, sorts state by where it lives and who uses it. These categories overlap with application, resource and session state, but they’re the ones most teams use day to day.

UI state (local state)

Local state belongs to one component: whether a dropdown is open, what’s typed in a search box, which slide a carousel is on. It’s short-lived and nobody else needs it. Keeping it local is simpler and faster. It’s also the state that motion responds to, which is why animation design is really about the transitions between UI states.

Shared state (global state)

Shared state is data that several parts of an app read or change: the signed-in user, the cart count in the header and on the checkout page, a theme setting. It needs a single source of truth so every component shows the same value. Front-end teams use tools like React context, Redux or Zustand for this, and React’s own guide to managing state is a good primer. In back-end systems, shared state also means data that multiple threads, processes or users touch at once, which is where race conditions come from.

Server state vs. client state

Client state lives in the browser and the app owns it fully, like a form in progress or a selected filter. Server state lives on the server and the browser only holds a copy, like product prices or inventory. That copy can go stale the moment someone else changes the data, so server state needs caching, refetching and invalidation rules. Libraries such as TanStack Query exist mainly to handle this.

URL state

The URL holds state too: the current page, search terms, filters and sort order (for example, /shop?color=red&sort=price). This is router state from the list above. Putting state in the URL makes a view shareable, bookmarkable and friendly to the back button. It’s an easy UX win that a lot of sites miss.

Persisted state and database state

Database state is the data stored in your database at a given point in time: every customer, order and article. It’s the most durable form of state and survives restarts and deploys. Persisted state can also live in the browser through cookies, localStorage or IndexedDB. A common pattern is to keep servers stateless and push durable data into a database, so any server can handle any request without losing anything.

Stateless vs. stateful: what’s the difference?

A stateful system remembers previous interactions and uses them in the next one. A stateless system treats every request as new and relies only on the information that request carries. REST APIs are stateless by design, while a logged-in shopping session is stateful.

  • Stateless strengths: easier to scale (any server can answer any request), easier to recover after a crash, simpler to test and cache.
  • Stateful strengths: personalization, continuity across steps (carts, multi-page forms, logins) and fewer repeated lookups.
  • The usual answer is both: stateless application servers, with state kept deliberately in a database, cache or session store.

Nearly every useful app is stateful from the user’s point of view. The design question is where that state is stored, not whether it exists.

Why do state bugs happen?

Many of the bugs users notice are state bugs: a cart that empties, a price that’s out of date, a form that loses what you typed. They tend to come from a few causes.

  • Duplicated state: the same value stored in two places that drift apart, such as a header cart count that disagrees with the checkout page.
  • Stale server state: the browser shows a cached copy after the real data has changed, such as an item that’s sold out but still shows as available.
  • Race conditions: two updates to shared state arrive in the wrong order, and the older one wins.
  • State held in memory: data kept only on one server or in one tab disappears on a restart or refresh.
  • Unhandled in-between states: loading, empty and error states that nobody designed, so users see a blank screen or a confusing message.

The fixes follow the causes: keep one source of truth for each piece of data, keep state as local as possible, set clear rules for refreshing server data, and design every state a screen can be in, happy path or not. These are the kinds of architecture decisions our technology and development team works through early in a project, because they’re much cheaper to get right before launch.

Why state matters for your website

Where your application holds information, and how smooth the experience feels to the people using it, both come down to decisions about state. Good state decisions make a web application more resilient and faster to load, and they belong in your security planning too. The next time you build a new version of your website or launch a web application, ask your developers how they plan to handle state and what it means for your users.

Newsletter

Sign up for the Daylight Newsletter to receive helpful blog posts, insights and featured client projects.

Our Clients

Wacom Logo
Webtrends Logo
Nike Logo
Trinchero Logo
Sutter Home Logo
Ste. Michelle Logo
SheerID Logo
Providence Logo
Newmans Own Logo
Marchex Logo
Intel Logo
Black and decker Logo
Brooks Logo
Coca-Cola Logo
Dakine Logo
No Kid Hungry Logo
American Red Cross Logo
Jackson Family Wines logo
Argyle Winery logo
Wente Vineyards logo
Evanta a Gartner Company
Coeur d’Alene Resort Logo
Travel Oregon Logo
Stanley Logo
Superhuman Logo