Day 1 of 5
⏱ ~60 minutes
Redis in 5 Days — Day 1

Redis Data Types

Install Redis, use redis-cli, and work with all five core data types.

Redis Is Not a Cache. It's a Data Structure Server.

Most developers think of Redis as a cache. It is one, but it's also much more. Redis keeps all data in memory (fast) and persists to disk (durable). It has five data structures that solve specific problems elegantly.

Install and connect
# Mac: brew install redis
# Linux: sudo apt install redis
redis-server &
redis-cli
Strings
SET name 'Alice'
GET name

SET counter 0
INCR counter         # atomic increment → 1
INCRBY counter 5     # → 6
DECR counter         # → 5

SET token 'abc123' EX 3600   # expires in 1 hour
TTL token                     # time remaining (seconds)
PERSIST token                 # remove expiration
Hashes (like a row in a table)
HSET user:1 name Alice email [email protected] age 28
HGET user:1 name
HGETALL user:1
HMSET user:2 name Bob email [email protected]
HDEL user:1 age
HEXISTS user:1 email
Lists, Sets, Sorted Sets
# Lists (ordered, allows duplicates)
LPUSH mylist 'a' 'b' 'c'    # push to head
RPUSH mylist 'd'             # push to tail
LRANGE mylist 0 -1           # get all
LPOP mylist                  # pop from head

# Sets (unordered, unique members)
SADD tags python javascript rust
SMEMBERS tags
SISMEMBER tags python        # 1 if member
SCARD tags                   # count

# Sorted Sets (score + member)
ZADD leaderboard 100 alice 85 bob 92 carol
ZRANGE leaderboard 0 -1 WITHSCORES
ZRANK leaderboard alice      # 0-based rank
ZREVRANK leaderboard alice   # rank from top
📝 Day 1 Exercise
Build a Leaderboard
  1. U
  2. s
  3. e
  4. a
  5. s
  6. o
  7. r
  8. t
  9. e
  10. d
  11. s
  12. e
  13. t
  14. t
  15. o
  16. b
  17. u
  18. i
  19. l
  20. d
  21. a
  22. g
  23. a
  24. m
  25. e
  26. l
  27. e
  28. a
  29. d
  30. e
  31. r
  32. b
  33. o
  34. a
  35. r
  36. d
  37. .
  38. A
  39. d
  40. d
  41. 1
  42. 0
  43. p
  44. l
  45. a
  46. y
  47. e
  48. r
  49. s
  50. w
  51. i
  52. t
  53. h
  54. s
  55. c
  56. o
  57. r
  58. e
  59. s
  60. .
  61. F
  62. e
  63. t
  64. c
  65. h
  66. t
  67. h
  68. e
  69. t
  70. o
  71. p
  72. 3
  73. .
  74. U
  75. p
  76. d
  77. a
  78. t
  79. e
  80. a
  81. p
  82. l
  83. a
  84. y
  85. e
  86. r
  87. '
  88. s
  89. s
  90. c
  91. o
  92. r
  93. e
  94. .
  95. G
  96. e
  97. t
  98. t
  99. h
  100. e
  101. r
  102. a
  103. n
  104. k
  105. o
  106. f
  107. a
  108. s
  109. p
  110. e
  111. c
  112. i
  113. f
  114. i
  115. c
  116. p
  117. l
  118. a
  119. y
  120. e
  121. r
  122. .
  123. S
  124. t
  125. o
  126. r
  127. e
  128. e
  129. a
  130. c
  131. h
  132. p
  133. l
  134. a
  135. y
  136. e
  137. r
  138. '
  139. s
  140. p
  141. r
  142. o
  143. f
  144. i
  145. l
  146. e
  147. i
  148. n
  149. a
  150. h
  151. a
  152. s
  153. h
  154. .

Day 1 Summary

  • Strings: SET/GET/INCR/TTL. Set expiration with EX. Good for simple values, counters, tokens.
  • Hashes: store object fields. HSET/HGET/HGETALL. One key, many fields.
  • Lists: ordered, allows duplicates. LPUSH/RPUSH/LRANGE. Good for queues and timelines.
  • Sorted Sets: members with scores. ZADD/ZRANGE/ZRANK. Perfect for leaderboards and rate limiting.
Finished this lesson?