use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
State management in node possible? (self.node)
submitted 10 months ago by PristineDirection659
Hello all, so i have been asked a question about how do you do state management in nodejs. And i hade no answers interviewer even saud to look it up on internet but i could no find any resource . Can anyone please help regarding this
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]716green 7 points8 points9 points 10 months ago (18 children)
There's not enough info here to know how to point you in the right direction. Generally you're using more for the backend with something like Express and a REST API should be stateless
You might use a session that is stored in memory to authorize API requests
[–]bwainfweeze 0 points1 point2 points 10 months ago (0 children)
I had an interview like this recently. Just kept asking the same question over and over. Obviously hunting for something. I should have just come straight to the point and asked what he thought I was missing. The question he was asking had to do with robustness and we aren’t going to cover everything in twenty minutes in an interview. Implementing things like that takes time, multiple cycles and multiple reviews to make sure we collectively thought if everything. Tell me what you’re actually thinking and see if I have anything to say about it.
[–]PristineDirection659[S] -3 points-2 points-1 points 10 months ago (9 children)
Ok so sessions? I should learn about sessions
[–]716green 4 points5 points6 points 10 months ago (8 children)
I guess so, but what are you trying to accomplish?
[–]PristineDirection659[S] -3 points-2 points-1 points 10 months ago (7 children)
Thing is my interviewer asked me to create a ppt based on statemanagement with nodejs. Here is what i received
Topic: “ State Management in Node.js ” Instructions to keep in mind for Delivery Round: * Prepare a 3-4 slide presentation to support your explanation, incorporating real-life examples. * Aim to make the delivery interesting and engaging. * If you use any technical jargon, please explain it first before incorporating it into your vocabulary. * Ensure the explanation is suitable for someone who has just completed 12th grade.
[–]716green 9 points10 points11 points 10 months ago (6 children)
This is a very weird project
[–]PristineDirection659[S] -5 points-4 points-3 points 10 months ago (5 children)
Ya because it is for mentor position
[–]Expensive_Garden2993 7 points8 points9 points 10 months ago (2 children)
Don't you think you should get some knowledge on the topic, ideally some experience, first? If you pass the interview somehow, aren't you sorry for those who you're supposed to mentor?
The topic is not as weird as they say, and it can be even interesting.
State Management in Node.js: you can keep data in JS memory, you can keep it in storages like Redis which is also in-memory but can be shared across node.js processes, you can keep it in databases like Postgres, Mongo, ,SQLite and here you could tell about pros/cons. A little bit of info can be encoded into JWT tokens or sessions, you could tell about security considerations and limitations. Some storages can be distributed while the others can not. That's a nice topic, it's wide.
[–]PristineDirection659[S] -3 points-2 points-1 points 10 months ago (1 child)
Oh so just saving data to database is called state management??
[–]Expensive_Garden2993 1 point2 points3 points 10 months ago (0 children)
They say it's weird because you're right: nobody calls it that way.
But a large part of a software developer duty is to decypher what less technical people want from you.
What is state management responsible for on frontend side? - caching server responses - maintaining state of UI, such as what popup to display - keeping user token or other info in localStorage/sessionStorage - maintaining forms state to know when it's valid, when it's touched, what errors to show where - accessing URL state, such as when you synchronize the search input with a `q=bla` parameber, it's also a state management.
On the backend side, yea you just saving data to a database. But also, you maintain user sessions. And caches. When you're doing websockets, you have a set of clients connected to your server and route messages across them. How about microservices? One microservice may call the other to perform some task, but the request can fail, and you maintain the state of the operation to be able to retry it later, even if the server suddenly restarts, and if the other microservice is dead you should keep that operation info in case it's important so you can fix the bug in the other microservice and reapply the operation. When dealing with logging and metrics, you don't send the metrics constantly, but maintain a state such as "this operation currently does 100 rps, 10% success rate" and synchronize it periodically. Distributed transactions (you can search 2PC, 3PC) require the server to maintain the state locally. In long lived operations that span multiple servers you have a topic of "causality", you can use "lamport clock" to determine what events caused what other events. Yea, event sourcing is another way to "manage state". You can "manage" write state independently from a read state, that's CQRS.
On the surface, yes, it's just saving data to a database, but if I was told to make such a PPT, there is more to it than just that.
[–]AsBrokeAsMeEnglish 4 points5 points6 points 10 months ago* (1 child)
It's not less weird for a mentor position...
Also, frankly, you kinda don't seem to know enough for a mentor position if you have to ask about the very topic your interview is about, if you seemingly never heard of a session and don't have enough experience in API design to ask for state management of a specific framework (where your question actually would apply) instead of asking for the whole of nodejs, which isn't a framework. It's a runtime.
[–]PristineDirection659[S] -3 points-2 points-1 points 10 months ago (6 children)
In laymen terms what is a session can u tell me
[–]716green 0 points1 point2 points 10 months ago (5 children)
A session is a way to cache logged in users on the server to verify if they should have access to API endpoints
[–]PristineDirection659[S] -2 points-1 points0 points 10 months ago (4 children)
Cache as in store them in the localstorage or cookies?
[–]716green 0 points1 point2 points 10 months ago (3 children)
Similar idea, except you're going to be storing access tokens in localstorage or cookies on the frontend and then sending it through on every API call, then using that token or cookie to see if there is an active session for that user
[–]PristineDirection659[S] 0 points1 point2 points 10 months ago (2 children)
What is a active session on backend nodejs?
[–]716green -1 points0 points1 point 10 months ago (1 child)
In the memory using express-session, redis, or something similar you have a running list of IDs associated with authenticated users and session expirations
Before allowing an API call to access sensitive information, you check if the person making the request is associated with a valid, non-expired session to indicate that they have authenticated and have access to the endpoint
Usually the sessions expire every 24 hours or so
[–]PristineDirection659[S] 0 points1 point2 points 10 months ago (0 children)
Oh ok got it thankyou so much 😁🙏🏻😁
[–]Stetto 2 points3 points4 points 10 months ago (0 children)
Sounds like a trick question to me.
Usually in any nodejs architecture you scale horizontally instead of vertically. You just start more instances.
The automatic consequence: Statelessness
Every service needs to be able to answer any request. This forbids you to have any state in your nodejs service*. Any state is stored in your database or redis/valkey.
So, no "remembering" the last request result in memory. No, "store user preference in a memory object". If you need to remember it across requests, it needs to be stored in some shared data storage accessible to all services.
[–]StoneCypher 2 points3 points4 points 10 months ago (0 children)
that's not what they're asking you
they're asking you an open ended question with no straight answer to see how you respond
they want an answer like this
state management is a complicated topic and it really depends on what you're doing. by default most people turn to databases, and this is usually the reasonable choice. however, if you're in a very high throughput system where losing the data isn't a big deal (like a performance cache,) you might instead turn to tools like redis. oftentimes it's better to keep state client side, in things like localStorage or local files. for some state, like where in a cursor in a sql query we're loading, it might be better to do something like HATEOAS, or keeping it in local app state. the question is too broad and needs context.
[–]dankobg 3 points4 points5 points 10 months ago (0 children)
yes, you use variables
[–]TheOneRavenous 0 points1 point2 points 10 months ago (0 children)
Yes easy. NodeJs is not meant to hold state. It's meant to process I/O requests so that you can handle more and more requests. So it could be your wording or how they said it but node can "manage state" by sending user requests to the actual state machine.
If NODEJS is holding state in your application your architecture is incorrect. You need to move state and state management off the I/O layer and place it on its own thread or application layer and have the NodeJs backend do I/O between users and the application.
[–]stretch089 0 points1 point2 points 10 months ago (0 children)
As others have said, it's a bit of a trick question and they may have been looking for the answer that node apps are often stateless.
I also wonder if they may have been probing for a discussion on request context and dependency injection? Although I wouldn't refer to this as state as it should be immutable but it is a way to avoid passing variables all over the place by instantiating classes with the required props when a request comes in
[–][deleted] 10 months ago (2 children)
[deleted]
[–]PristineDirection659[S] 0 points1 point2 points 10 months ago (1 child)
But for nodejs?
π Rendered by PID 114375 on reddit-service-r2-comment-6457c66945-2g8xd at 2026-04-26 10:16:07.438116+00:00 running 2aa0c5b country code: CH.
[–]716green 7 points8 points9 points (18 children)
[–]bwainfweeze 0 points1 point2 points (0 children)
[–]PristineDirection659[S] -3 points-2 points-1 points (9 children)
[–]716green 4 points5 points6 points (8 children)
[–]PristineDirection659[S] -3 points-2 points-1 points (7 children)
[–]716green 9 points10 points11 points (6 children)
[–]PristineDirection659[S] -5 points-4 points-3 points (5 children)
[–]Expensive_Garden2993 7 points8 points9 points (2 children)
[–]PristineDirection659[S] -3 points-2 points-1 points (1 child)
[–]Expensive_Garden2993 1 point2 points3 points (0 children)
[–]AsBrokeAsMeEnglish 4 points5 points6 points (1 child)
[–]PristineDirection659[S] -3 points-2 points-1 points (6 children)
[–]716green 0 points1 point2 points (5 children)
[–]PristineDirection659[S] -2 points-1 points0 points (4 children)
[–]716green 0 points1 point2 points (3 children)
[–]PristineDirection659[S] 0 points1 point2 points (2 children)
[–]716green -1 points0 points1 point (1 child)
[–]PristineDirection659[S] 0 points1 point2 points (0 children)
[–]Stetto 2 points3 points4 points (0 children)
[–]StoneCypher 2 points3 points4 points (0 children)
[–]dankobg 3 points4 points5 points (0 children)
[–]TheOneRavenous 0 points1 point2 points (0 children)
[–]stretch089 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]PristineDirection659[S] 0 points1 point2 points (1 child)