Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default value for KeyspaceEventMessageListener on keyspace event notifications #2671

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jxblum
Copy link
Contributor

@jxblum jxblum commented Aug 10, 2023

Closes #2670

NOTE: the next effect of theses changes is preserving the existing behavior.

While the default value for notifiy-keyspace-events in the abstract class KeyspaceEventMessageListener changed from "EA" to "empty" in this PR, it is an abstract class in the first place, and users must extend the class with a custom implementation in any case. But, I have reluctantly let the default value for the 1 and only Spring Data Redis provided implementation, KeyExpirationEventMessageListener, to "Ex", thereby preserving the existing behavior, i.e. to notify on key expiration events.

This change came about because of #2654, which occurs for 2 reasons.

  1. First, the default Redis server configuration setting for notify-keyspace-events is "empty" (or, in other words, disabled). Therefore, on Redis server restarts, any configuration supplied at runtime, such as is the case with a Spring Data Redis application when the KeyExpirationEventMessageListener (or any KeyspaceEventMessageListener) is registered in the Spring ApplicationContext, gets lost! This is even the case for the redis-cli.

  2. In addition, any configuration coming from a Spring Data Redis application when a KeyspaceEventMessageListener (such as KeyExpirationEventMessageListener) is declared and registered in the Spring container, only happens once, on initialization. So, when the Redis server restarted, again this configuration is lost!

The user should be specifying Redis server (infrastructure) configuration in redis.conf, which can then also be managed in version control along with the application.

…notifications.

Users must now explicitly call setKeyspaceNotificationsConfigParameter(:String) to a valid redis.config, notify-keyspace-events value to enable Redis keyspace notifications. This aligns with the Redis servers default setting for notify-keyspace-events in redis.conf, which is disabled by default.

Additionally, the default value for KeyExpirationEventMessageListener has been changed to 'Ex', for development-time convenience only.

However, users should be aware that any notify-keyspace-events configuration only applies once on Spring container initialization and any Redis server reboot will not remember dynamic configuration modifications applied at runtime. Therefore, it is recommended that users applly infrastructure-related configuration changes directly to redis.conf.

Closes #2670
@Lvbuqing
Copy link

Thank you for your help,
I read your PR carefully:
98d25f1

As you said:
Why this configuration for the Redis server was even added to Spring Data Redis (i.e. the KeyExpirationEventMessageListener component) to begin with...  
mp911de also said:
this is both an infrastructure (i.e. "data management policy") and application concern. The framework definitely should not be deciding in this case, no matter how "convenient".

I have a few questions about this PR:

  1. EA is deleted here
    image
    Why is it changed to Ex here?
    image

  2. According to the last discussion, in my opinion, we should not configure related properties for Redis, for example, we should not set related values through spring-data-redis,as shown in the figure
    image

keyspaceNotificationsConfigParameter should be used as an expected value, not directly set

@jxblum
Copy link
Contributor Author

jxblum commented Aug 11, 2023

The only reason I set the value to a "Ex" in KeyExpirationEventMessageListener is to preserve existing behavior in the 3.x line. In 4.0, I think this should also be set this value to "empty", even for this listener.

However, I did set the value for notify-keyspace-events in the abstract KeyspaceEventMessageListener to "empty", thereby disabling notifications on all key(space) events.

@christophstrobl
Copy link
Member

thanks for picking up this topic. I agree that, though convenient, the Redis config should not be altered by the message listener. However, I'd rather not change defaults in a minor but wait for the next major release.
IIRC there's MappingExpirationListener in KeyValueAdapter used in conjunction with TTL, that would interact with the server config, if EnableKeyspaceEvents is set to ON_STARTUP/ON_DEMAND. It's OFF by default in the adapter and EnableRedisRepositories.

@jxblum
Copy link
Contributor Author

jxblum commented Aug 15, 2023

@christophstrobl - Thank you for your feedback.

Correct me if I am wrong, but it seems that 1) the RedisKeyValueAdapter.MessageExpirationListener extends from the SD Redis KeyExpirationEventMessageListener class directly where I kept, though declared a (minimal) value for expiration (i.e. Ex rather than EA; FYR: definitions of E and x) which seems to be all that is required to receive key expiration events.

Perhaps EA is overkill since A is g$lshzxetd (all this as documented in redis.conf), which would only increase the amount of network traffic when pub/sub is enabled when using the SD Redis Repository infrastructure since then Repositories would receive notifications for all events when we are only seemingly interested in expiration events (hence "MappingExpirationListener" as named).

Even the code suggests we filtering for "expiration-only" events, though the logic (and then this) is very cryptic to me. The logic only seems to be checking for the presence of a valid "key" (i.e. keyspace:id) in the message rather than whether the message is really an "expiration" event. That is, I am not sure if other events (e.g. on ADD or DEL) would also contain the key, and if so, what really constitutes an "expiration" message.

@jxblum jxblum changed the title Change default value for KeyspaceEventMessageListener keyspace event notifications Change default value for KeyspaceEventMessageListener on keyspace event notifications Aug 15, 2023
@christophstrobl
Copy link
Member

Thanks for digging up the documentation, being well aware of the config parameters, my point was about changing defaults in a minor, given there are ways to configure it from outside.

@jxblum
Copy link
Contributor Author

jxblum commented Aug 16, 2023

@christophstrobl - I understood your concern and your point. I was also trying to explain that we are not really changing behavior here since we are still receiving "expiration" events with this configuration.

Ideally, Redis clients are not sending any configuration to the servers, otherwise we are going to (possibly) run into situations exactly like this.

@jxblum
Copy link
Contributor Author

jxblum commented Aug 16, 2023

@christophstrobl - I also stumbled upon and noticed this configuration in SD (Redis) Repositories today (i.e. Ex).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:messaging Redis messaging components type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change default value for KeyspaceEventMessageListener on keyspace event notifications
3 participants