Amrendra47
Amrendra47

Reputation: 11

Locking Mechanism for data stored in redis

Dears, I have a problem where multiple redis-clients are accessing a common structure stored in redis-server.

Requirements are as follows:-

  1. If a particular redis-client is accessing the structure stored in redis-server (shall do read and write operation on the structure), no other redis-client should be able to access and wait for being released.
  2. Every time other redis-client is accessing the structure, they should access the updated structure.

How can I put locking mechanism to fulfill this requirement in C Code. Thanks in Advance.

Upvotes: 1

Views: 2437

Answers (3)

georgeliatsos
georgeliatsos

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

Amrendra47
Amrendra47

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

Srini Sydney
Srini Sydney

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

Related Questions