A Couple Redis Gotchas with a Focus on Concurrency

[复制链接]
查看11 | 回复4 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
Redis is an amazing global datastructure server. The fact that it’s global, makes it ideal for a multi-process or multi-threaded system, to get some concurrency action going. This also means, that a lot of the cautions that need to be taken while working in a shared memory system also apply to a situation where redis is operating in a concurrent/distributed environment.
In this article, I am going to glaze over a couple of gotcha’s to watch out for when working with Redis. It is by no means an attempt to be an exhaustive monograph on concurrency and Redis, but rather something to get your feet wet.
Having the rug pulled from under you
Check out the following code:
1.if redis.exists("some_key")
2.puts "Yay! Redis' got it"
3.compute_primes #Perform some time-intensive computation
4.val = redis.get("some_key")
5.render :json => { :value => val }
6.end
复制代码
This code that checks for the existence of a key in redis in line 1 and then performs some conditional logic, part of which involves retrieving the key from redis has a race condition in it. It is the fact that in between lines 1 and 4 another process could’ve deleted the key from redis. A quick fix for this:
1.if val = redis.get("some_key")
2.#rest of the code here ...
3.end
复制代码

回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Modifying keys safely
Again an example is far more illustrative:1.def update_safe_ips
2.redis.del("safe_ips")
3.safe_ip_ids = SafeIps.select(id).all.map(&:id)
4.safe_ip_ids.each { |safe_ip_id| redis.lpush("safe_ips", safe_ip_id) }
5.end
复制代码What this method is supposed to do is update the safe_ips redis list with stuff from the SafeIps table in the DB. The problem with this code is that it’s too eager to delete the “safe_ips” list.
After line 1 executes the safe_ips list is nixed from redis. Assuming a different process runs after line 1 executes, and that process depends upon this safe_ips list existing, it’s going to blow. So what’s the solution?
For any kind of operations that involve updating a redis datastructure, avoid deleting it. Instead lean towards creating a “temp” version of the datastructure and using the rename command, which is atomic. A second pass at fixing the code looks something like this:1.def update_safe_ips
2.safe_ip_ids = SafeIps.select(id).all.map(&:id)
3.safe_ip_ids.each { |safe_ip_id| redis.lpush("safe_ips_temp", safe_ip_id) }
4.redis.rename "safe_ips_temp", "safe_ips"
5.end
复制代码While this code looks like it should work great it still has a race condition in it. If two processes were to concurrently hit this method, they both would each populate this “safe_ids_temp” list thereby creating dups. In essence, if:SafeIps.select(id).all.map(&:id) # => ["10.0.0.1", "10.0.0.2", "10.0.0.3"]复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Then if two processes were to execute this update_safe_ips method at the same time, the “safe_id_temp” list could be [“10.0.0.1”, “10.0.0.2”, “10.0.0.3”,“10.0.0.1”, “10.0.0.2”, “10.0.0.3”]. To protect against this:1.def update_safe_ips
2.safe_ip_ids = SafeIps.select(id).all.map(&:id)
3.temp_list = %Q{ safe_id_temp#{UUID.getUUID} }
4.safe_ip_ids.each { |safe_ip_id| redis.lpush(temp_list, safe_ip_id) }
5.redis.rename temp_list, "safe_ips"
6.end
复制代码This code assumes the existence of a UUID library, which returns a unique ID on every call. Now, if more than one process were to run, they would each create their own temp_list’s. This way, dups will not be created.
Running a piece of code only once
Often times, we would like for a certain piece of code to run successfully exactly once. A classic example of this is something like setting up some auth tokens:1.def setup_auth_tokens
2.username = redis.hget "web_service_creds", "username"
3.pwd= redis.hget "web_service_creds", "pwd"
4.auth_token = get_auth_token username, pwd
5.redis.set "web_service_auth_token", auth_token
6.end
复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Now, we know that this setup_auth_tokens method is going to be called multiple times in a concurrent environment. How do we ensure it executes successfully just once, in the lightest possible manner? A first stab could be:1.def setup_auth_tokens
2.unless redis.setnx "setting_up_auth_token", true
3.username = redis.hget "web_service_creds", "username"
4.pwd= redis.hget "web_service_creds", "pwd"
5.auth_token = get_auth_token username, pwd
6.redis.set "web_service_auth_token", auth_token
7.end
8.end
复制代码The setnx command would return true, only if the key does not already exist. This way we can force a certain block of code to be executed only once. While this would ensure that the block in the ensure happens only once, if for some reason an exception gets thrown in the block code no other attempts are made at re-setting the auth token. A quick fix:01.def setup_auth_tokens
02.begin
03.unless redis.setnx "setting_up_auth_token", true
04.username = redis.hget "web_service_creds", "username"
05.pwd= redis.hget "web_service_creds", "pwd"
06.auth_token = get_auth_token username, pwd
07.redis.set "web_service_auth_token", auth_token
08.end
09.rescue
10.redis.del "setting_up_auth_token"
11.end
12.end
复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
Conclusion
Redis is shared memory on steroids. Working with redis in a concurrent environment is both fun and highly performant. Enjoy!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行