<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Caner Saritac on Medium]]></title>
        <description><![CDATA[Stories by Caner Saritac on Medium]]></description>
        <link>https://medium.com/@csaritac?source=rss-2535a1701e33------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3C2qqUXqQZAnZfv9</url>
            <title>Stories by Caner Saritac on Medium</title>
            <link>https://medium.com/@csaritac?source=rss-2535a1701e33------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:29:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@csaritac/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Session State Management With Clustered Cache]]></title>
            <link>https://medium.com/@csaritac/session-state-management-with-clustered-cache-55db7a665d1c?source=rss-2535a1701e33------2</link>
            <guid isPermaLink="false">https://medium.com/p/55db7a665d1c</guid>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[sessions]]></category>
            <category><![CDATA[session-state]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Caner Saritac]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 09:35:59 GMT</pubDate>
            <atom:updated>2020-09-16T09:38:08.366Z</atom:updated>
            <content:encoded><![CDATA[<h3>1.Sessions And Cookies</h3><p>As we all know that HTTP (HyperText Transfer Protocol) was designed to be support stateless. Stateless means, the server does not know about your past request. All requests by your browser are considered as a new one. But there are some cases where we would like our states to be remembered in some ways. One of the best examples that could be given is in an online shop, after we add a product in a shopping cart we don&#39;t want out selected products to be disappeared when you leave to the other page to purchase more items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*VxB4rB90ZddXcCVIm_uQqw.png" /></figure><p>That’s why we need to maintain states meaning that we need to add the stateful ability to stateless protocol HTTP. Other for every request we had to log in again to an authenticated websites.</p><p>Why??</p><p>These all magic happen because of cookies which are less than the size of 4kb.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/165/1*Wc-MdzWKDQ_NjoC3fEzUkw.png" /></figure><p>We can think of cookies like an ID card. So the first time when we go to a company, we don’t have an ID card. If we don’t have an ID, then each day when we enter into the company, we need to give our identity.</p><p>If we request an ID card when we enter the first time, then the company will give an ID card. So each time when we go, we don’t need to show our identity. That ID card will act as a unique identity of us. If we show our ID card of that company, then that will be more than enough. Then the security guard will allow us.</p><p>Similarly, in the very first request to the server from a browser, the HTTP request will not have a cookie value. So the server will set a new cookie for that browser session and sends it back with the response. So the browser will store the cookie for that domain.</p><h4>1.1 Session-Based Authentication</h4><p>In the session-based authentication, the server will create a session for the user after the user logs in. The session id is then stored on a cookie on the user’s browser. While the user stays logged in, the cookie would be sent along with every subsequent request. The server can then compare the session id stored on the cookie against the session information stored in the memory to verify the user’s identity and sends a response with the corresponding state!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*MJB7ElUKeuWBzOnwbUASIg.png" /></figure><h4>1.2 Token-Based Authentication</h4><p>In the token-based application, the server creates JWT with a secret and sends the JWT to the client. The client stores the JWT (usually in local storage) and includes JWT in the header with every request. The server would then validate the JWT with every request from the client and sends a response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*2Ivf_-UfuohZhpgqiYZQSg.png" /></figure><p>The biggest difference here is that the user’s state is not stored on the server, as the state is stored inside the token on the client-side instead. Most of the modern web applications use JWT for authentication for reasons including scalability and mobile device authentication.</p><h4>1.3 Scalability</h4><p><strong>1.3.1. Session-based authentication</strong></p><p>Because the sessions are stored in the server’s memory, scaling becomes an issue when there is a huge number of users using the system at once.</p><p><strong>1.3.2. Token-based authentication</strong></p><p>There is no issue with scaling because the token is stored on the client-side.</p><h3>2. Session State Provider With Redis</h3><h4>2.1 High Available Architecture</h4><p>For web apps using session, states are required most of the time in order to make HTTP calls stateful. If this is the case we need to think about session state optimizations. In a monolithic architecture, you can store session state in a lot of places you can store it in SQL Server, in local files, and also in memory of the server that you run on. In-memory is commonplace to store sessions because having it in memory is much faster than an SQL server or a file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*SSXwQJnP99hp8zh7OyFFhg.png" /><figcaption>Monolithic architecture</figcaption></figure><p>But what happens when you scale your application to multiple instances. As in the picture below your new application instance might run on its own server with its own memory. That&#39;s difficult because the state is in the memory of your original server. And what if one of your servers fails maybe you are running in the cloud which means that another server takes over, but the new instance doesn’t have the state in its memory. These are problems that you have to address. You could use an external SQL server to solve these problems. But that would also make accessing session state a lot slower.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*A4cQg-RLXCDxSVQgTN63ew.png" /><figcaption>Instance 1 and Instance 2</figcaption></figure><p>There is another way, you can also store the session state of your app in Redis Cache, just like a SQL server, this is also external to your application, so it will be there and can be used when the scale or when you use another server and it is very fast because it also runs in memory. It might be a little bit slower than running in memory alongside your application because it is external, which means you need to travel the network to get to your state, but it is still very fast.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*dntJJDcJArgJtYtUR3Lfhg.png" /></figure><h4>2.1 Load Balancer Architecture</h4><p>Let’s start with the session handling basics. When the user logs in, the session is created on one web server in the cluster. On subsequent requests, the load balancer may bounce that user to another web server which doesn’t have that session information. To the user, it appears that they are no longer logged in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*WuGXLbXL5dpyLxBVSAlxEg.png" /></figure><p><strong>2.1.2 Sticky sessions.</strong></p><p>They mean user sessions, usually identified by a cookie, will tell the load balancer to always send requests from a client to the same server. Thus all requests from the same client are sent to the same server. “Sticky sessions” is a common way to solve the problem described at the beginning. If the session storage is not shared, in the case of the server is down, the user will lose all the session data. To overcome it, there are two strategies.</p><p><strong>2.1.2.1 Replicating the session data across the cluster</strong></p><p>The load balancer redirects requests from the failed server to another server that server will use its copy of the session to continue the client’s session from where it was before the failure. Thus the client will not notice any service interruption, which is the goal of high availability strategy.</p><p><strong>2.1.2.2 Сentral shared session storage</strong></p><p>Persistent stores such as RDBMS or NoSQL databases are commonly used as session drivers. The RDBMS, such as MySQL or Oracle, is considered too slow for the session handling, because every request may update or insert data. NoSQL databases are much better for this purpose: Redis, MongoDB, Tarantool, Memcached, Cassandra.</p><p><strong>2.1.3 Non-Sticky sessions.</strong></p><p>As we see, the centralized session storage looks like the universal solution, both for sticky and non-sticky sessions. The default hybris doesn’t support the centralized session storage, but the newer versions support it to an even lesser extent.</p><p>As a summary, the flow of session management shall be as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*qXK1I5gFj3SJZO8AfVviWw.png" /></figure><p>Thanks for Reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55db7a665d1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>