Perform atomic and non-atomic multi-key operations
mainValkey Cluster supports multi-key commands (like mget and mset) under two conditions:
- Atomic Operations: Use standard methods like
mgetormsetwhen all keys are guaranteed to be in the same hash slot (e.g., by using hash tags like{foo}1and{foo}2). If keys belong to different slots, aValkeyClusterExceptionis raised. - Non-atomic Operations: Use
mget_nonatomicormset_nonatomicto handle keys across different slots. The client batches keys by their hash value and sends separate commands to the respective slot owners. This is not an atomic operation.
To force keys into the same slot, use hash tags in the key names.
# Atomic operations (requires hash tags for same slot)
rc.mset({'{foo}1': 'bar1', '{foo}2': 'bar2'})
rc.mget('{foo}1', '{foo}2')
# Non-atomic operations (keys can be in different slots)
rc.mset_nonatomic({'foo': 'value1', 'bar': 'value2', 'zzz': 'value3'})
rc.mget_nonatomic('foo', 'bar', 'zzz')