Learn Redis Β· 14/15

Going Live: Security, Monitoring, and Administration

7 min read

Going Live: Security, Monitoring, and Administration πŸš€

Welcome back! Now that you've got the hang of Redis basics, it's time to talk about what happens when you move from your local development environment to a live, production server. It's like going from a go-kart to a rocket ship – things get a lot more serious, and you definitely don't want to crash. This guide will walk you through the essentials of securing your Redis instance, keeping an eye on its performance, and managing it like a pro.

1. Securing Your Instance: Passwords and the AUTH Command πŸ”

The Problem: By default, anyone who can connect to your Redis server can execute any command. This is like leaving the front door of your house wide open with a sign that says "Free stuff inside!". In a production environment, this is a recipe for disaster.

The Solution: The most basic and crucial security measure is to set a password.

How it Works:

You can set a password by editing the redis.conf file. Look for the requirepass directive.

1c
# requirepass foobared

Uncomment it and replace "foobared" with a very strong, long, and random password. You can generate a strong password using tools like openssl.

bash
openssl rand -base64 32 

Once you've set the password, you'll need to restart your Redis server for the change to take effect.

Connecting with a Password:

Now, when a client connects to the Redis server, it will need to authenticate using the AUTH command before it can execute any other commands.

Here's how you do it with redis-cli:

bash
redis-cli -a "your-super-secret-password" 

Or, after connecting:

node-repl
> AUTH your-super-secret-password
OK

If the password is correct, Redis will respond with "OK", and you can start executing commands. If not, you'll get an error.

Pro-Tip: In a production environment, you should also bind Redis to a specific network interface (e.g., bind 127.0.0.1 in redis.conf) to prevent it from being accessible from the public internet.

2. Redis ACLs (Access Control Lists): Fine-Grained Permissions πŸ’‚β€β™‚οΈ

The Problem: What if you have different types of users or applications accessing your Redis instance? Maybe you want your analytics service to only be able to read data, while your main application needs both read and write access. A single password for everyone doesn't cut it. It's like giving everyone the same master key to every room in a hotel.

The Solution: Redis Access Control Lists (ACLs), introduced in Redis 6, allow you to create multiple users with specific permissions. You can control which commands a user can execute and which keys they can access.

How it Works:

You can define ACL rules in your redis.conf file or by using the ACL commands.

An ACL rule for a user has several parts:

  • User State: on or off (enabled or disabled).
  • Passwords: One or more passwords.
  • Command Permissions: Which commands the user can run.
  • Key Permissions: Which keys the user can access.

Example:

Let's create a user named analytics-user that can only read keys with the prefix stats: and can only use the GET and KEYS commands.

pgsql
ACL SETUSER analytics-user on >some-strong-password ~stats:* +GET +KEYS 

Let's break this down:

  • ACL SETUSER analytics-user: Creates a user named analytics-user.
  • on: Enables the user.
  • >some-strong-password: Sets a password for the user.
  • ~stats:*: Allows access to keys that match the pattern stats:*.
  • +GET +KEYS: Allows the GET and KEYS commands.

Now, to connect as this user, you would use:

crmsh
> AUTH analytics-user some-strong-password
OK

Interesting Fact: Redis ACLs are designed to be lightweight, so they don't introduce a significant performance overhead, even when you have many users and complex rules.

3. Renaming or Disabling Dangerous Commands πŸ’£

The Problem: Some Redis commands are incredibly powerful and can have disastrous consequences if used incorrectly. The most notorious of these is FLUSHALL, which will delete all keys in all databases. Accidentally running this in production could be a career-limiting move.

The Solution: You can either rename these dangerous commands to something obscure or disable them completely.

How it Works:

You can do this in your redis.conf file. To rename a command, you use the rename-command directive.

livecodeserver
rename-command FLUSHALL "REALLY_SURE_YOU_WANT_TO_DELETE_EVERYTHING_42" 

To disable a command completely, you can rename it to an empty string:

livecodeserver
rename-command FLUSHALL "" 

You'll need to restart your Redis server for these changes to take effect.

A Word of Caution: While this is a useful safety measure, be aware that renaming commands that are used in AOF files or for replication can cause issues. Using ACLs to restrict access to these commands is often a better approach.

4. Monitoring Performance: INFO, MONITOR, and SLOWLOG πŸ“Š

You can't improve what you don't measure. Keeping an eye on your Redis server's performance is crucial for identifying bottlenecks, optimizing resource usage, and preventing outages.

INFO: The All-Seeing Eye

The Problem: You need a comprehensive overview of your Redis server's health and statistics. How much memory is it using? How many clients are connected? What's the cache hit rate?

The Solution: The INFO command provides a wealth of information and statistics about your server.

How it Works:

Running INFO in redis-cli will give you a detailed report. You can also request specific sections of the report.

bash
# Get all statistics
redis-cli INFO

