<p>How do you architecture a Kubernetes application so that a logged-in user is always served back session information stored inside the correct Redis replica?</p>
<p>I've got a working Apollo/GraphQL application written in Typescript which logs users in and stores their session information in Redis. I'm not sure how to architect the application for production, when I'll have multiple Redis instances running, via Kubernetes. The Kubernetes configuration files that I've currently written (for Redis and the application) are <a href="https://github.com/KingOfCramers/typeorm-typegrapqhl-api/tree/master/infrastructure" rel="nofollow noreferrer">here</a>.</p>
<p>Presumably I'll need to have some sort of Load Balancer service sitting in front of my application in order to distribute traffic. But here's where I'm a little confused––</p>
<p>When a user makes a request to my application (via a kubernetes loadbalancer service, for instance) how do I ensure that my application checks the &quot;right&quot; Redis replica? It's my understanding that would be necessary to ensure that their credential information is retrieved, for instance to check their logged-in status. If my application is checking a different Redis replica every time for the user's details (via a cookie/session) then I'm not sure how the logged-in functionality would work ... unless I'm mistaken and somehow Kubernetes knows how to search across all the replicas?</p>
<p>Here's how my current application <a href="https://github.com/KingOfCramers/typeorm-typegrapqhl-api/blob/master/src/redisLogic.ts" rel="nofollow noreferrer">connects</a> to Redis (this works after starting up Redis and exposing it via a ClusterIP) if that's relevant:</p>
<pre><code>import Redis from &quot;ioredis&quot;;
import session from &quot;express-session&quot;;
import connectRedis from &quot;connect-redis&quot;;

// Running Redis with docker-compose
let tries = 5;
const connectionOpts: Redis.RedisOptions = {
  host: process.env.REDIS_HOST,
  port: parseInt(process.env.REDIS_PORT as string) || 6379,
  retryStrategy: (time) =&gt; {
    if (tries === 0) {
      throw new Error(&quot;Could not connect to Redis.&quot;);
    } else {
      setTimeout(() =&gt; {
        tries--;
      }, time);
      return 2000;
    }
  },
};

// Connect to Redis
export const redis = new Redis(connectionOpts);

// Configure Redis to store session information
const RedisStore = connectRedis(session);

// Initialize session parameters and cookie name, etc.
export const mySession = session({
  store: new RedisStore({
    client: redis,
  }),
  name: &quot;qid&quot;,
  secret: process.env.SECRET || &quot;wiuy10b1la&quot;,
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    secure: process.env.ENV === &quot;production&quot;,
    maxAge: 1000 * 60 * 60 * 24 * 7 * 365,
  },
});
</code></pre>

Harrison Cramer
<p>Looks like you are running single master node Redis . if you are running the multiple Redis replicas then they must be running in sync or i think in cluster mode.</p>
<p>In cluster mode, Redis will clone the data across the multiple replicas.</p>
<p>you can read at : <a href="https://redis.io/topics/cluster-tutorial#redis-cluster-101" rel="nofollow noreferrer">https://redis.io/topics/cluster-tutorial#redis-cluster-101</a></p>
<p>you can also read more about redis replication concepts: <a href="https://redislabs.com/redis-enterprise/technology/highly-available-redis/" rel="nofollow noreferrer">https://redislabs.com/redis-enterprise/technology/highly-available-redis/</a></p>
<p>Regarding load balancing requests it will be done by the K8s service but your service won't be knowing which is &quot;right&quot; (read)(<strong>slave</strong>) replica and which one is &quot;read/write&quot;(<strong>Master</strong>) replica of redis so there is another component come in picture known as <strong>Sentinel</strong>.</p>
<p><strong><a href="https://github.com/andymccurdy/redis-py/blob/master/README.rst#sentinel-support" rel="nofollow noreferrer">Sentinel</a></strong> always checks master and slave nodes and tries to stable the cluster of Redis if any failure occurs without human intervention.</p>
<p>if you are running <strong>HA Redis cluster</strong> it will replicate your data across multiple replicas and using the library your can first request to sentinel and it will give you <strong>master IP</strong>, where you can perform a <strong>write</strong> operation on other IPs, <strong>read</strong> operation.</p>
<p>simple python code</p>
<pre><code>from redis.sentinel import Sentinel
sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
sentinel.discover_master('mymaster')
('127.0.0.1', 6379)
sentinel.discover_slaves('mymaster')
[('127.0.0.1', 6380)]
</code></pre>
<p>On the master node, you will be able to write the data and on slave you can only read the data and replication will be there at a place across all replicas.</p>
<p>You should have to check the replication first inside Redis, still as i am not sure how you have set up the Redis can't suggest much.</p>
