Redis source code analysis (17) --- multi transaction operation, redismulti

Source: Internet
Author: User
Tags map data structure

Redis source code analysis (17) --- multi transaction operation, redismulti

As a non-relational database, redis has the same transaction operations with RDBMS, which surprised me. A dedicated file in redis is used to perform transaction-related operations. We can also learn how to implement transaction operations in Redis code. First, some apis under mulic. c are displayed.

/* ==================================== MULTI/EXEC ==== =========================*/void initClientMultiState (redisClient * c) /* initialize the client operation */void freeClientMultiState (redisClient * c)/* release all client resources related to multi/exec */void queueMultiCommand (redisClient * c) /* Add a new command to the multi command queue of the client */void discardTransaction (redisClient * c)/* cancel transaction operation */void flagTransaction (redisClient * c) /* indicates that a transaction is in the DIRTY_EXEC state and the execution fails. This method is called when the command is inserted */void multiCommand (redisClient * c) /* Add the multi command */void discardCommand (redisClient * c)/* undo command */void execCommandPropagateMulti (redisClient * c) /* Send the multi Command to all slave clients and aof files */void execCommand (redisClient * c)/* run the Command on a single client */void watchForKey (redisClient * c, robj * key)/* Add a key listener to the client */void unwatchAllKeys (redisClient * c)/* remove all keys from the client */void touchWatchedKey (redisDb * db, robj * key) /* indicates that the key is being monitored, and the next operation will fail. */void touchWatchedKeysOnFlush (int dbid)/* indicates the database where the key is located, touch all the watched-keys in this db */void watchCommand (redisClient * c)/* watch key command method, use parameters in the client to pass the value */void unwatchCommand (redisClient * c)/* cancel the command Method for listening to keys */
There are not many methods, but there is a frequently used word "key" in it ". This key does play a key role here. The muli code mainly contains commands, execution commands, and undo commands, such as the following undo transaction operations.