# Get just the memory section
redis-cli INFO memory

# Get just the stats section
redis-cli INFO stats 

Key Metrics to Watch:

  • used_memory_human: How much memory Redis is currently using.
  • connected_clients: The number of clients connected to the server.
  • instantaneous_ops_per_sec: The number of commands being processed per second.
  • keyspace_hits and keyspace_misses: These can be used to calculate your cache hit ratio.
  • mem_fragmentation_ratio: A value greater than 1.5 might indicate memory fragmentation issues.

MONITOR: Real-Time Command Streaming

The Problem: You need to see exactly what commands are being sent to your Redis server in real-time. This can be incredibly useful for debugging.

The Solution: The MONITOR command streams back every command processed by the Redis server.

How it Works:

bash
redis-cli MONITOR 

You'll then see a live feed of all commands being executed.

Funny Exaggeration: Running MONITOR on a busy production server is like trying to drink from a fire hose. You'll be flooded with information!

Important Note: MONITOR can have a significant performance impact, so it should be used with caution in production environments. It's best for short debugging sessions.

SLOWLOG: Finding the Slowpokes

The Problem: Your application is experiencing latency, and you suspect that some Redis commands might be taking too long to execute.

The Solution: The Redis SLOWLOG is a system that logs queries that have exceeded a specified execution time.

How it Works:

The slow log is controlled by two configuration parameters:

  • slowlog-log-slower-than: The execution time in microseconds that a command must exceed to be logged. The default is 10,000 microseconds (10 milliseconds).
  • slowlog-max-len: The maximum number of entries in the slow log.

You can configure these in your redis.conf file or at runtime using CONFIG SET.

To view the slow log, you can use the SLOWLOG GET command:

bash
# Get the latest 10 entries from the slow log
redis-cli SLOWLOG GET 10 

Each slow log entry includes:

  • A unique ID.
  • The Unix timestamp of when the command was processed.
  • The execution time in microseconds.
  • The command and its arguments.
  • The client's IP address and port.
  • The client's name (if set).

The SLOWLOG is an invaluable tool for identifying and optimizing slow-running commands that could be impacting your application's performance.


Further Reading πŸ“š

Challenge

You are the lead engineer responsible for deploying a new Redis instance to production. Your task is to create a secure and observable configuration.

Part 1: redis.conf Configuration

Write the configuration directives that accomplish the following:

  1. Set the master password to p@ssw0rdS3cur3!.
  2. Restrict the server to only accept connections from the localhost interface.
  3. Completely disable the FLUSHALL command to prevent accidental data loss.
  4. Rename the DEBUG command to the obscure string DBG_c295d828.
  5. Configure the slow query log to capture any command that takes longer than 10 milliseconds (10000 microseconds) to execute.
  6. Limit the slow query log to a maximum of 128 entries.

Part 2: Access Control List (ACL) Commands

Below your redis.conf directives, write the Redis commands to set up the following users:

  1. Create a user named webapp.
    • The user must be enabled.
    • Set their password to apP@3wR!.
    • This user should only be allowed to access keys with the cache: prefix.
    • Grant permissions for the GET, SET, and EXPIRE commands only.
  2. Create a user named metrics-collector.
    • The user must be disabled by default.
    • Set their password to m3tr!csC0l1ect.
    • This user should only be allowed to access keys with the stats: prefix.
    • Grant read-only access by allowing only the GET, HGETALL, and KEYS commands.

Expected result

Your solution should contain two distinct parts: the redis.conf directives and the ACL commands.

Part 1: redis.conf Directives

The following lines correctly configure the server according to the requirements:

livecodeserver
requirepass p@ssw0rdS3cur3!
bind 127.0.0.1
rename-command FLUSHALL ""
rename-command DEBUG DBG_c295d828
slowlog-log-slower-than 10000
slowlog-max-len 128
  • requirepass sets the master password for authentication.
  • bind 127.0.0.1 restricts network access to localhost only.
  • rename-command FLUSHALL "" disables the command by renaming it to an empty string.
  • rename-command DEBUG DBG_c295d828 renames a dangerous command to an obscure, hard-to-guess string.
  • slowlog-log-slower-than is set to 10000 microseconds, which is equivalent to 10 milliseconds.
  • slowlog-max-len caps the number of entries in the slow query log.

Part 2: ACL Commands

The following ACL SETUSER commands create the users with the specified permissions:

routeros
ACL SETUSER webapp on >apP@3wR! ~cache:* +get +set +expire
ACL SETUSER metrics-collector off >m3tr!csC0l1ect ~stats:* +get +hgetall +keys
  • The webapp user is created, enabled (on), assigned a password (>apP@3wR!), restricted to the cache:* key pattern (~cache:*), and given access to specific commands (+get +set +expire).
  • The metrics-collector user is created, but is disabled by default (off), assigned its password, restricted to the stats:* key pattern, and given a set of read-only commands.
redis

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.