Comparison of INCR/DECR differences between Redis and memcached

Source: Internet
Author: User

The widely used distributed cache Redis and memcached support the addition and subtraction of integer value values, which corresponds to the incr and DECR commands in the specific command.

INCR/DECR is an atomic operation (Memcached 1.2.4 and later added/redis), which is widely used in counters and speed limiters.

distributed Cache Redis and memcached differ in the specific syntax of the two commands

The INCR command syntax for Redis is divided into two types:

1) incr key, which will specify the value of the primary key key plus one;

2) Incrby key increment, which will specify the value of the primary key key plus increment.

The DECR command syntax for Redis is also divided into two types:

1) DECR key, the value of the specified primary key key is reduced by one;

2) Decrby key decrement, which will specify the value of the primary key key minus decrement.

Memcached's INCR command syntax

1) incr key Integer, which will specify the value of the primary key key plus the given integer,

2) The DECR command syntax is DECR key Interger, which is about to specify the value of the primary key key minus the given Interger.

The difference between the integer values supported by the distributed cache Redis and memcached

The specific type of the integer value value that Redis supports is a signed 64-bit integer that is represented by a string (decimal).

The memcached supported integer value value is an unsigned 64-bit integer that is represented by a string (decimal).

This results in a different range of values for the integer value values supported by the two systems. The integer value range supported by Redis is -9223372036854775808~9223372036854775807, while the range of memcached is 0~18446744073709551615.

1. The string stored in key is interpreted as a decimal

#Server

memcached_version:1.2.6

# Server
redis_version:3.1.999

From the above experiments on memcached and Redis, you can see that the INCR command is saved as a string.

2. Redis signed 64-bit Integer, memcached unsigned 64-bit integer

1) First assign a signed 64-bit integer maximum value of 9223372036854775807 to a key max; Redis 127.0.0.1:6379> set Max 9223372036854775807 OK2) The value of the primary key is added one, the system is prompted to add an action will cause an upward overflow, this shows that Redis supported the largest integer value is indeed 9223372036854775807; redis 127.0.0.1:6379> incr Max (Error) ERR increment or decrement would overflow3) Another key min to assign a signed 64-bit integer minimum-9223372036854775808; Redis 127.0.0.1:6379> Set min-9223372036854775808 OK4) The value of the primary key is reduced by one, the system indicates that this minus one operation will cause a downward overflow, so that the minimum number of Redis supported integer value is indeed-9223372036854775808; redis 127.0.0.1:6379> decr min (Error) ERR increment or decrement would overflow

What happens when you look at memcached?

1) First assign an unsigned 64-bit integer maximum value of 18446744073709551615 to a key max; set Max 0 0 18446744073709551615 STORED2) The value of the primary key is added one, the system execution result is 0, that is, the system itself will automatically avoid upward overflow, this shows that the memcached supported by the largest integer value should indeed be 18446744073709551615; incr Max 1 Client_error cannot increment or decrement non-numeric value question: After incrementing 1 ask what does not overflow, instead prompts "the non-numeric cache operation will return the error message"3) then assign an unsigned 64-bit integer minimum value of 0 to a key min; Set min 0 0 1 0 STORED4) The value of the primary key is reduced by one, the execution of the system is still 0, that is, the system itself will automatically avoid the downward overflow, thus memcached support of the minimum integer value value should indeed be 0; decr min 1 0

differences in distributed cache Redis and MEMCACHED,INCR/DECR

1, Redis if the corresponding key does not exist, INCR/DECR will not fail, but will count from 0

2, memcached if the corresponding value does not exist, INCR/DECR will fail, but will not count from 0

3. INCR/DECR operation cannot refresh expiration time

Redis, we know that the set, Getset command for the key value assignment will also eliminate the key value of the time to live, the persist command to set the expiration time setting of the key cancel the expiration time setting, if necessary can re-use the expire command to set the time to live the key. Other action commands for keys, such as INCR, Lpush, Hset, Zrem, do not affect the lifetime of the key.

The MEMCACHED,INCR/DECR operation cannot refresh the expiration time, so the expiration time is based on the initialization time.

Therefore, if you use memcached as a long-term counter, you must refresh the item periodically with an additional mechanism. The memcached protocol provides a touch method that refreshes only the time and does not modify the value.

Memcached official agreement look here.

Reference: http://blog.sina.com.cn/s/blog_48c95a190101772i.html

Comparison of INCR/DECR differences between Redis and memcached

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.