Authentication with requirepass in Redis - Time & Space Complexity
When Redis requires a password, it checks the password each time a client connects. Understanding how this check grows with more clients helps us see if it slows down as more people connect.
We want to know: How does the time to verify a password change as more clients try to connect?
Analyze the time complexity of the following Redis authentication commands.
CONFIG SET requirepass mysecret
# Client tries to authenticate
AUTH mysecret
# Server checks password
# If correct, client is allowed commands
# If wrong, connection is rejected
This snippet sets a password and shows how clients authenticate by sending the password to Redis, which checks it before allowing commands.
Look for repeated actions that affect time.
- Primary operation: Password comparison for each client authentication.
- How many times: Once per client connection that sends AUTH.
Each time a client connects and sends a password, Redis compares it to the stored password.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 clients | 10 password checks |
| 100 clients | 100 password checks |
| 1000 clients | 1000 password checks |
Pattern observation: The number of password checks grows directly with the number of clients trying to authenticate.
Time Complexity: O(n)
This means the total time to handle authentication grows in a straight line as more clients connect and authenticate.
[X] Wrong: "Checking the password takes longer as the password gets longer or more clients connect at once."
[OK] Correct: Redis compares the password in a simple way that takes about the same time regardless of password length, and each check is independent per client, so more clients just add more checks, not slower checks.
Understanding how authentication scales helps you explain how systems stay fast even when many users connect. This skill shows you can think about real-world system behavior clearly.
"What if Redis stored multiple passwords for different users? How would the time complexity of authentication change?"
Practice
requirepass directive do in Redis?Solution
Step 1: Understand the purpose of requirepass
Therequirepassdirective is used to protect Redis by requiring clients to authenticate with a password.Step 2: Compare options with this purpose
Only It sets a password that clients must use to authenticate before accessing Redis. describes setting a password for client authentication, matching the purpose ofrequirepass.Final Answer:
It sets a password that clients must use to authenticate before accessing Redis. -> Option AQuick Check:
requirepass = password protection [OK]
- Thinking requirepass disables connections
- Confusing requirepass with encryption
- Assuming requirepass limits client count
requirepass is set?Solution
Step 1: Identify the Redis command for authentication
Redis uses theAUTHcommand followed by the password to authenticate clients.Step 2: Check the options for the AUTH command
Only AUTH mypassword uses the correctAUTHcommand syntax.Final Answer:
AUTH mypassword -> Option AQuick Check:
AUTH command = authenticate password [OK]
- Using LOGIN instead of AUTH
- Using CONNECT or PASSWORD commands which don't exist
- Forgetting to include the password after AUTH
requirepass foobar set, what will be the output of the following commands sequence?
SET key1 value1 GET key1 AUTH foobar GET key1
Solution
Step 1: Understand requirepass effect on commands
Whenrequirepassis set, all commands except AUTH fail with NOAUTH error until the client authenticates.Step 2: Analyze command sequence
SET key1 value1 fails with NOAUTH (unauthenticated). GET key1 fails with NOAUTH. AUTH foobar succeeds (OK). GET key1 returns (nil) because the key was never successfully set.Final Answer:
(error) NOAUTH Authentication required. (error) NOAUTH Authentication required. OK (nil) -> Option DQuick Check:
Pre-AUTH: NOAUTH errors; post-AUTH: (nil) since key unset [OK]
- Assuming commands work before AUTH
- Thinking AUTH returns error
- Confusing order of outputs
requirepass mysecret in your Redis config. A client tries to run AUTH wrongpass followed by GET key. What is the problem and how to fix it?Solution
Step 1: Identify the error cause
Using a wrong password with AUTH means client is not authenticated, so GET command will fail with NOAUTH error.Step 2: Correct the authentication process
Client must provide the correct password with AUTH to authenticate and access commands like GET.Final Answer:
The client used wrong password; fix by using correct password with AUTH. -> Option BQuick Check:
Wrong password = no access; correct password fixes it [OK]
- Trying commands before successful AUTH
- Thinking requirepass disables commands permanently
- Restarting server unnecessarily
newpass123 dynamically?Solution
Step 1: Understand how to change requirepass dynamically
Redis allows changing configuration parameters at runtime usingCONFIG SETcommand.Step 2: Identify correct command to change password
To change the password, useCONFIG SET requirepass newpass123. AUTH is for client login, SET is for keys, CONFIG GET only reads.Final Answer:
CONFIG SET requirepass newpass123 -> Option CQuick Check:
CONFIG SET changes config without restart [OK]
- Using AUTH to change password
- Trying SET command for config
- Using CONFIG GET which only reads
Ready to prove it?
Pass the test and this pattern moves unknown
