Redis

Kubernetes deployment

chevron-rightmanifesthashtag

d

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 2
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        resources:
          requests:
            memory: "1Gi"   # Minimum memory requested
            cpu: "500m"      # 0.5 CPU requested
          limits:
            memory: "1Gi"    # Maximum memory limit set to 2GB
            cpu: "1"         # Maximum CPU limit set to 1 core
        ports:
        - containerPort: 6379
        livenessProbe:
          exec:
            command:
            - redis-cli
            - ping
          initialDelaySeconds: 5
          periodSeconds: 10
        readinessProbe:
          exec:
            command:
            - redis-cli
            - ping
          initialDelaySeconds: 5
          periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  selector:
    app: redis
  ports:
  - protocol: TCP
    port: 6379
    targetPort: 6379
  type: ClusterIP

d

Check Redis data

chevron-rightcheck datahashtag

Since you have deployed Redis in Kubernetes, you can check the stored data using redis-cli. Here’s how you can do it:


🔹 Step 1: Get Redis Pod Name

Run the following command to get the Redis pod name:

kubectl get pods -l app=redis

Example output:

NAME                     READY   STATUS    RESTARTS   AGE
redis-7f9d5b7d9c-xyz    1/1     Running   0          10m

Copy the pod name (e.g., redis-7f9d5b7d9c-xyz).


🔹 Step 2: Access Redis Pod

Now, exec into the Redis pod:

kubectl exec -it redis-7f9d5b7d9c-xyz -- redis-cli

Once inside the Redis CLI, you can start running commands.


🔹 Step 3: Check Redis Data

1️⃣ List all keys

KEYS *

2️⃣ Get a specific key's value

GET key_name

3️⃣ Check memory usage

INFO memory

4️⃣ Check all data inside Redis

SCAN 0

5️⃣ Monitor real-time Redis commands

MONITOR

🔹 Step 4: Exit Redis CLI

Once done, exit the Redis CLI:

exit

💡 Alternative: Run Redis CLI from Another Pod

If your API pod needs to check Redis data, you can run the redis-cli from that pod:

  1. Get your API pod name:

    kubectl get pods -l app=api
  2. Exec into the API pod:

    kubectl exec -it api-pod-name -- redis-cli -h redis-service

    (Replace api-pod-name with your actual API pod name.)

Let me know if you need more details! 🚀

chevron-righthow it workshashtag

🔹 Full Flow: How Redis Works in an API for Customer Login

Let's break it down step by step, assuming Redis is used for session management and caching.


🔹 Step-by-Step Flow

1️⃣ Customer Logs into the Server

  • A customer enters their username & password.

  • The API server (e.g., a Node.js, Python, or Go backend) receives the request.

  • The server checks credentials against the database (PostgreSQL, MySQL, MongoDB, etc.).


2️⃣ Redis Caching: Checking for an Active Session

  • Before querying the database, the API first checks Redis to see if the user has an active session.

  • Redis command:

    GET session:user_id
  • If a session exists in Redis:

    • Return session details from Redis (faster than querying DB).

    • If no session found, continue authentication.


3️⃣ Authentication & Session Storage in Redis

  • If Redis has no session, the API authenticates using the database.

  • On successful authentication:

    • The API generates a JWT token or session ID.

    • The token is stored in Redis for quick access:

      SET session:user_id "auth_token_value" EX 3600  # Expires in 1 hour
  • The API returns the session token to the client.


4️⃣ Client Makes Authenticated API Requests

  • On every subsequent request:

    • The client sends the JWT token in the request header.

    • The API verifies the token against Redis.

    • Redis command:

      GET session:user_id
    • If token is valid: API processes the request.

    • If token is expired or missing: API rejects the request & asks for re-login.


5️⃣ Redis for Rate Limiting & Security

  • Redis can rate-limit login attempts to prevent brute-force attacks.

  • Example: Allow only 5 login attempts per minute.

  • Redis command:

    INCR login_attempts:user_id
    EXPIRE login_attempts:user_id 60

6️⃣ Logging Out (Removing Session from Redis)

  • When a user logs out:

    DEL session:user_id
  • This removes the session, forcing the user to log in again next time.


🔹 How Redis Improves Performance?

Faster login validation (Redis lookup instead of DB query). ✅ Session caching reduces database load. ✅ Rate limiting prevents abuse. ✅ TTL (Time-To-Live) for automatic session expiration.

Would you like an API implementation example using Redis? 🚀

Last updated