Managing Symfony Sessions: Deployment with Redis and Docker Compose
Redis Cheatsheet: Docker & Essential Commands

Redis Cheatsheet: Docker & Essential Commands

🐳 1. Launch and Management via Docker

Using Docker is the cleanest way to launch a Redis instance in development or production.

Basic Launch (Ephemeral)

Ideal for quick tests or temporary caching. Data is lost when the container stops.

Bash

docker run --name my-redis -p 6379:6379 -d redis

Launch with Data Persistence

Uses appendonly mode to save data to your host disk and survive restarts.

Bash

docker run --name my-persistent-redis \
  -p 6379:6379 \
  -v /path/to/local/folder:/data \
  -d redis redis-server --appendonly yes

Launch with Password (Secured)

Bash

docker run --name my-secure-redis -p 6379:6379 -d redis redis-server --requirepass "MySuperStrongPassword"

Connect to the Redis CLI (from the container)

Once your container is running, use this command to open the Redis prompt (redis-cli) and type the commands below:

Bash

docker exec -it my-redis redis-cli
# If you set a password:
docker exec -it my-redis redis-cli -a "MySuperStrongPassword"

🔑 2. Basic Commands (Keys & Strings)

Strings are the simplest data type in Redis (ideal for page or session caching).

CommandUsage ExampleDescription
SETSET user:1 "Eric"Stores the value “Eric” in the key “user:1”.
GETGET user:1Retrieves the value of the key “user:1”.
DELDEL user:1Deletes the key and its value.
EXISTSEXISTS user:1Returns 1 if the key exists, 0 otherwise.
EXPIREEXPIRE session:99 3600Sets a time-to-live of 3600 seconds (1h) for the key.
TTLTTL session:99Shows the remaining time (in seconds) before expiration. (-2 = no longer exists, -1 = never expires).
INCR / DECRINCR views:article:42Increments (or decrements) an integer by 1. Highly performant for counters.

📦 3. Advanced Data Structures

Redis excels thanks to its complex data structures, which can be manipulated directly in memory.

Lists

Arrays of strings, sorted by insertion order. Perfect for queues or news feeds.

CommandUsage ExampleDescription
LPUSH / RPUSHLPUSH tasks "email_welcome"Adds a value to the left (start) or right (end) of the list.
LPOP / RPOPLPOP tasksRemoves and returns the first (or last) element of the list.
LRANGELRANGE tasks 0 -1Shows all elements in the list (from index 0 to the last index -1).
LLENLLEN tasksReturns the size (number of elements) of the list.

Hashes

Ideal for storing structured objects (like a database row or a user profile).

CommandUsage ExampleDescription
HSETHSET user:2 name "Doe" age 35Assigns fields and values to the hash “user:2”.
HGETHGET user:2 nameRetrieves the value of the “name” field for this hash.
HGETALLHGETALL user:2Retrieves all fields and values of this hash.
HDELHDEL user:2 ageDeletes a specific field in the hash.

Sets

Collections of unique, unordered elements. Perfect for managing tags, banned IPs, or checking membership.

CommandUsage ExampleDescription
SADDSADD banned_ips "192.168.1.1"Adds an element to the set (if it already exists, it is ignored).
SMEMBERSSMEMBERS banned_ipsLists all elements in the set.
SISMEMBERSISMEMBER banned_ips "10.0.0.1"Returns 1 if the element is part of the set, 0 otherwise.

⚙️ 4. Administration and Utilities

Useful commands to monitor and clean your Redis instance.

CommandDescription
PINGConnection test. Redis replies PONG if the server is online.
INFOShows detailed server statistics (memory used, clients, version…).
KEYS *Lists all existing keys. ⚠️ Warning: Never use in production on a large database, it blocks the server (use SCAN instead).
FLUSHDBDeletes all keys from the currently selected database.
FLUSHALLDeletes absolutely all keys from all databases in the instance.
MONITORShows all commands received by the server in real time. Handy for debugging in development.

Sur Tellaw.org :

Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.