讓Redis資料集保持一致性
As you start using Redis more, you soon find yourself delving into redis' transactions. A traditional RDBMS' view of CAS (compare-and-set) transactions is:
- Lock down the entire database to writes, allowing just the one connection in the transaction to write
- Perform. a query to figure out which things need to change (Compare Step)
- Change those things (Set step)
- Release the lock (this happens automaticaly as part of your transaction completing)
Redis, on the other hand uses Optimistic Locking which makes CAS transactions look like:
- Start tracking stuff you think could change while you are in your transaction
- Perform. a query to figure out which things need to change (Compare Step)
- Execute the transaction to change those things (Conditional Set)
- Check to see if the transaction completed successfully
- Repeat from step 1 to re-run transaction or just abort
Step 1, where you start tracking stuff prior to doing anything is where Optimistic Locking and the more traditional Pessimistic Locking diverge in a pretty big way.
Optimistic Locking
The general idea behind optimistic locking is that you need to know before hand what you think might change while you are perform. a transaction and watch out for that. Pessimistic locking on the other hand, is a more heavy handed approach where you don’t want anything to change while you are in the middle of a transaction.
Pessimistic locking, as you might’ve guessed, is more punishing on performance and forwrite heavy datastores like Redis that need to maintain high performance, it just is not an option. The downside with optimistic locking, though is that more of the heavy lifting falls on the engineer, who needs to put in a little more thought while dealing with transactions. Redis ships with a watch command that lets you specify what keys you want to keep an eye on, prior to running a multi-exec transaction.
- Lock down the entire database to writes, allowing just the one connection in the transaction to write
- Perform. a query to figure out which things need to change (Compare Step)
- Change those things (Set step)
- Release the lock (this happens automaticaly as part of your transaction completing)
Redis, on the other hand uses Optimistic Locking which makes CAS transactions look like:
- Start tracking stuff you think could change while you are in your transaction
- Perform. a query to figure out which things need to change (Compare Step)
- Execute the transaction to change those things (Conditional Set)
- Check to see if the transaction completed successfully
- Repeat from step 1 to re-run transaction or just abort
Step 1, where you start tracking stuff prior to doing anything is where Optimistic Locking and the more traditional Pessimistic Locking diverge in a pretty big way.
Optimistic Locking
The general idea behind optimistic locking is that you need to know before hand what you think might change while you are perform. a transaction and watch out for that. Pessimistic locking on the other hand, is a more heavy handed approach where you don’t want anything to change while you are in the middle of a transaction.
Pessimistic locking, as you might’ve guessed, is more punishing on performance and forwrite heavy datastores like Redis that need to maintain high performance, it just is not an option. The downside with optimistic locking, though is that more of the heavy lifting falls on the engineer, who needs to put in a little more thought while dealing with transactions. Redis ships with a watch command that lets you specify what keys you want to keep an eye on, prior to running a multi-exec transaction.
An example
All this sounds great, but nothing beats a real-world example to see how to work with this and why it might be harder than you think. Recently, I was working on a task that required me to do just this — when a user logs into our app, figure all of that users facebook friends who are logged in, in our app and send that over.
The setup
The data in our app is structured in the following format:
- Users are hashes of the format “user|”
- All currently logged-in users have their facebook ID’s stored in a “logged_in_fb_ids” set
- There is a facebook ID to user ID reverse look up hash map “fb_id_to_user_id_hash”
- Every user has a set of facebook friends ids that contains facebook id’s of people they are friends with on facebook — “fb_friend_ids_for_user|”
All this sounds great, but nothing beats a real-world example to see how to work with this and why it might be harder than you think. Recently, I was working on a task that required me to do just this — when a user logs into our app, figure all of that users facebook friends who are logged in, in our app and send that over.
The setup
The data in our app is structured in the following format:
- Users are hashes of the format “user|
- All currently logged-in users have their facebook ID’s stored in a “logged_in_fb_ids” set
- There is a facebook ID to user ID reverse look up hash map “fb_id_to_user_id_hash”
- Every user has a set of facebook friends ids that contains facebook id’s of people they are friends with on facebook — “fb_friend_ids_for_user|”
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-741026/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Redis和MySQL如何保持資料一致性?RedisMySql
- Redis與資料庫資料一致性Redis資料庫
- mysqldump備份時如何保持資料的一致性MySql
- 保持MySQL和Redis中的資料一致MySqlRedis
- Mysql和Redis資料如何保持一致MySqlRedis
- Redis 如何保持和MySQL資料一致【一】RedisMySql
- Redis 如何保持和MySQL資料一致【二】RedisMySql
- Redis叢集 - cluster叢集、資料分片Redis
- Redis的主從資料一致性Redis
- redis基礎篇——資料一致性Redis
- Redis和資料庫的資料一致性問題Redis資料庫
- 【資料庫】Redis叢集篇資料庫Redis
- Golang 實現 Redis(7): Redis 叢集與一致性 HashGolangRedis
- redis叢集 資料遷移方案Redis
- 分散式服務資料一致性-redis篇分散式Redis
- 如何保證MySQL和Redis資料一致性?MySqlRedis
- Redis資料型別, Redis主從哨兵和叢集(將資料匯入叢集) ubuntu使用Redis資料型別Ubuntu
- [Redis] 02-快取和資料庫資料一致性問題Redis快取資料庫
- redis資料庫叢集三種模式Redis資料庫模式
- 提升客戶體驗,讓DSR保持高位執行——資料資訊圖
- 8張圖搞懂Redis和MySQL資料一致性問題RedisMySql
- Redis 切片叢集的資料傾斜分析Redis
- Redis叢集模式和常用資料結構Redis模式資料結構
- 讓前端監控資料採集更高效前端
- 如何讓tableView保持順暢View
- MYSQL兩個資料庫字符集保持一致問題MySql資料庫
- mysql 資料假刪 保持資料唯一MySql
- redis叢集資料儲存和獲取原理Redis
- redis常用資料型別操作命令集錦Redis資料型別
- 掘地三尺搞定 Redis 與 MySQL 資料一致性問題RedisMySql
- 美團四面:如何保障 MySQL 和 Redis 的資料一致性?MySqlRedis
- 面試常問:如何保證Redis快取和資料庫的資料一致性NRXW面試Redis快取資料庫
- Redis叢集資料沒法拆分時的搭建策略Redis
- 資料一致性
- 重工業機臺資料彙集,如何保證資料準確的同時,又保持高效率?
- Redis快取穿透、擊穿、雪崩,資料庫與快取一致性Redis快取穿透資料庫
- 如何保證快取(redis)與資料庫的雙寫一致性快取Redis資料庫
- 面試官:談談Redis快取和MySQL資料一致性問題面試Redis快取MySql