Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more.
# Start a Redis server
redis-server
# Connect to the Redis server
redis-cli
# Set a key-value pair
SET mykey "Hello"
# Get the value of a key
GET mykey- Caching
- Session storage
- Real-time analytics
- Message brokering
- Pub/Sub: Publish and subscribe to messages.
- Transactions: Execute a series of commands as a single atomic operation.
- Lua Scripting: Run Lua scripts for complex operations.
- Persistence: Save data to disk with RDB and AOF persistence.
# Subscribe to a channel
SUBSCRIBE mychannel
# Publish a message to a channel
PUBLISH mychannel "Hello, Redis!"# Start a transaction
MULTI
# Queue commands
SET key1 "value1"
SET key2 "value2"
# Execute the transaction
EXEC-- Example of a Lua script to increment a key
local current = redis.call("GET", KEYS[1])
if not current then
current = 0
end
redis.call("SET", KEYS[1], current + 1)
return current + 1# Save data to disk (RDB)
SAVE
# Save data to disk (AOF)
BGREWRITEAOF- Use Expiration: Set expiration times for keys to manage memory usage.
- Monitor Performance: Use Redis monitoring tools to track performance and usage.
- Data Structures: Choose the right data structure for your use case.
- Persistence: Configure persistence settings based on your data durability requirements.
- Security: Secure your Redis instance with authentication and network isolation.