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

Production Redis

Persistence (RDB vs AOF), high availability, connect from Python and Node, deploy with Upstash.

Redis Persistence

RDB vs AOF
# RDB (Redis Database Backup)
# Snapshots: saves the dataset to disk at intervals
# Fast to restore, compact file, small performance impact
# Risk: lose data between last snapshot and crash

# AOF (Append Only File)
# Logs every write operation
# More durable, can recover to exact state before crash
# Larger file, slightly slower writes

# In redis.conf:
save 900 1       # RDB: save after 900s if 1 key changed
save 300 10      # save after 300s if 10 keys changed
appendonly yes   # enable AOF
appendfsync everysec  # fsync every second (good balance)
Python client
pip install redis

import redis

r = redis.Redis(
    host='localhost',
    port=6379,
    decode_responses=True  # auto-decode bytes to strings
)

r.set('key', 'value')
print(r.get('key'))  # 'value'

# Pipeline: batch commands (one round trip)
pipe = r.pipeline()
pipe.set('a', 1)
pipe.set('b', 2)
pipe.incr('a')
pipe.execute()
Upstash (serverless Redis)
# Upstash = fully managed Redis, serverless pricing
# Free tier: 10,000 commands/day
# No server to manage, works in Vercel/Netlify edge functions

# Get UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN
# from upstash.com

# Node.js
npm install @upstash/redis

import { Redis } from '@upstash/redis';
const redis = Redis.fromEnv();
await redis.set('key', 'value');
const val = await redis.get('key');
📝 Day 5 Exercise
Deploy Redis to Production
  1. S
  2. e
  3. t
  4. u
  5. p
  6. a
  7. f
  8. r
  9. e
  10. e
  11. U
  12. p
  13. s
  14. t
  15. a
  16. s
  17. h
  18. R
  19. e
  20. d
  21. i
  22. s
  23. i
  24. n
  25. s
  26. t
  27. a
  28. n
  29. c
  30. e
  31. .
  32. C
  33. o
  34. n
  35. n
  36. e
  37. c
  38. t
  39. t
  40. o
  41. i
  42. t
  43. f
  44. r
  45. o
  46. m
  47. y
  48. o
  49. u
  50. r
  51. N
  52. o
  53. d
  54. e
  55. .
  56. j
  57. s
  58. a
  59. p
  60. p
  61. .
  62. R
  63. e
  64. p
  65. l
  66. a
  67. c
  68. e
  69. y
  70. o
  71. u
  72. r
  73. l
  74. o
  75. c
  76. a
  77. l
  78. R
  79. e
  80. d
  81. i
  82. s
  83. U
  84. R
  85. L
  86. w
  87. i
  88. t
  89. h
  90. t
  91. h
  92. e
  93. U
  94. p
  95. s
  96. t
  97. a
  98. s
  99. h
  100. U
  101. R
  102. L
  103. .
  104. V
  105. e
  106. r
  107. i
  108. f
  109. y
  110. c
  111. a
  112. c
  113. h
  114. i
  115. n
  116. g
  117. s
  118. t
  119. i
  120. l
  121. l
  122. w
  123. o
  124. r
  125. k
  126. s
  127. .
  128. C
  129. h
  130. e
  131. c
  132. k
  133. t
  134. h
  135. e
  136. U
  137. p
  138. s
  139. t
  140. a
  141. s
  142. h
  143. d
  144. a
  145. s
  146. h
  147. b
  148. o
  149. a
  150. r
  151. d
  152. t
  153. o
  154. s
  155. e
  156. e
  157. c
  158. o
  159. m
  160. m
  161. a
  162. n
  163. d
  164. c
  165. o
  166. u
  167. n
  168. t
  169. s
  170. .

Day 5 Summary

  • RDB = snapshots (fast, compact, some data loss risk). AOF = full log (durable, larger).
  • Use both: AOF for durability, RDB for fast restarts.
  • Python client: redis.Redis(decode_responses=True) for string responses.
  • Upstash = managed Redis with per-request pricing. Perfect for serverless apps.
Finished this lesson?