All Articles

Should I Store State In Memory On The Server?

Jack Herrington
Jack Herrington
August 20th, 2023

Question: Should I store state in memory on the server?

Answer: It's super tempting to store state on the server. For example in demos I'll store the to-do list we are working on in memory on the server, as opposed to say, in a database or in a microservice. But I'm doing that in the demo becuase I'm focusing on how we edit or view the to-dos and not how we store them.

In production you should never store any state in server memory between requests. In other words you want your server to be stateless. This means that given a user identity (usually provided as a cookie) and a URL any server should be able to service any request from any user. When this is the case you can scale your application to service more customers by simply deploying more servers.

If you store the data from customer A in the memory of server B, then only server B can process requests from that customer because only that server has that data. This is technically do-able using "sticky sessions" but it is undesirable because the only way to scale for a customer or a set of customers is to make the single server bigger.

This is also why you should always deploy at least two instances of a given server in production. If you have one server then it's possible to hold state in memory and not see any issues. If you have two load-balanced servers then if you hold state in memory you will see bugs as users requests bounce between servers and the state between those servers gets out of sync.

It is ok to cache data in memory on a per-request basis. For example if three components on the same page need the same piece of data from a REST service then you cache that request once at the start of the request and then use that cache for all three requests. This is fine as long as you are guaranteed that the cache is flushed after each request. The fetch caches provided by React and NextJS are per-request caches, so they are safe.

Long story short, even though at times you might see someone persistently storing state on a server in a demo, that's not something you should do in production.

Share this article with your friends

Subscribe for Free Tips, Tutorials, and Special Discounts

We're in this together!

We respect your privacy. Unsubscribe at any time.

Jack Herrington

Written by Jack Herrington

Jack Herrington is a Full Stack Principal Engineer who orchestrated the rollout of React/NextJS at Walmart Labs and Nike. He is also the "Blue Collar Coder" on YouTube where he posts weekly videos on advanced use of React and NextJS as well as other frontend technologies trends. His YouTube channel hosts an entire free courses on React and TypeScript. He has written seven books including most recently No-BS TypeScript which is a companion book to the YouTube course.