Typorithm · easy

Counters with INCR

redis-incrredisincratomiccounter
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Counters with INCR

0%

Strings & Keys

INCR and DECR atomically change an integer-string value by one, INCRBY by an arbitrary amount; because each operation is atomic they are safe for concurrent counters and rate limits, and a missing key is treated as 0.

Redis stores numbers as strings, but INCR views interprets that string as an integer and bumps it by 1, returning the new total. DECR does the same in reverse.

INCRBY views 10 adds an arbitrary amount in one step, and a matching DECRBY subtracts. All of these run as a single atomic operation on the server.

Atomicity is the key property: even with many clients hitting INCR at once, no increment is lost, which makes these commands ideal for page counters and rate limiters. If the key is missing, Redis treats it as 0 before applying the operation.

01

INCR/DECR change an integer by 1 atomically.

02

INCRBY adds an arbitrary amount N.

03

A missing key is treated as 0.

SET views 0
INCR views
INCRBY views 10
DECR views
GET views

Read the implementation

Connect each part of the code to the concept before you type.

1. Seed the counter

SET initializes the counter to the string "0"; INCR also treats a missing key as 0.

SET views 0

2. Add one

INCR atomically increments the integer value by 1 and returns the new total.

INCR views

3. Add N

INCRBY adds an arbitrary amount — here 10 — in a single atomic step.

INCRBY views 10

4. Subtract one

DECR atomically decrements the integer value by 1.

DECR views

5. Read the total

GET returns the counter's current value as a string.

GET views

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Counters with INCR: Lesson & Practice · Typorithm | ApplyDjinn