How to implement "trigger" for redis datastore?

You can use the Pub/Sub feature of Redis. It's exactly what you need given your circumstances as you described.

Essentially, you SUBSCRIBE to a "channel", and the other part of your application writes (PUBLISH) the value being changed to that channel. Your subscriber (consumer, the client that wants to know about the change) will get notified in virtually realtime.


Since Redis 2.8 (released 22 Nov 2013), there is now a feature called Keyspace Notifications which lets clients subscribe to special Pub/Sub channels for keyspace events, which you can use as a trigger on a certain key.

The feature is disabled by default because "while not very sensible the feature uses some CPU power." To enable, send a CONFIG SET command to configure the feature. For example, the following command will enable keyspace events for String commands:

> CONFIG SET notify-keyspace-events K$
OK

Next, use the regular pubsub SUBSCRIBE command to subscribe to the specially-named channel. For example, to listen to keyspace events on the mykey key in DB 0:

> SUBSCRIBE __keyspace@0__:mykey
Reading messages... (press Ctrl-C to quit)

Test out the feature by setting the key's value from another client:

> SET mykey myvalue
OK

You should receive a message in the subscribed client:

1) "message"
2) "__keyspace@0__:mykey"
3) "set"

After receiving the event, you can fetch the updated value and see if it satisfies the condition in your application code.