Memcache
Memcache is a high-performance distributed memory object caching system that is primarily used to cache data queried from the MySQL database, reducing the pressure on the MySQL database.
Memcache Workflow:
When a user occurs a dynamic request, first go to memcache server inside query cache data, when the first query, memcache there is certainly no data, this time need PHP program to get data in MySQL database, Will get cached one copy into the Memcache server inside, the data is returned to the user. When the second occurrence of the same dynamic request, this time because the last time the memcache has cached the corresponding query data, the Memcache can directly return data, without the need for MySQL database participation, reduce the MySQL database pressure. For this architecture, because Memcache is in the side, this caching system is called a bypass caching system.
Memcache uses a pre-allocated mechanism to manage its own memory units. Memcache This set of allocation mechanism is mainly to solve the memcache of a memory fragmentation problem.?
Basic commands for Memcache :
Add a key replace update set If one does not exist add if existing then update delete Delete a key get Gets the value of a specified key prepend append stats in front of a value to view the memcache status flush_all emptying all the memcache inside
Memcache distributed: Memcache is a high performance, open source, distributed cache system. Due to the limited capacity of a single memcache service, multiple memcache can be used to complete the service at this time, and the resulting architecture is called the Memcache distributed (multiple) cache system.
Memcache Security Issues:
1. Memcache in the process of use, there is no user name of the authentication operation. So generally in the use of memcache is used in the intranet, do not use the public network IP.
2. If you really want to use the public network IP, you can write a special firewall authentication rules, only allow the specified IP to operate the Memcache server.
Memcache Avalanche: When using the Memcache cache system all crashes, this time all the pressure on the MySQL server, MySQL server can not support its access, will immediately crash, this phenomenon is called the memcache avalanche phenomenon. To prevent such problems from being sent, it is necessary to optimize the Memcache cache system (using the consistent hash algorithm to implement the caching system).
? Memcache Summary:
1, Memecache the data all exist in memory, power off will hang, the data can not exceed the size of memory.
2. Memcache uses the Slaballocator memory allocation mechanism: divides allocated memory into blocks of specific length in a predetermined size to completely resolve memory fragmentation issues.
3, memcache exist in memory, allocated memory is full, will be in accordance with certain rules to delete some k/v data, restart the natural all lost.
4. Expiration policy--memcache is specified when set, such as set Key1 0 08, which never expires. Redis can be set through, for example, expire.
5, the first thing to note is that the memcached supports the largest storage object is 1M. Its memory allocation is very special, but the allocation method is based on performance considerations, simple allocation mechanism can be more easily recycled redistribution, save the use of the CPU. Larger than 1M requires splitting.
6, memcached can accept the maximum length of the key is, 255 characters.
7, the same data sent a set command and a GET command, they do not affect each other, but after get, processing may be the other set, the subsequent set will overwrite the previous, However, the Memcached1.2.5 and later versions provide the Get and CAS commands, which solve the problem above. If you use the GET command to query the item,memcached of a key, you will be returned with a unique identifier for the item's current value. If you overwrite this item and want to write it back to memcached, you can send the unique identity to memcached with the CAS command. If the item's unique identity in the memcached is consistent with what you provide, your write operation will succeed. If the item is also modified by another process during this time, the unique identity of the item stored in the memcached will change and your write will fail.
8, no authentication, the authentication is considered to be a higher level of the problem.
9. Deleting the delete operation simply resets the chunk to the delete state so that the next use will take precedence over such chunk.
10. Flush operation is equivalent to an action that invalidates all the item. Does not change the memcache memory allocation situation.
11, Memcache already allocated memory will not be actively cleaned up.
12, Memcache assigned to a Slab memory page can no longer be assigned to other slab.
13, Flush_all can not reset memcache allocation memory page of the landscape, just to all the item set to expire.
14, Memcache maximum storage of the item (key+value) size limit of 1M, which is limited by the page size of 1M.
15, because the Memcache distributed is the client program through the hash algorithm to get the key to achieve, different languages may use different hash algorithm, the same client program may be used differently, so in the multi-language, multi-module sharing the same set of memcached services , be sure to pay attention to choosing the same hash algorithm on the client.
16. When starting memcached, LRU substitution can be disabled through the-m parameter, and add and set will return failure when memory is exhausted.
17, memcached When starting to specify the amount of data storage, does not include the memory itself, and in order to save data and set up the management space. As a result, it consumes more memory than the memory allocation specified at startup, which requires attention.
Redis
Redis:remotedictionaryserver, can be directly understood as a remote dictionary service
Redis is a key-value storage system. Similar to memcached, it supports storing more value types , including string (string), list (linked list), set (set), Zset (sortedset--ordered collection), and hash (hash type, Similar to the associative array inside PHP). As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file (data persistence).
The advent of Redis has largely compensated for the problem of memcached such Key/value (Memcache's value can only hold a single string type).
Features of Redis:
1, (value can be a variety of data types) a variety of data types are stored: String type strings, hash types (associative arrays) hash, list type lists, set type set, ordered collection type
2. Memory storage and persistence
(1), memory read and write speed far faster than the hard disk
(2), self-provided a persistent function (RDB, aof two ways)
RDB (snapshot) persistence can save data from memory to the hard disk within a specified time interval. This method of preservation is a danger of losing data.
AOF Persistent records all the write commands performed on the Redis server (a bit like the bin logs inside MySQL) are all saved to the log on the disk file, and the data set is restored by re-executing the commands when the server starts. The commands in the AOF file are all saved in the Redis protocol format, and the new command is appended to the end of the file.
Redis can also use both AOF persistence and RDB persistence. In this case, when the Redis restarts, it takes precedence over the aof file to restore the dataset because the AoF file saves a dataset that is typically more complete than the data set saved by the Rdb file.
You can even turn off the persistence feature so that the data exists only in memory when the server is running. Equivalent to Memcache.
3. Rich function
(1), can be used as a cache, queue (FIFO)
(2), support the key to the time of survival (cache time can be set to the millisecond level (to do the second kill application), Memcache only support to the second level)
(3), according to certain rules to delete the corresponding key
4. Simple and stable
(1), compared to SQL is more concise, do not need to do the parsing of SQL statements.
(2), the client in different languages is rich, support multiple languages to operate Redis
(3), based on C language development (more efficient), the code volume only more than 30,000 lines
? Redis Multi-database:
Redis supports 16 databases by default, and is named after an incremental number starting from 0, and can be modified by the database to modify the default number of databases. After the client connects to the Redis service, the No. 0 database is automatically selected, and the database can be replaced by the Select command, such as the 1th database.
Description
Redis does not support custom database names.
Redis does not support setting access passwords for each database.
Multiple databases for Redis are not securely isolated, and the Flushall command empties data from all databases.
Redis Survival Time:
Redis is used more as a cache in its actual use, whereas cached data is generally set to survive, that is, the data is destroyed after expiration.
To set the time-to-live syntax:
EXPIRE keyseconds?: Set the time to live in minutes pexpire keymilliseconds?: Setting the time to live in milliseconds allows for more precise control.
TTL to see the remaining time of key, when the return value is 2, indicates that key has expired and has been deleted.
Purge time-to-live syntax: PERSIST key
You will need to reset the lifetime of the key when setting up new data. Re-setting the value also clears the time to live.
?
Common commands in Redis:?
Client Connection command: SRC/REDIS-CLI
? Stop Redis service: Src/redis-clishutdown?
1. Send command: Ping (return pong)
? 2, command return value: Set KEYVALUE
3. Get help command: Helps?
4. Database switchover: Select 1
5. Get the list of build names that conform to the rules command:
keys* keystest[_]* keyst[a-d] Description:? Match one character * matches any one (including 0) characters [] matches any character between parentheses, you can use "-" to represent the range. such as a[a-d] Match ab/ac/ad \x match character X, used to escape compliance if you want to match "? "You need to use \?"
6. Exists judgment command: exists KEY
Determine if a key exists. Returns an integer type of 1 if key exists, otherwise 0 is returned.
7. del Delete command: Del KEY
? Delete key, you can delete one or more keys, multiple keys separated by a space, the return value is the number of deleted keys.
8. Type View key value type command: Type key
Gets the data type of the key value, which may be a string (string), hash (hash type), list (list type), set (collection type), Zset (ordered collection type).
9, Flushall command: Flushall (This command is usually used only when doing the test)
Clears all databases. (All data in 16 databases that contain the default configuration)
10, Assignment and Value command:
SET key value (assigned value)
GET key (take value)
Note: When there are spaces in the middle of a character, enclose it in either a single-or double-apostrophe.
Multiple Msetmget
11. Set/Get multiple key values at the same time
MSET key value [keyvalue ...]
MGET Key[key ...]
12. INCR Self-increment implementation command: Incrby Keynumber
When the stored string is an integer , Redis provides a useful command, INCR, to increment the current key value and return the incremented value. If key does not exist, it will be created automatically if there is an automatic +1.
13, DECR Self-reduction command:
? DECR key
Decrby Key number (reduces the specified integer)
14. APPEND Append command: APPEND keyvalue
Appends a value to the tail. If the key does not exist, the key is created, and its value is written as value, which is equivalent to set keyvalue. The return value is the total length of the appended string.
15. strlen Get string length command: Strlenkey
The length of the string, returns the length of the data, and returns 0 if the key does not exist. Note that if the key value is an empty string, the return is also 0.
16. Zadd operation
Zadd keyscore1 value1socre2 value2 ...
17. Zrange operation
Zrangekey 0-1 (-1 represents the last subscript in the collection, from small to large)
? 18. Zrevrange operation
Zrevrangekey 0-1 (representing reversal sort from large size)
19. Lpush add element information to the left of the linked list
Lpush Link2 a D e F
20, Lrange operation: Get the contents of the list
Lrangelink1 0-1 (-1 for the last element subscript)
21, Lrem Delete the contents of the list
Lrem link-2 B (meaning to delete 2 element B from the left)
Lrem Link 2 b (means to delete 2 element B from the right)
?? Add: If the site is in high concurrency: There are two ways to extend: 1. Do cluster: Expansion server 2. Hardware upgrade, add a queue server to the original project. Queue Server can support very high concurrency, you can first save the user's request directly, after slowly processing.
Redis Summary:
1, Redis not only supports simple k/v type of data, but also provides the storage of data structures such as List,set,hash.
2, Redis support data backup, that is, Master-slave mode of data backup.
3, Redis support data persistence, you can keep the in-memory data on the disk, restart the time can be loaded again for use.
4, Redis, with a certain database characteristics.
5, Redis data can be stored to the hard disk, the basic no expiration policy.
6. Redis has a fatal flaw when the memory is full, the dump data CPU consumes 100%.
Memcache and Redis differences
In Redis, not all data is stored in memory all the time, which is the biggest difference compared to memcached.
Redis has a database feature in many ways, or a database system, and memcached is simply a k/v cache.
They need to do the expansion of the cluster, the implementation of the way: Master-slave, Hash.
In more than 100k of data, memcached performance is higher than Redis.
If you want to say that memory usage efficiency, using simple key-value storage, memcached memory utilization is higher, and if Redis uses hash structure to do key-value storage, because of its combined compression, its memory utilization will be higher than memcached. Of course, this is related to your application scenario and data characteristics.
Memcache and Redis Review summary