Is there any support for transactions in Redis Cluster python client? Obviously the keys participating in the transaction are mapped to the same slot using Hash Tags
For example: how to perform following commands atomically?
redis.delete("key1.{123}")
redis.set("key2.{123}", 1)
You can use pipelines with transaction=True (default)
pipe = redis.pipeline(transaction=True)
pipe.delete("key1.{123}")
pipe.set("key2.{123}", 1)
pipe.execute()
Related
When using multiple databases on a single redis instance and its memory is full, when I'm trying to insert new data it samples a number of keys and it applies an algorithm to them to determine which ones should be evicted.
But, if I'm using db0 and db1 and I'm trying to insert a new record into db1, will redis sample keys from the same database or does it sample them globally?
When it does eviction, Redis chooses eviction candidate from all databases.
In your case, it might evict keys from db0 or db1.
I am using lua script to do 2 operations belonging to same key. Running Redis in cluster mode. Using java jedis library to connect to the Redis cluster.
The syntax for loading lua script is as below
jedisCluster.loadScript(<ScriptString>, <Key>);
It returns a SHA value which I can use in evalsha function on jedis cluster as below
jedisCluster.evalsha(<ShaValue>, <Key Count>, <key>)
I am handling NoScript error when executing above method and will load the script again.
Question: If I am loading the same script with different key values will the SHA value is different? If the two keys land up in different cluster then the SHA value is different?
I am trying to save this SHA value in string use it for all keys.
I know the SHA of a string will be same but I am not sure if redis adding any extra information to the script before generating the SHA.
The SHA1 sum of the script will always be the same for the same script (you can also compute it externally, e.g. using the sha1sum tool). This remains true in single-instance and cluster modes, regardless the number of keys and arguments that the script gets as input.
I'm absolutely a newbie using redis.
I need to:
list all databases
list all data structures
I've connected to redis 4.0.11 server using redis-cli.
Redis is a key value storage not a database, You can't query or structure the Redis like you do in a database. You can only receive the relevant value from the key that you are passing.
Usually instead of database a key value storage like redis is used to to high performance key value storage and retrieve, if performance of a database is not enough.
I have data of several users in redis e.g.
hset - user111; field - dayssincelogin .....
I want to periodically update dayssincelogin for all users, one way to do it is
KEYS user*
HINCRBY ${key from above} dayssincelogin 1
Is it possible to do this in a single call? If not what's the most optimal way? I'm using using redis cluster and java client.
You can't do multiple increments in one command but you can bulk your commands together for performance gains.
Use Redis Pipe-lining or Scripting.
In Jedis I dont thing LUA is supported (If someone could answer that :) )
As #mp911de suggested; Use Exec for LUA Scripting
and you can also use pipelining to execute your bulk methods faster.
Have a Pipelining readup here for more information
And here is the sample code to use Jedis Pipelining.
Pipeline p = jedis.pipelined();
p.multi();
Response<Long> r1 = p.hincrBy("a", "f1", -1);
Response<Long> r2 = p.hincrBy("a", "f1", -2);
Response<List<Object>> r3 = p.exec();
List<Object> result = p.syncAndReturnAll();
Edit: Redis allows multi key operations only when they are present in the same shard. You should arrange your keys in such a way to ensure data affinity. like key1.{foo} and key5678.{foo} will reside in the same server
I have a list A in redis having values
K1 , K2 , K3
I want to delete all keys from redis matching values in list.
Is there a way to do this thing on one command or pipelining ?
You can fetch your list on the client side and then pipe some delete commands on the server. There is no other possibility for your task to be accomplished, as the LUA scripting feature is missing for the moment. With it, you could execute your task on the server without the need to fetch the whole list on the client.
yes, you can do that using eval and Lua (since redis 2.6)
eval "redis.call('del', unpack(redis.call('smembers', 'A')))" 0