The Desi Architect
Notes / Docker Sandboxes / Lua in Redis: Atomic Scripts Explained from Zero
Docker Sandboxes · Tier 0: Start Here · Lab 07

Lua in Redis: Atomic Scripts Explained from Zero

What is Lua, why does Redis run it, and how do you read a script? Learn the ten pieces of Lua you need, watch a coupon get oversold when a check and a write are two commands, fix it with one script, and see what a slow script does to everyone else.

Time40 minutes
StackRedis 7Ports 16411
MemoryAbout 15 MB
Before thisLab 06Redis Basics
Verified runWindows, Mac, LinuxBash and PowerShell
  1. 1PrepareWhat you will learn
  2. 2PredictGuess before you run
  3. 3RunStep by step
  4. 4ObserveWhat you should see
  5. 5Break itChange one thing
  6. 6Clean upLeave nothing running
On this page
  1. 00 / What You Will Learn
  2. 01 / Why It Matters
  3. 02 / What We Are Building
  4. 03 / Words You Need
  5. 04 / Predict First
  6. 05 / Step by Step
  7. 06 / What You Should See
  8. 07 / Look Inside
  9. 08 / Break It, Fix It
  10. 09 / In Real Projects
  11. 10 / Interview Corner
  12. 11 / Clean Up

00 /What You Will Learn

By the end you will be able to
  • Say what Lua is and why Redis has it built in.
  • Read and write a short script using local, if, for, tables and ...
  • Use EVAL with KEYS and ARGV, and redis.call to run Redis commands inside a script.
  • Reproduce an oversell bug caused by "check, then act" as two commands, and fix it with one script.
  • Explain EVALSHA, the no-rollback rule, and why a slow script blocks everyone.

Before you start

Finish Lab 06 (Redis Basics) first, or be comfortable with SET, GET, INCR and DECR. You do not need to know Lua. This page teaches the small part you need.