/* Cancel the transaction */void discardTransaction (redisClient * c) {freeClientMultiState (c); initClientMultiState (c); c-> flags & = ~ (REDIS_MULTI | REDIS_DIRTY_CAS | REDIS_DIRTY_EXEC); // The client cancels listening for all keys unwatchAllKeys (c );}
There is an unwatchAllKeys () method. The following are the key principles of transaction operations:

/* In transaction processing, there are two mapping types: key --> client lists, which indicates that all the clients in the list are listening for this key. When the value of this key changes, it can mark these clients as DIRTY States and need to be updated. At the same time, a key of list is maintained inside the Client, indicating all keys monitored by a Client. When the Client has a free operation, update the list of clients maintained in the key */

/* Indicates that the key is being monitored, and the next operation will fail */
That is to say, if the client is listening for the key, its next command will fail to be executed, achieving the synchronization effect,

/* "Touch" a key, so that if this key is being WATCHed by some client the * next EXEC will fail. * // * indicates that the key is being monitored, and the next operation will fail */void touchWatchedKey (redisDb * db, robj * key) {list * clients; listIter li; listNode * ln; if (dictSize (db-> watched_keys) = 0) return; clients = dictFetchValue (db-> watched_keys, key); if (! Clients) return;/* Mark all the clients watching this key as REDIS_DIRTY_CAS * // * Check if we are already watching for this key */listRewind (clients, & li ); while (ln = listNext (& li) {redisClient * c = listNodeValue (ln); // traverses the Client owned by the key, mark flag as DIRTY_CAS status c-> flags | = REDIS_DIRTY_CAS ;}}
When the Client tries to use the touch method to listen to the key, the flag status of the Client is changed to DIRTY_CAS. I cannot help but guess that the synchronous method is to use the CAS algorithm, if many clients use this algorithm, the CPU usage is indeed high. In general, the key maintains a Client list, and a Client also has a list of all its watch keys. The structure of the key is very simple:

/* Defines the watchedKey struct */typedef struct watchedKey {robj * key; redisDb * db;} watchedKey;
The key contains the database to which it belongs. Therefore, if you just undo the transaction, you need to remove all the keys listened by the client.


Java Development 20: Redis in the Real World: How does Redis defeat memcached in applications that contain a large number of read operations?

I will not discuss them one by one here. I will introduce some of them in a practical application development scenario. Using Redis as a cache solution I mentioned before, Redis can be easily used as a cache solution, and I happen to need such a solution now! In this application example, I integrate Redis into my location-based mobile Web service, called Magnus. If you are not interested in this series, I will first use the Play framework to implement Magnus. Since then, I have developed and reconstructed it in various implementations. Magnus is a simple service that can use JSON documents through http put requests. These documents describe the location of a specific account, indicating the person holding the mobile device. Now, I want to integrate the cache into Magnus, that is, I want to reduce I/O traffic by storing infrequently changed data in the memory. Magnus cache! In step 5, you can call get to check whether the newly introduced account name (a key) is a key in REdis. Get can return the account ID as a value or return null. If a value is returned, I use it as my acctId variable. If null is returned (indicating that the account name is not a key in Redis), I will search for the account value in MongoDB and add it to Redis by using the set command. The advantage here is speed: Next, the requested account will be submitted to a location, so that I can get its ID from Redis (as the memory cache ), instead of forwarding to MongoDB, it will incur additional I/O read costs. Listing 5. use Redis as the memory cache "/location/: account" {put {def jacksonMapper = new ObjectMapper () def json = jacksonMapper. readValue (request. contentText, Map. class) def formatter = new SimpleDateFormat ("dd-MM-yyyy HH: mm") def dt = formatter. parse (json ['timestamp']) def res = [:] try {def jedis = pool. getResource () def acctId = jedis. get (request. parameters ['account']) if (! AcctId) {def acct = Account. findByName (request. parameters ['account']) jedis. set (request. parameters ['account'], acct. id. toString () acctId = acct. id} pool. returnResource (jedis) new Location (acctId. toString (), dt, json ['latitude ']. doubleValue (), json ['longline']. doubleValue ()). save () res ['status'] = 'success'} catch (exp) {res ['status'] = "error $ {exp. message} "} response. json = jacksonMapper. w ...... remaining full text>

Java Development 20: Redis in the Real World: How does Redis defeat memcached in applications that contain a large number of read operations?

In addition, I have discussed common server-based data storage, such as MongoDB and CouchDB. Each data storage has its own advantages and disadvantages, especially when used in a specific field. This Java Development 2.0 focuses on Redis, a lightweight key-value pair data storage. Most NoSQL implementations are essentially key-value pairs, but Redis supports a wide range of value sets, including strings, lists, sets, and hashes. Therefore, Redis is generally called a Data Structure server. Redis is also known for its exceptional speed, making it the best choice for a specific type of use cases. When we want to know something new, it may be helpful to compare things we are familiar with. Therefore, we will start Redis exploration by comparing them with memcached. Next we will introduce the main functions of Redis, which can make it better than memcached in some application scenarios. Finally, I will show you how to use Redis as a traditional data storage for Model objects. Redis and memcached Memcached are well-known Memory Object cache systems that run by importing target keys and values into the memory cache. Therefore, Memcached can avoid I/O costs when reading a disk. Pasting memcached between a Web application and a database produces better read performance. Therefore, Memcached is a good choice for applications that require fast data query. One example is the stock query service. You need to access the database to obtain relatively static data, such as the stock name or price information. MemcacheDB is unfair to Redis and memcached. It is much better than MemcacheDB. MemcacheDB is a Distributed Key-Value Pair storage system designed for data persistence. MemcacheDB is similar to Redis. Its new advantages allow it to easily communicate with clients implemented by memcached. However, memcached also has its limitations. one fact is that all its values are simple strings. Redis, as a substitute for memcached, supports a richer set of functions. Some benchmarks also indicate that Redis is much faster than memcached. Redis provides a wide range of data types that allow it to store more complex data in the memory, which cannot be achieved using memcached. Unlike memcached, Redis can persist its data. Redis solves a major cache problem, and its rich feature set finds other uses for it. Because Redis can store data on disks and replicate data across nodes, it can be used as a data warehouse in traditional data mode (that is, you can use Redis, just like using RDBMS ). Redis is also often used as a queue system. In this case, Redis is the basis for backup and work queue persistent storage (using the Redis list type. GitHub uses this method to prepare Redis for a large-scale infrastructure example using Redis. Start now! To start using Redis, you need to access it through local installation or hosting vendor. If you use a MAC, the installation process may not be that simple. If you are using Windows ??, Install Cygwin first. If you are looking for a hosting vendor, Redis4You has a free plan. Regardless of the access method, you can perform operations according to the following examples in this article. However, it may not be a good cache solution to use a hosting vendor for caching, network latency may offset any performance advantage. You need to use commands to interact with Redis. That is to say, there is no SQL query language. Working with Redis is very similar to using a traditional map data structure, that is, all the other full texts>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.