Experience on using the keyvalue database redis and TTSERVER. Redisredis is a keyvalue storage system similar to memcached. it supports a large number of stored value types, including string, list, and set) and zset
Redis first
Redis is a key/value storage system similar to memcached. it supports a large number of storage value types, including string, list, and set) and zset ). On this basis, redis supports sorting in different ways. Like memcached, data is cached in the memory to ensure efficiency. The difference is that redis periodically writes updated data to the disk or writes modifications to the append Record File (I personally think redis is safer than memcache in data storage ), in addition, master-slave (master-slave) synchronization is implemented.
Redis has high access performance. SET operations are performed 110000 times per second, and GET operations are performed 81000 times per second (high speed !).
Redis provides different commands for different storage types of objects.
Redis currently provides four data types: string, list, set, and zset (sorted set ).
String is the simplest type. you can think of it as a type modeled by Memcached, and a key corresponds to a value. operations supported by string are similar to those of Memcached. However, it provides more functions.
List is a linked list structure. its main functions are push, pop, and getting all values in a range. In the operation, the key is interpreted as the name of the linked list.
Set is a set. it is similar to the set concept in our Mathematics. It adds and deletes elements to the set operations, and performs operations such as intersection and other operations on multiple sets. The key in the operation is interpreted as the name of the set.
Zset is an upgraded version of the set. It adds an ordered attribute based on the set. this attribute can be specified when you add and modify elements. after each attribute is specified, zset automatically re-adjusts the order according to the new value. It can be understood that there are two columns of mysql tables, one column stores value and one column stores order. In the operation, the key is interpreted as the zset name.
The following provides the redis command:
Applicable to all types of commands
EXISTS key determines whether a key EXISTS. If yes, 1 is returned; otherwise, 0 is returned;
DEL key: deletes a key or a series of keys. DEL key1 key2 key3 key4
TYPE key returns the data TYPE of a key element (none: Nonexistent, string: character, list, set, zset, hash)
KEYS pattern returns the list of matched KEYS (keys foo *: Search for KEYS starting with foo)
RANDOMKEY randomly obtains an existing key. if the current database is empty, an empty string is returned.
RENAME oldname newname: change the key name. if the new key exists, it will be overwritten.
RENAMENX oldname newname: change the key name. if the name exists, the change fails.
DBSIZE returns the total number of keys of the current database.
EXPIRE sets the expiration time of a key (in seconds), (EXPIRE bruce 1000: sets bruce to automatically delete the key1000 S) noteMeaning: if the value is changed before it expires, the value will be cleared.
How long does TTL wait for a key to expire? the returned Time is seconds.
SELECT index SELECT database
MOVE key dbindex: MOVE the specified key from the current database to the target database dbindex. 1 is returned; otherwise, 0 is returned (the source database does not have a key or the target database already has a key with the same name );
FLUSHDB clears all keys in the current database
FLUSHALL clears all keys in all databases
String Processing Command
SET key value sets the string value for a key. SET keyname datalength data (SET bruce 10 paitoubing: Save a string paitoubing with a key of burce and a string of 10 to the database). The maximum data size cannot exceed 1 GB.
GET key obtains the value of a key. If the key does not exist, "nil" is returned. if the key value is not of the string type, an error is returned.
GETSET key value can be understood as the value of the obtained key and then SET this value for more convenient operations (SET bruce 10 paitoubing, in this case, you need to modify bruce to 1234567890 and obtain the previous data paitoubing, GETSET bruce 10 1234567890)
MGET key1 key2... KeyN returns the values of multiple keys at a time.
The difference between SETNX key value SETNX and SET is that SET can create and update the key value, while SETNX is created if the key does not exist.Key and value data
MSET key1 value1 key2 value2... KeyN valueN sets multiple keys and values at a time in one atomic operation.
MSETNX key1 value1 key2 value2... KeyN valueN sets multiple keys and values at a time in one atomic operation (if the target key does not exist, if more than one key already exists, it fails)
INCR key auto-increment key value
INCRBY key integer enables auto-increment of key values.
DECR key auto-subtract key value
DECRBY key integer: used to reduce the value of a key.
Lists Processing Command
RPUSH key value adds an element from the end of the List (if the sequence does not exist, it is created first. if there is a Key with the same name but not a sequence, an error is returned)
LPUSH key value adds an element from the List header
LLEN key returns the length of a List.
LRANGE key start end returns the elements of the sequence from a custom range (LRANGE testlist 0 2; returns the first 0 1 2 elements of the sequence testlist)
LTRIM key start end trim data out of a certain range (LTRIM testlist 0 2; retain 0 1 2 elements, and delete the remaining)
LINDEX key index returns the value of the sequence at a certain position (LINDEX testlist 0; returns the element of the sequence testlist where the position is 0)
The LSET key index value updates the value of an element at a certain position.
The LREM key count value deletes a certain number of elements matching the value from the header (count positive) or tail (count negative) of the List, and returns the number of deleted elements.
LPOP key pops up the first element of the List.
RPOP key pops up the last element of the List.
RPOPLPUSH srckey dstkey pops up the last element in _ srckey _ and pushes it into the _ dstkey _ header. if the key does not exist or the sequence is empty, "nil" is returned"
Command for processing sets (with index unordered sequence)
SADD key member adds elements to the SETS sequence. if the element (membe) does not exist, 1 is added successfully; otherwise, 0 is failed; (SADD testlist 3/n one)
SREM key member: deletes an element in the SETS sequence. if the element does not exist, the result is 0. Otherwise, the result is 1 (SREM testlist 3/N one)
SPOP key randomly pops up a member from the collection.
SMOVE srckey dstkey member moves an element of a set sequence to another set sequence (SMOVE testlist test 3/n two; moves the element two from the sequence testlist to test, the two element does not exist in testlist)
SCARD key counts the number of elements in a sequence of SETS.
SISMEMBER key member knows whether the specified member exists in the collection
SINTER key1 key2... KeyN returns key1, key2 ,..., Intersection in keyN
SINTERSTORE dstkey key1 key2... KeyN splits key1, key2 ,..., Store the intersection in keyN to dstkey
SUNION key1 key2... KeyN returns key1, key2 ,..., Union of keyN
SUNIONSTORE dstkey key1 key2... KeyN splits key1, key2 ,..., KeyN union is stored in dstkey
SDIFF key1 key2... KeyN is based on key2 ,..., KeyN calculates the difference set of key1. Official example:
Key1 = x, a, B, c
Key2 = c
Key3 = a, d
SDIFF key1, key2, key3 => x, B
SDIFFSTORE dstkey key1 key2... KeyN is based on key2 ,..., The keyN calculates the difference set of key1 and saves it to the dstkey.
SMEMBERS key returns all elements of a sequence.
SRANDMEMBER key returns random elements of a sequence.
Zsets)
ZADD key score member: add a specified member to the sorted set. if the target exists, the score is updated (score, used for sorting)
ZREM key member deletes a specified member from an ordered set
ZINCRBY key increment member adds _ increment _ if a member exists. Otherwise, it sets a member whose score is _ increment _.
ZRANGE key start end: return the members of the specified range in ascending order.
ZREVRANGE key start end returns the members of the specified range after descending order.
ZRANGEBYSCORE key min max returns all ZCARD keys that match score> = min and score <= max. returns the number of elements in an ordered set. ZSCORE key element returns the SCORE value of a specified Member. ZREMRANGEBYSCORE key min max is deleted. all members meeting the score> = min and score <= max conditions.
Experience:
I personally think that redis speed is needless to say (very fast), but it consumes a lot of physical memory. it is a drawback of redis. redis is suitable for websites with small data volumes and fast updates, for example, the community is not suitable for websites with large data volumes, such as forums. I used to use redis in a forum post, but I gave up using redis because the data volume was too large and the physical memory consumption was astonishing!
Let's talk about TTSERVER.
Tokyo Cabinet is a DBM implementation. The database here consists of a series of key-value pairs. Both key and value can be byte sequences of any length, either binary or string. There is no concept of data types and data tables. When used as a Hash table database, each key must be different, so the same values of two keys cannot be stored. The following access methods are provided: the key and value parameters are provided for storage, records are deleted by key, and records are read by key. In addition, traversal of keys is also supported, although the order is arbitrary, it cannot be guaranteed. These methods are the same as the Unix-standard DBM, such as GDBM and NDBM, but are much better than their performance (so they can be replaced) when stored in the B + tree, records with the same key can also be stored. Functions like reading, storing, and deleting hash tables are also provided. Records are stored based on user-provided comparison functions. You can use an ordered or inverted cursor to read each record. Based on this principle, the forward string match search and integer range search are also implemented. In addition, transactions in the B + tree are also available. For a fixed-length array, records are stored by natural numbers. Two or more records with the same key cannot be stored. In addition, the length of each record is limited. The reading method is the same as that of the hash table. Tokyo Cabinet is written in C and provides APIs for c, perl, ruby, and java. Tokyo Cabinet is available on both POSIX and C99 platforms. it is released using the GNU Lesser Public License protocol.
Tokyocabinet:A key-value DBM database, but does not provide network interfaces, which are called TC.
Tokyotyrant:It is a network interface written for TC. it supports the memcache protocol and can also be operated through HTTP, which is called TT.
Performance:
Tokyo Cabinet is a DBM database developed by the Japanese Pinglin Yixiong. Tokyo Cabinet is released based on the GNU Lesser General Public License protocol and is developed in C language, it can run on any supported C99 and POSIX platforms. Compared with the common DBM database, it has the following features: small space, high efficiency, high performance, high reliability, and support for multiple development languages (currently C, Perl, Ruby, Java, lua APIs), supports 64-bit operating systems. The database reads and writes very fast. it takes only 1 million seconds to write 0.643 data records in hash mode, and only 1 million seconds to read 0.773 data records, which is several times that of DBM such as Berkeley DB.
Tokyo Tyrant and Tokyo Cabinet constitute a distributed persistent storage system that supports high concurrency. for any original Memcached client, you can regard Tokyo Tyrant as a Memcached. However, its data can be stored permanently. This is similar to the Memcachedb of Sina.
Ttserver and memcache comparison:
Ttserver is a database and memcached is a cache. Both save Form of data, any operation through the key. Ttserver can save data persistently. all memcached files are stored in the memory. memcached automatically deletes expired data for up to 30 days. When memcached works with APIs, it can automatically serialize data in and out and read deserialization. Ttserver supports master-slave replication and Operation logs, which are only available in the database. It is said that memcached is adjusting the overall architecture to support the in mechanism. network, event processing, and memory storage are separated. you can write a storage engine to store disk-based key-value in the future. The secondary development of memcached has entered a small climax.
Redis is a key/value storage system similar to memcached. it supports a large number of storage value types, including string, list, and set) and zset (sorted set...