Skip to content

Instantly share code, notes, and snippets.

@ggoodman
Created February 22, 2012 17:58
Show Gist options
  • Save ggoodman/1886322 to your computer and use it in GitHub Desktop.
Save ggoodman/1886322 to your computer and use it in GitHub Desktop.
Proposed use-case for a redis alias / pointer type and operations.

My crappy understanding of how this would currently be done

Adding a new revision

Step 1: Get the current revision pointer

GET foo:1:latest_revision
// Returns foo:1:11

Step 2: Add the new revision and tweak the pointers

MULTI
RENAME foo:1:current foo:1:11 // This came from the first round-trip
SET foo:1:current_revision foo:1:12
SET foo:1:current "This is version 12 of foo:1"
EXEC

The problem here is that because of the two round-trips, I could find myself with an inconsistent state. Is there an atomic approach to the above?

Getching latest revision

More or less than the same as the ALIAS way.

GET foo:1:current

Disclaimer

  1. I'm sure this can be done in a single round-trip w/ scripting.
  2. I'm pretty much a redis beginner, but I try.

Proposed approach to using ALIAS

Adding a new revision

MULTI
SET foo:1:12 "This is version 12 of foo:1"
ASET foo:1:latest foo:1:12
EXEC
// Returns: foo:1:11 "This is version 11 of foo:1" (key, value) or blank if this is the first time the alias is used

Fetching latest revision

Again, single round-trip

AGET foo:1:latest
// Returns: "This is version 12 of foo:1"
@ggoodman
Copy link
Author

Just realized that a temporary key can be used to avoid the 2 round-trip issue in the current approach. Still think this syntax would make for some more elegant approaches to certain problems.

@antirez
Copy link

antirez commented Feb 22, 2012

with scripting you can invoke a Lua command that implements exactly what you want. And you can abstract this in your client code as an "ALIAS" meta-command. Redis scripting in 2.6 is really powerful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment