Redis · Chapter 18 of 26

Lua scripts, several steps in one command

By the end of this chapter you can run a small Lua script inside Redis with EVAL, pass it keys and arguments, call Redis from it with redis.call and redis.pcall, load it once and run it by its hash with EVALSHA, define it as a command in ioredis, and say why a script is a better tool than WATCH for a check followed by a change. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis in production
  • 3 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Chapter 8 took the last spot of a contest with WATCH and MULTI: watch the count, read it, and run the change only if nobody touched the count in between. It is correct. It is also slow when everyone arrives at once, which is exactly when the last spots go. A test raced 30 fans, each on its own connection, for the last 3 spots of the Pune Fans League with that WATCH loop:

30 fans, 3 spots: 3 joined, 27 full
EXEC answered nil 82 times; 591 commands sent; 28.8 ms

The answer was right, but 82 transactions were thrown away and retried, and 591 commands went over the wire for 30 fans. Venkat has two more of these: a login code that must work exactly once (two phones sending the same OTP at the same moment must not both log in), and a limit on how often a fan can refresh the leaderboard. Each is a read, a decision and a write, and the commands you know can only do the decision in your code, between round trips, where another app server can slip in.

What Redis offers is to send it the decision too: a short Lua script that Redis runs, whole, as one command.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    verify-otp.js: an OTP that works once

    Export verifyOtp(redis, fanId, code). The fan's OTP is the string otp:<fanId>, with a TTL. If it holds exactly code, delete it and return true. If the code is wrong, leave the key and its TTL as they are and return false; with no OTP, return false.

    Do the compare and the delete in one script with EVAL: the key in KEYS, the code in ARGV. Send nothing else to Redis (the Check records every command your function sends, and expects one script per call). The Check also sends the right code twice at the same moment: exactly one of the two may return true.

  2. Task 2

    token-bucket.js: a token bucket for leaderboard refreshes

    Export takeToken(redis, fanId, now = Date.now()), now in milliseconds. Keep a bucket per fan in the hash bucket:<fanId>, with the tokens left and the time it was last updated. A new fan starts with 10 tokens. Each call first refills 1 token per 1000 ms since the last update (fractions count, never above 10), then takes 1 token if at least one whole token is there. The key expires 20000 ms after each call (the Check reads a PTTL from 1 to 20000).

    Return { allowed, tokens }: allowed true or false, and tokens the whole tokens left after this call, rounded down. 11 calls at the same now give tokens 9, 8, ... 0, 0 and only the 11th is refused; 2500 ms later three calls give { true, 1 }, { true, 0 }, { false, 0 }; an hour later only 10 of 12 calls are allowed. Another fan has a bucket of its own.

    All of it runs in one script per call, defined once with ioredis's defineCommand and called by its name, with now passed in ARGV (the Check expects to see EVALSHA, and no other command from your function).

  3. Task 3 · interview question

    Interview: join-contest.js, the last spots without WATCH

    Export joinContest(redis, contestId, fanId), using contest:<contestId>:spots_left (a number) and the set contest:<contestId>:fans. Return 'already' when the fan is already in the set (nothing changes), 'full' when spots_left is 0 or the key does not exist, and otherwise take one spot and add the fan, and return 'joined'.

    Do it in one script per call, with both keys in KEYS and the fan id in ARGV, and no WATCH or MULTI (the Check records what your function sends and reads the keys you pass). With 3 spots left in contest 75 and six fans at once, exactly three get 'joined' and three 'full', spots_left ends at '0' and the set holds exactly the fans told joined.

    Farhan will ask: Lua, or WATCH and MULTI, and why?

What you will learn

  • EVAL: run a Lua script, with KEYS and ARGV.
  • redis.call and redis.pcall: Redis commands from inside a script, and their errors.
  • Replies: how Lua values come back (numbers, false, nil).
  • A script runs whole: one command at a time again, so keep it short.
  • SCRIPT LOAD and EVALSHA: send a script once, run it by its hash.
  • defineCommand: a script as an ioredis method.

More in Redis in production

Build "Lua scripts, several steps in one command" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.