Read and write Redis cache/store data using redis-cli or Upstash REST API. Manage keys, lists, hashes, sets, and sorted sets.
Read and write data in Redis using redis-cli or the Upstash REST API.
# Using redis-cli
redis-cli -u "$REDIS_URL" SET greeting "hello"
redis-cli -u "$REDIS_URL" GET greeting
# Using Upstash REST API
curl -s "$UPSTASH_REDIS_REST_URL/get/greeting" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" | jq '.result'
| Variable | Description | Example |
|---|---|---|
REDIS_URL | Redis connection URL | redis://default:password@host:6379 |
| Variable | Description | Example |
|---|---|---|
UPSTASH_REDIS_REST_URL | Upstash REST endpoint | https://us1-xxx.upstash.io |
UPSTASH_REDIS_REST_TOKEN | Upstash REST token | AXxxBase64Token... |
# redis-cli
redis-cli -u "$REDIS_URL" PING
# Upstash
curl -s "$UPSTASH_REDIS_REST_URL/ping" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" | jq .
# Set a value
redis-cli -u "$REDIS_URL" SET mykey "myvalue"
# Set with expiry (60 seconds)
redis-cli -u "$REDIS_URL" SET mykey "myvalue" EX 60
# Get a value
redis-cli -u "$REDIS_URL" GET mykey
# Delete a key
redis-cli -u "$REDIS_URL" DEL mykey
# Set multiple keys
redis-cli -u "$REDIS_URL" MSET key1 "val1" key2 "val2" key3 "val3"
# Get multiple keys
redis-cli -u "$REDIS_URL" MGET key1 key2 key3
# Scan keys (safe for production — non-blocking)
redis-cli -u "$REDIS_URL" SCAN 0 MATCH "prefix:*" COUNT 100
# Get key type
redis-cli -u "$REDIS_URL" TYPE mykey
# Get TTL (time to live)
redis-cli -u "$REDIS_URL" TTL mykey
# Set expiry on existing key
redis-cli -u "$REDIS_URL" EXPIRE mykey 3600
# Set hash fields
redis-cli -u "$REDIS_URL" HSET user:1 name "Alice" email "[email protected]" role "admin"
# Get one field
redis-cli -u "$REDIS_URL" HGET user:1 name
# Get all fields
redis-cli -u "$REDIS_URL" HGETALL user:1
# Push to list
redis-cli -u "$REDIS_URL" RPUSH queue "task1" "task2" "task3"
# Pop from list
redis-cli -u "$REDIS_URL" LPOP queue
# Get list range
redis-cli -u "$REDIS_URL" LRANGE queue 0 -1
# List length
redis-cli -u "$REDIS_URL" LLEN queue
# Add to set
redis-cli -u "$REDIS_URL" SADD tags "python" "javascript" "typescript"
# Get all members
redis-cli -u "$REDIS_URL" SMEMBERS tags
# Check membership
redis-cli -u "$REDIS_URL" SISMEMBER tags "python"
# Add with scores
redis-cli -u "$REDIS_URL" ZADD leaderboard 100 "alice" 85 "bob" 92 "charlie"
# Get top scores
redis-cli -u "$REDIS_URL" ZREVRANGE leaderboard 0 9 WITHSCORES
# Increment score
redis-cli -u "$REDIS_URL" ZINCRBY leaderboard 5 "bob"
# Memory usage
redis-cli -u "$REDIS_URL" INFO memory
# Database size (key count)
redis-cli -u "$REDIS_URL" DBSIZE
For serverless Redis (no redis-cli needed):
# SET
curl -s "$UPSTASH_REDIS_REST_URL/set/mykey/myvalue" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN"
# SET with EX (expiry in seconds)
curl -s "$UPSTASH_REDIS_REST_URL/set/mykey/myvalue/ex/60" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN"
# GET
curl -s "$UPSTASH_REDIS_REST_URL/get/mykey" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" | jq '.result'
# DEL
curl -s "$UPSTASH_REDIS_REST_URL/del/mykey" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN"
curl -s "$UPSTASH_REDIS_REST_URL/pipeline" \
-H "Authorization: Bearer $UPSTASH_REDIS_REST_TOKEN" \
-H "Content-Type: application/json" \
-d '[
["SET", "key1", "val1"],
["SET", "key2", "val2"],
["MGET", "key1", "key2"]
]' | jq .
UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKENREDIS_URL: redis://default:password@host:portredis://host:6379REDIS_URL=redis://localhost:6379 or redis://:password@host:6379| Error | Cause | Solution |
|---|---|---|
Could not connect | Host unreachable | Check REDIS_URL, firewall |
NOAUTH | Auth required | Include password in URL (redis://:pass@host:port) |
WRONGTYPE | Wrong operation for key type | Check key type with TYPE key first |
OOM | Memory limit reached | Delete unused keys or increase memory |
401 (Upstash) | Invalid token | Check UPSTASH_REDIS_REST_TOKEN |
SCAN instead of KEYS in production — KEYS blocks the serverEX to auto-expire keys and prevent memory bloat--no-auth-warning flag to suppress auth warnings: redis-cli --no-auth-warning -u "$REDIS_URL"