Skip to content

Latest commit

 

History

History
86 lines (69 loc) · 2.01 KB

File metadata and controls

86 lines (69 loc) · 2.01 KB

Redis

Introduction

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.

Basic Syntax

# 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

Common Use Cases

  • Caching
  • Session storage
  • Real-time analytics
  • Message brokering

Advanced Features

  • 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.

Code Snippets

Using Pub/Sub

# Subscribe to a channel
SUBSCRIBE mychannel

# Publish a message to a channel
PUBLISH mychannel "Hello, Redis!"

Using Transactions

# Start a transaction
MULTI

# Queue commands
SET key1 "value1"
SET key2 "value2"

# Execute the transaction
EXEC

Using Lua Scripting

-- 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

Using Persistence

# Save data to disk (RDB)
SAVE

# Save data to disk (AOF)
BGREWRITEAOF

Tips & Best Practices

  • 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.

External Resources