Reputation: 11
Dears, I have a problem where multiple redis-clients are accessing a common structure stored in redis-server.
Requirements are as follows:-
How can I put locking mechanism to fulfill this requirement in C Code. Thanks in Advance.
Upvotes: 1
Views: 2437
Reputation: 1218
Redis provides the following:
1) Use Redis transactions and optimistic locking. See Redis Transactions
2) Or Lua scripting, which will be executed in Redis atomically. See EVAL
Upvotes: 1
Reputation: 11
Dears, thanks all for your response. It was helpful to know various features and generalised way available with the redis.
However I approached as follows as my requirement met this way.
I used secound timestamp (say t_sec) as key and counter as hash value. If in that particular second, further request comes, the counter value corresponding to t_sec key was incremented (HINCRBY command) in atomic way. Rest of the parameters are locally stored in a structure. If counter reaches a particular set limit, requests were dropped. If this is next sec, new t_sec key value is used and counter is incremented from zero. t_sec key corresponding to previous second got deleted (HDEL command).
Upvotes: 0
Reputation: 580
Use the watch command https://redis.io/commands/watch to detect modification by other clients. This command is specified only to the specified keys in redis transactions
Upvotes: 0