ACL rules and categories in Redis - Time & Space Complexity
When working with Redis ACL rules and categories, it's important to understand how the time to check permissions grows as the number of rules increases.
We want to know how Redis handles checking many ACL rules and what affects the speed.
Analyze the time complexity of checking ACL rules for a command.
ACL SETUSER alice on >password ~* +@all
ACL SETUSER bob on >password ~cache:* +@read
ACL LIST
ACL GETUSER alice
ACL CAT
This snippet shows setting users with ACL rules, listing them, and retrieving categories.
Look at what repeats when Redis checks ACL rules.
- Primary operation: Checking each ACL rule or category against the command or key pattern.
- How many times: Once per rule or category during permission checks.
As the number of ACL rules or categories grows, Redis checks more rules to find matches.
| Input Size (number of rules) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rules.
Time Complexity: O(n)
This means the time to check permissions grows in a straight line as you add more ACL rules.
[X] Wrong: "Checking ACL rules is instant no matter how many rules exist."
[OK] Correct: Each rule must be checked to see if it applies, so more rules mean more work.
Understanding how ACL rule checks scale helps you explain system behavior and design efficient permission checks in real projects.
"What if Redis used a hash map to store ACL rules by command? How would the time complexity change?"
Practice
Solution
Step 1: Understand ACL rules function
ACL rules define what commands and keys a user can access in Redis.Step 2: Compare options with ACL purpose
Only To control user access to commands and keys matches the control of user access, others describe unrelated features.Final Answer:
To control user access to commands and keys -> Option AQuick Check:
ACL rules = control access [OK]
- Confusing ACL rules with data storage
- Thinking ACL rules optimize performance
- Mixing ACL with backup features
Solution
Step 1: Recall Redis ACL command for user creation
The correct command to create or update a user isACL SETUSER.Step 2: Check each option's command
Only ACL SETUSER alice on >password ~* +@all usesACL SETUSER, others use invalid commands.Final Answer:
ACL SETUSER alice on >password ~* +@all -> Option AQuick Check:
Use ACL SETUSER to add/update users [OK]
- Using ACL ADDUSER instead of ACL SETUSER
- Using ACL CREATEUSER or ACL NEWUSER which don't exist
- Confusing command names
ACL SETUSER bob on >mypassword ~cache:* +@read, which commands can user 'bob' execute?Solution
Step 1: Analyze the ACL rule components
User 'bob' is enabled (on), password set, keys limited to pattern 'cache:*', and allowed commands are in category '@read'.Step 2: Determine allowed commands and keys
Only read commands (+@read) on keys matching 'cache:*' are allowed.Final Answer:
Only read commands on keys starting with 'cache:' -> Option CQuick Check:
ACL keys + command categories limit access [OK]
- Assuming all commands are allowed
- Ignoring key pattern restrictions
- Thinking user is disabled
ACL SETUSER alice on >pass123 ~* +@write but Alice cannot execute SET commands. What is the likely issue?Solution
Step 1: Verify user status and rules
User 'alice' is enabled (on), password set, keys unrestricted (~*), commands allowed in @write category.Step 2: Check if SET is in @write category
SET command belongs to @string category, not @write, so permission is missing.Final Answer:
The command category @write does not include SET -> Option BQuick Check:
Command categories must include needed commands [OK]
- Assuming @write includes all write commands
- Thinking key pattern restricts commands
- Ignoring user enabled status
Solution
Step 1: Identify key pattern restriction
User must access keys starting with 'cache:', so pattern ~cache:* is needed.Step 2: Select command categories for caching
Commands related to caching are in @cache and key management in @keyspace categories.Step 3: Evaluate options
ACL SETUSER cacheAdmin on >secret ~cache:* +@cache +@keyspace correctly combines key pattern and relevant categories; others either allow too much or wrong keys.Final Answer:
ACL SETUSER cacheAdmin on >secret ~cache:* +@cache +@keyspace -> Option DQuick Check:
Use key pattern + relevant categories for precise access [OK]
- Using ~* allows all keys, not just cache keys
- Using +@all grants too many permissions
- Missing key pattern restriction
Ready to prove it?
Pass the test and this pattern moves unknown
