Redis Tutorial 0/42 lessons ~6 min read Lesson 5

    Installing Redis

    You can run Redis in Docker for local dev, Homebrew on macOS, or managed services (ElastiCache, Azure Cache, Redis Cloud) in production.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    You can run Redis in Docker for local dev, Homebrew on macOS, or managed services (ElastiCache, Azure Cache, Redis Cloud) in production. The install path matters less than the configuration you apply on day one.

    Default redis.conf allows unbounded memory growth, no password on many setups, and persistence settings tuned for laptops — not multi-GB production datasets.

    This lesson covers a safe baseline: maxmemory, eviction policy, ACL, and kernel tuning before traffic hits the instance.

    Understanding the topic

    Key concepts

    • redis-server reads redis.conf at startup; CONFIG SET changes runtime (not always durable).
    • Docker is ideal for dev; bind mounts let you inject custom conf.
    • Managed Redis offloads patching, failover, and backups.
    • maxmemory + maxmemory-policy prevent OOM kills at the OS level.
    • requirepass/ACL restrict who can run FLUSHALL and CONFIG.
    • vm.overcommit_memory=1 on Linux reduces fork failures during BGSAVE.
    text
    flowchart LR
    Dev[Docker local] --> Test[Integration tests]
    Staging[Compose cluster] --> CI[Pipeline]
    Prod[ElastiCache HA] --> App[Spring Boot pods]

    Step-by-step explanation

    1. Package manager or container pulls the Redis binary.
    2. Configuration file sets port, memory limits, persistence, ACL.
    3. Server binds to interface (127.0.0.1 dev; private subnet prod).
    4. Clients connect on 6379 (or TLS wrapper port in managed services).
    5. Health checks use PING; metrics from INFO exported to Prometheus.

    Syntax reference

    Common commands

    • Always set maxmemory in any shared environment.
    • Use redis:7-alpine for smaller images in CI.
    • Map volumes for RDB/AOF only when you need local persistence.
    bash
    # Docker with memory limit
    docker run -d --name redis \
    -p 6379:6379 \
    redis:7-alpine redis-server \
    --maxmemory 256mb \
    --maxmemory-policy allkeys-lru
    # Verify
    redis-cli ping

    Informative example

    Production-style docker-compose snippet with persistence and password:

    bash
    # docker-compose.yml excerpt
    services:
    redis:
    image: redis:7-alpine
    command: >
    redis-server
    --requirepass ${REDIS_PASSWORD}
    --maxmemory 512mb
    --maxmemory-policy volatile-lru
    --appendonly yes
    ports: ["6379:6379"]

    In real production prefer managed Redis with TLS, multi-AZ, and automated backups. Compose is for staging parity.

    Real-world use

    Real-world use cases

    • Local dev stack with Docker Compose alongside PostgreSQL.
    • CI pipeline spinning ephemeral Redis for integration tests.
    • Staging cluster mirroring prod eviction and persistence settings.
    • ElastiCache replication group for production microservices.
    • On-prem Redis with Sentinel for regulated industries.

    Best practices

    • Set maxmemory to ~75–80% of instance RAM before go-live.
    • Use ACL users per application with command restrictions.
    • Enable TLS in transit for any cross-network access.
    • Set vm.overcommit_memory=1 and disable THP on Linux.
    • Pin Redis version in Docker tags — avoid :latest in prod.
    • Document restore procedure from RDB/AOF or cloud snapshot.

    Common mistakes

    • Exposing 6379 to the public internet without auth.
    • No maxmemory — Redis grows until Linux OOM killer strikes.
    • Using default persistence on dev and assuming prod matches.
    • Skipping kernel tuning then blaming Redis for fork failures.

    Advanced interview questions

    Q1BeginnerHow do you run Redis locally?
    Docker, Homebrew, or package manager; verify with redis-cli ping.
    Q2BeginnerWhy set maxmemory?
    Caps RAM usage and triggers eviction policy instead of unbounded growth or OOM.
    Q3IntermediateDocker vs managed Redis in production?
    Managed handles HA, patching, backups; self-hosted needs Sentinel/Cluster ops.
    Q4IntermediateWhat is vm.overcommit_memory?
    Linux kernel setting; value 1 reduces fork failures during RDB snapshot.
    Q5AdvancedDesign Redis deployment for a new microservice?
    Managed multi-AZ, TLS, ACL per service, maxmemory 80%, alerts on memory and evictions, staging parity.

    Summary

    Install is easy; safe configuration is the hard part.

    Ready to mark this lesson complete?Track your journey across the entire course.