Skip to content

Command to wait until a key exists or doesn't exist #2072

Closed

Description

@eode

A command to wait until a key exists or doesn't exist would be nice -- like:

Wait until a key exists:
BKEY somekey 1 # blocks until somekey exists

Wait until a key doesn't exist anymore:
BKEY somekey 0 # blocks until somekey doesn't exist

If waiting for a list to exist, the blocking pop commands can be used, but for other keys, or if waiting for a list to not exist, there's currently no solution other than polling (possibly via a lua script).

Activity

  1. changed the title [-]Command(s) to wait until a key exists or doesn't exist[/-] [+]Command to wait until a key exists or doesn't exist[/+] on Oct 15, 2014
  2. mattsta commented on Oct 15, 2014

    @mattsta
    Contributor

    That's an interesting idea.

    We currently only process lists, but the initial "new list exists" functionality is centralized in one place now: https://github.com/antirez/redis/blob/38a5db6c9ac21812e4942d7016e4e58429f9ab09/src/db.c#L98
    The follow-up result sending is more complicated, but that's because it returns a value and modifies the list too.

    It looks like you're proposing the client not get any value from the key, but just a "ok: key now exists (or was deleted)" notification.

    The reason blocking operations on lists are so nice is because they do three things: a.) wake up only one client, b.) return a value and c.) remove a value from the list. Since BKEY wouldn't be returning any values, would you wake up all clients waiting for the key or only one client?

    Though, for regular keys, there's always the simple EXISTS command too. Many times it's easier to just poll Redis directly since you're not likely to impact performance even if you want to poll at 10 Hz per key.

    So... in the end, it would be a combination of delete-only keyspace notifications for specific keys and a light pub/sub way of communicating something now exists (with currently unspecified "single receiver" or "all blocked watchers receive notification" behavior).

    It could make Redis a little more real-timey while removing the need for some client-side coding for special cases.

  3. eode commented on Jan 9, 2015

    @eode
    Author

    Yes, I'd say that what I suggested is pretty much covered by existing commands.

    The scenario is that I have an 'acting' client and a 'reacting' client. The acting client doesn't know which keys the reacting client is interested in. I suppose I could just do this for single-client behavior:

    On the reacting client:
    BRPOP keyname-watch
    (do stuff with the hash table at keyname)

    On the acting client:
    HMSET keyname (values)
    RPUSH keyname-watch junkdata

    ..or, for multi-client behavior:
    Reacting Client:
    SUBSCRIBE keyname-watch
    (wait for activity, then UNSUBSCRIBE and do something with hash table at keyname)

    Acting client:
    HMSET keyname (values)
    PUBLISH keyname-watch junkdata

    I was fairly new to Redis at the time of this post, and hadn't thought of using a dummy list when I posted this, nor had I thought of using a key-specific pub/sub. I wanted to avoid the bandwidth usage of broad publish/subscribe channels and filtering on the client-side for the desired key. Overhead of tracking it with a secondary key via list or pub/sub instead of a single command is negligible to say the least.

    However, if you did ever feel like doing it, I'd suggest a change from my original semantics to:
    BKEY 1 key1 key2... (returns with existing keys as soon as any key in 'keys' exists)
    BKEY 0 key1 key2... (returns with nonexistent keys as soon as a key in 'keys' doesn't exist)
    BKEY -1 key1 key2... (returns with modified keys as soon as any key watched is modified in any way -- useful?)

    But again, I think existing functionality covers the needs.

    Edit: Removed gt/lt symbols interfering with display

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions