Redis · Chapter 8 of 26

Transactions

By the end of this chapter you can make several changes in Redis run together, one after another with nothing in between, say what happens when one of them fails, and make a change give up when another app server got there first. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis from the command line
  • 4 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

Joining a contest is four changes at GullyXI: the entry fee comes out of the fan's wallet, it goes into the contest's fees, the contest has one spot fewer, and the fan is added to its fans. Venkat needs all four or none: a fan charged but not in the contest is a refund and an angry message, and a fan in the contest without paying is money lost.

Last night one join went wrong in exactly that way. Hemant (u1180) was charged 99 rupees for the Pune Fans League, the fee was counted and his spot was taken, but he was never added to the fans.

And tonight Rhea has a harder one: Winner Takes All has 10 spots, 9 are taken, and two app servers are each selling the last spot to a different fan at the same moment. With the commands you know, each server reads "1 spot left", each says yes, and the contest ends with 11 fans in 10 spots. Chapter 1's INCR fixed a lost count because it was one command. Here the work is four commands and a decision, and Redis needs a way to keep them together.

What you build: 4 tasks

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

  1. Task 1

    join.redis: join a contest in one transaction

    Mohit (u1400) joins the Mega Contest, entry fee 4900 paise. Make four changes together in one transaction (MULTI, the four commands, EXEC): take 4900 from the field balance of the hash wallet:u1400 (20000 to 15100), add 4900 to the counter contest:71:fees (1176000 to 1180900), take 1 from the counter contest:71:spots_left (60 to 59), and add the member u1400 to the set contest:71:fans.

    All four changes must be queued between MULTI and EXEC, and EXEC must answer their four replies with no error among them. The Check makes the wallet and the two counters first; setup.redis makes the same in your own Redis.

  2. Task 2

    wrong-key.redis: a join that charged but did not join

    wrong-key.redis is the app's join, for Nitin (u1250) and the Pune Fans League (entry fee 9900 paise): one transaction of four commands. It is what charged Hemant last night without adding him. Predict what EXEC answers before you run it, then run setup.redis and the file and read EXEC's four replies.

    Fix the one wrong command so that all four changes work, in the same transaction: wallet:u1250's balance 30000 to 20100, contest:74:fees 178200 to 188100, contest:74:spots_left 2 to 1, and u1250 added to the set contest:74:fans (17 fans to 18).

    EXEC must answer no error. The contest's own hash, contest:74 (its name, entry_fee and spots fields), must stay as it is, and wallet:u1180 is not touched.

  3. Task 3

    watched-join.redis: the last spot, watched

    Winner Takes All has one spot left (contest:75:spots_left is 1, nine fans in contest:75:fans, contest:75:fees is 179100). Join Ravi (u1390, entry fee 19900 paise) as in Task 1: take 19900 from wallet:u1390's balance, add 19900 to contest:75:fees, take 1 from contest:75:spots_left, and add u1390 to contest:75:fans, all four in one MULTI and EXEC.

    Before the transaction, WATCH contest:75:spots_left and read it, so the transaction gives up if anyone changes the spots first.

    The Check runs your file twice. First, another app server sells the spot to u1229 right after your WATCH: your EXEC must answer nil, the spots end at 0, Ravi's balance stays 50000 and he is not in the set. Then with nobody else: EXEC runs, the spots end at 0, Ravi's balance is 30100, the fees are 199000 and u1390 is in the set.

  4. Task 4 · interview question

    Interview: undo.redis, undo last night's half join

    Last night's broken join charged Hemant (u1180) and counted his fee and spot, but never added him to contest:74:fans. Venkat wants it undone in one transaction: give 9900 back to wallet:u1180's balance (15100 to 25000), take 9900 from contest:74:fees (178200 to 168300), and give the spot back to contest:74:spots_left (2 to 3).

    undo.redis is Gurpreet's first try: EXEC refuses it and nothing is undone. Fix it so all three changes are queued between MULTI and EXEC and EXEC answers no error. Hemant must stay out of contest:74:fans, and the hash contest:74 must stay as it is.

    Farhan will ask: in Task 2 the other commands ran despite the error, but here nothing ran. Why are the two errors treated differently, and why does Redis never roll back?

What you will learn

  • MULTI and EXEC: queue commands and run them together, with nothing in between.
  • QUEUED and EXEC's replies: what Redis answers while you queue, and after.
  • DISCARD: throw a queued transaction away.
  • Errors in a transaction: a mistake while queueing cancels it all (EXECABORT); a failure while running does not stop the rest.
  • No rollback: why Redis never undoes the commands that ran.
  • WATCH and UNWATCH: give up when someone else changed a key first.

More in Redis from the command line

Build "Transactions" 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.