<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Managing session in Micro-Services]]></title><description><![CDATA[Managing session in Micro-Services]]></description><link>https://session.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 22:23:50 GMT</lastBuildDate><atom:link href="https://session.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding Stateless Microservices and the Mystery of Session Management]]></title><description><![CDATA[“If REST APIs are stateless, how come our app still remembers who I am?”That’s the exact question that unlocked a cascade of insights for me—and probably the reason you’re reading this right now.
🔹 What is "State" in Software?
In software, state sim...]]></description><link>https://session.hashnode.dev/understanding-stateless-microservices-and-the-mystery-of-session-management</link><guid isPermaLink="true">https://session.hashnode.dev/understanding-stateless-microservices-and-the-mystery-of-session-management</guid><category><![CDATA[auth token]]></category><category><![CDATA[Microservices]]></category><category><![CDATA[State Management ]]></category><category><![CDATA[Redis]]></category><category><![CDATA[REST API]]></category><category><![CDATA[Session]]></category><category><![CDATA[server management]]></category><dc:creator><![CDATA[Shailesh Patil]]></dc:creator><pubDate>Fri, 11 Jul 2025 15:16:40 GMT</pubDate><content:encoded><![CDATA[<p><em>“If REST APIs are stateless, how come our app still remembers who I am?”</em><br />That’s the exact question that unlocked a cascade of insights for me—and probably the reason you’re reading this right now.</p>
<h3 id="heading-what-is-state-in-software">🔹 What is "State" in Software?</h3>
<p>In software, <strong>state</strong> simply means: stored information about a user or process that persists across requests.</p>
<p>It could be:</p>
<ul>
<li><p>Logged-in user information</p>
</li>
<li><p>Items in a shopping cart</p>
</li>
<li><p>Preferences or roles</p>
</li>
<li><p>Temporary application data</p>
</li>
</ul>
<p>When we say something is <strong>stateful</strong>, it means it remembers things. The server stores information from our response.<br />When we say it’s <strong>stateless</strong>, it forgets everything between requests — meaning the server holds no cache or data from our transaction between request and response.</p>
<hr />
<h3 id="heading-what-does-it-mean-when-a-micro-service-is-stateless">🔹 What Does It Mean When a Micro-service is Stateless?</h3>
<p>So as we know, micro-services are multiple services or components deployed independently which communicate with each other.</p>
<p>For example, you log into Amazon and then go to:</p>
<ul>
<li><p>Orders</p>
</li>
<li><p>Wishlist</p>
</li>
<li><p>Buy Now</p>
</li>
</ul>
<p>All these different components are deployed independently. This is great for scaling, load balancing, and fault tolerance.</p>
<p>But how do we share the data between all these components?</p>
<p>You cannot be logging in to check your orders and then again logging in to check your wishlist.<br />That provides a bad user experience and creates unnecessary API calls to login.</p>
<hr />
<h3 id="heading-so-what-is-a-session-exactly">🔹 So… What is a Session, Exactly?</h3>
<p>A <strong>session</strong> is a server-side storage of user data across multiple requests.</p>
<p>Think of it as a <strong>temporary memory slot</strong> the server gives to a user once they log in.<br />It typically includes:</p>
<ul>
<li><p>User ID</p>
</li>
<li><p>Roles / Permissions</p>
</li>
<li><p>Auth token or session ID</p>
</li>
<li><p>Timestamps, IP, etc.</p>
</li>
</ul>
<p>In traditional monolithic apps, session data is stored in memory (HttpSession in Java, for example).<br />But in distributed systems, that approach breaks.</p>
<p>So how do we communicate between these components <strong>without creating a new API just for session sharing</strong>?</p>
<p>After all, you may have noticed that when you run your application locally it creates a Tomcat server, and if we save our session data on the Tomcat server, then that data will be limited to <strong>that one server only</strong>.</p>
<p>How will a different server access it, making sure your session is continuing without a hassle — providing a smooth experience?</p>
<hr />
<h3 id="heading-the-challenge-stateless-services-need-session-data">🔹 The Challenge: Stateless Services Need Session Data</h3>
<p>Let’s say you have:</p>
<ul>
<li><p>Microservice A for authentication</p>
</li>
<li><p>Microservice B for user profile</p>
</li>
<li><p>Microservice C for transactions</p>
</li>
</ul>
<p>Each is deployed on separate servers, maybe auto-scaled.<br /><strong>How can they all recognize the same user and maintain consistency in session?</strong></p>
<p>You can’t store session in the memory of just one server (like you might in Tomcat), because:</p>
<ul>
<li><p>Another service won’t have access to it</p>
</li>
<li><p>Load balancers might route requests to different instances</p>
</li>
<li><p>It won't survive server restarts</p>
</li>
</ul>
<p>So how do we solve this?</p>
<hr />
<h3 id="heading-so-what-are-the-options-for-session-management-in-microservices">🔹 So What Are the Options for Session Management in Microservices?</h3>
<hr />
<h4 id="heading-1-session-stored-in-redis-what-my-project-uses"><strong>1. Session Stored in Redis (What My Project Uses)</strong></h4>
<p>This is what I just learned and found very smart.<br />To preserve statelessness while still maintaining session, we move session storage <strong>outside the micro-services</strong> to a centralized, fast, and scalable store like <strong>Redis</strong>.</p>
<p>Here’s how it works:</p>
<ul>
<li><p>When a user logs in, we create a session object (something like <code>userId</code>, <code>email</code>, maybe <code>roles</code>, etc.)</p>
</li>
<li><p>We store this session in Redis</p>
</li>
<li><p>A unique token (like <code>X-Auth-Token</code>) is given to the user</p>
</li>
<li><p>For every API call, the client sends this token</p>
</li>
<li><p>Our services read that token, fetch the session data from Redis, and proceed</p>
</li>
</ul>
<p><strong>This works well because:</strong></p>
<ul>
<li><p>All services can access that same session</p>
</li>
<li><p>Redis is super fast</p>
</li>
<li><p>We can expire sessions easily</p>
</li>
</ul>
<hr />
<h4 id="heading-2-session-stored-inside-the-server-memory-not-ideal-for-microservices"><strong>2. Session Stored Inside the Server Memory (Not Ideal for Microservices)</strong></h4>
<p>This is how old-school monoliths used to work.</p>
<ul>
<li><p>User logs in → session is stored in that specific server’s memory (like in Tomcat)</p>
</li>
<li><p>If the next request goes to a different server → session is not found</p>
</li>
</ul>
<p>You’d need to either:</p>
<ul>
<li><p>"Stick" the user to one server (called <strong>sticky sessions</strong>)</p>
</li>
<li><p>Or build <strong>complex APIs</strong> to share that session data across services</p>
</li>
</ul>
<p>It becomes a headache as soon as your system grows.</p>
<hr />
<h4 id="heading-3-stuff-everything-in-the-token-itself-jwt-approach"><strong>3. Stuff Everything in the Token Itself (JWT Approach)</strong></h4>
<p>Some systems don’t store sessions at all. Instead, they:</p>
<ul>
<li><p>Create a big, signed token (called <strong>JWT</strong>) and give it to the client</p>
</li>
<li><p>That token contains everything the server needs to know (user ID, roles, etc.)</p>
</li>
<li><p>Every time the client sends the token, the server reads it, trusts it, and moves on</p>
</li>
</ul>
<p><strong>Sounds nice, but here’s the problem:</strong></p>
<ul>
<li><p>You <strong>can’t revoke or update</strong> a token easily once it’s issued</p>
</li>
<li><p>If something changes (like user’s role), the token is now outdated</p>
</li>
<li><p>It can also become <strong>too large</strong> if you stuff too much info in it</p>
</li>
</ul>
<p>So it works well when you just need <strong>basic identity info</strong>, but <strong>not for large session data</strong></p>
]]></content:encoded></item></channel></rss>