A Couple Redis Gotchas with a Focus on Concurrency
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:
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.endThis 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:
2.#rest of the code here ...
3.end
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:
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.endThis 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:
CODE:
1.if val = redis.get("some_key")2.#rest of the code here ...
3.end
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733699/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- jQuery :focusjQuery
- Couple je l'ai dit précédemment dansAI
- JavaScript focus 事件JavaScript事件
- jQuery focus事件jQuery事件
- react focus 事件React事件
- Concurrency Patterns in GoGo
- Java Concurrency in Depth - 1Java
- Java Concurrency in Depth - 2Java
- jQuery|focus焦點家族jQuery
- Asyncio in Python and Concurrency tasksPython
- C++ Gotchas 條款64:丟擲String Literals (轉)C++Go
- CSS E:focus 選擇器CSS
- [CSS] :focus-visibleCSS
- Concurrency(六: 同步程式碼塊)
- Concurrency(四:執行緒安全)執行緒
- Entity Framework Tutorial Basics(28):ConcurrencyFramework
- GoLang之Concurrency再討論Golang
- MongoDB的鎖機制 (Concurrency)MongoDB
- 新CSS偽類:focus-withinCSS
- focusin與focus事件的區別事件
- Concurrency(五: Java記憶體模型)Java記憶體模型
- C++ Gotchas 條款62:替換Global New和Global Delete (轉)C++Godelete
- js堅持不懈之11:focus()方法JS
- 神奇的選擇器 :focus-within
- css :focus選擇器用法介紹CSS
- IOS input auto focus 解決方案探究iOS
- cobitsecuritybaseline/ImplementationGuide/cobit-focusNGUIIDE
- 迴歸Android Focus on Android.Android
- Concurrency(十五: Java中的讀寫鎖)Java
- Concurrency(一:如何理解多執行緒)執行緒
- Go 併發concurrency 學習筆記Go筆記
- 說說Flutter中的無名英雄 —— FocusFlutter
- CSS :focus-within 偽類選擇器CSS
- Concurrency(二:建立和啟動執行緒)執行緒
- 用 Go 語言實戰 Limit Concurrency 方法GoMIT
- div元素獲取焦點觸發focus事件事件
- 聊聊FluxFlatMap的concurrency及prefetch引數UX
- Concurrency(二十三: 非阻塞演算法下)演算法