0
0
Redisquery~10 mins

Counter pattern in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Counter pattern
Start
Check if counter key exists
Yes
Increment counter by 1
Return new counter value
End
No
Create counter key with value 1
Return 1
End
The counter pattern checks if a key exists in Redis, increments it if yes, or creates it with 1 if no, then returns the current count.
Execution Sample
Redis
INCR page_views
INCR page_views
GET page_views
This code increments the 'page_views' counter twice and then retrieves its current value.
Execution Table
StepCommandKey Exists?ActionCounter Value After
1INCR page_viewsNoCreate 'page_views' with 11
2INCR page_viewsYesIncrement 'page_views' by 12
3GET page_viewsYesRetrieve 'page_views' value2
💡 After step 3, the counter value is 2, showing two increments.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
page_viewsnull122
Key Moments - 2 Insights
Why does the first INCR command create the key instead of failing?
In Redis, the INCR command automatically creates the key with value 1 if it does not exist, as shown in execution_table step 1.
What happens if we GET the counter before any INCR commands?
If the key does not exist, GET returns null or nil, meaning no value is set yet, unlike INCR which initializes it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the counter value after the first INCR command?
Anull
B0
C1
D2
💡 Hint
Check the 'Counter Value After' column in row 1 of the execution_table.
At which step does the counter key first exist in Redis?
AStep 3
BStep 1
CStep 2
DIt never exists
💡 Hint
Look at the 'Key Exists?' column; it changes from No to Yes after step 1.
If we run INCR three times instead of two, what would be the counter value after step 3?
A3
B2
C1
D4
💡 Hint
Each INCR adds 1; see variable_tracker for increments after each step.
Concept Snapshot
Counter pattern in Redis:
- Use INCR key to increment a counter.
- If key doesn't exist, INCR creates it with 1.
- GET key retrieves current count.
- Useful for tracking counts like page views.
- Simple, atomic, and efficient increment operation.
Full Transcript
The Redis counter pattern uses the INCR command to increase a numeric value stored at a key. If the key does not exist, INCR creates it with the value 1 automatically. This pattern is useful for counting events like page views or clicks. The example increments the 'page_views' counter twice and then retrieves its value, which ends up as 2. The execution table shows each step's command, whether the key existed, the action taken, and the counter's value after the step. The variable tracker follows the 'page_views' value from null to 1, then 2. Key moments clarify why INCR creates the key and what happens if GET is called before any increments. The visual quiz tests understanding of the counter value after increments and when the key exists. This pattern is simple and efficient for counting in Redis.