Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Lists  ›  Capstone Project

LMOVE and More List Commands

LMOVE 12 minLMOVE pops an item from one end of a list and pushes it onto an end of another in one step, and LTRIM keeps only a chosen range so a list cannot grow forever.

LMOVE and More List Commands

A Wordcraft worker takes "send match summary email" jobs from a list. If it pops a job and then crashes before finishing, that job is gone for good: it left the list and lived only in the dead worker's memory. Moving the job to a second list in the same step as taking it fixes that, and a couple of other commands keep lists from growing without limit.

The rule

LMOVE takes from one end and puts on another, all in one step.

LMOVE source destination LEFT|RIGHT LEFT|RIGHT pops an item from the chosen end of source, pushes it onto the chosen end of destination, and returns the item. The first direction is where to take from, the second is where to put. If source is empty the reply is (nil) and nothing moves.

127.0.0.1:6379> RPUSH jobs:pending mail:1 mail:2 mail:3
(integer) 3
127.0.0.1:6379> LMOVE jobs:pending jobs:working LEFT RIGHT
"mail:1"
127.0.0.1:6379> LRANGE jobs:working 0 -1
1) "mail:1"
127.0.0.1:6379> LREM jobs:working 1 mail:1
(integer) 1

Because the move is a single command, the job is always in exactly one of the two lists, even if the worker crashes. When the work is done, LREM deletes it from jobs:working; anything left there after a crash can be found and retried. BLMOVE is the blocking version that waits for a job to appear. (Older code uses RPOPLPUSH, which is the same as LMOVE ... RIGHT LEFT and is now deprecated.)

Using the same list as source and destination rotates it: LMOVE players players LEFT RIGHT moves the head to the tail, handy for taking turns.

Other everyday list commands:

  • LTRIM key start stop keeps only that range and deletes the rest, replying OK. After each LPUSH to a feed, LTRIM feed:asha 0 99 caps it at the newest 100 entries.
  • LREM key count item removes up to count copies of item from the head side (0 means all of them) and replies with how many it removed.
  • LSET key index item overwrites one position; LINSERT key BEFORE|AFTER pivot item inserts next to an existing item.

The shortcut

Read LMOVE as a sentence: "take from the (first word) of source, put on the (second word) of destination."

LMOVE players players LEFT RIGHTStep 1 of 4
  1. Start

    players holds asha, ravi, meera. Asha has just taken her turn.

Drills

Answer fast — trust the rule.

quick drill

a is x, y, z and b is empty. After LMOVE a b RIGHT LEFT, what does b hold?

quick drill

jobs:pending is empty. What does LMOVE jobs:pending jobs:working LEFT RIGHT reply?

quick drill

feed:asha has 250 items. After LTRIM feed:asha 0 99, what does LLEN feed:asha reply?

quick drill

Why is LMOVE safer than LPOP followed by RPUSH for handing a job to a worker?

Remember

  • LMOVE takes from one end and puts on another, all in one step.
  • Read it as "take from the first word, put on the second".
  • LTRIM caps a list; LREM clears a finished job from the working list.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainNot losing a job

A Wordcraft worker takes jobs from jobs:pending. Explain why LMOVE jobs:pending jobs:working LEFT RIGHT is safer than LPOP followed by RPUSH, and what the worker should do when the job is finished.