Twemproxy Cache Proxy Server

Source: Internet
Author: User

Twemproxy Cache Proxy Server

Twemproxy Overview

Twemproxy (also known as Nutcracker) is a lightweight redis and memcached agent that is primarily used to reduce the number of connections to the back-end cache server. Twemproxy is an open source cache server Cluster Management tool used by Twitter to compensate for the redis/memcached of cluster (cluster) management.

Antirez (Redis author) wrote an introduction to Twemproxy that Twemproxy is the best solution for Redis Shard Management, although Antirez Redis cluster is being implemented and given great expectations, But from the existing cluster implementation still think cluster in addition to increase redis complexity, for the management of the cluster is not twemproxy to the light and effective.

When it comes to cluster management and the data Fragmentation Management (shard), in order to meet the growing and expanding data, data storage systems generally need to do some sharding, such as traditional MySQL horizontal table and vertical table, and then the application to access the correct location to find the right table. At this point, this data-directed work typically has three locations to put:

    • The data storage system itself supports Redis cluster as a typical attempt to support sharding on a data storage system;
    • Client support, memcached client support for the Shard is the client level;
    • Proxy support, Twemproxy is trying to build proxy support between server side and client;

In three different scenarios:

1. Client scenarios I think it's inappropriate because each client needs to maintain certain server information, but if you dynamically increase or decrease the node, you need to override the configuration of each client.

2, but on the server side to increase the cluster management is beneficial to users, reduce the need to understand the user, integrated cluster management to make performance than other scenarios are higher, but the disadvantage is that it will seriously increase the complexity of the code, resulting in server-side code explosion.

3, and the use of intermediary agent way I think is the most elegant and effective, without altering the server-side program, Twemproxy makes cluster management simpler, remove unsupported operations and merges, but also support multiple backend services, greatly reduce the number of connections, etc., but the shortcomings are obvious, It can not be more efficient use of the advantages of clustering, such as multi-key operations and range lookup operations, and so on, which are required by the server-side program itself support.

Twemproxy Installation

Install from Source:

git clone [email protected]:twitter/-fvi. /configure--enable-debug=full makesrc/nutcracker-h

Twemproxy command-Line options:

 Usage:nutcracker [-?HVDDT] [-v verbosity level] [-O output file  ][-C conf file ] [-s stats port] [-a stats addr][-I stats interval] [-p PID file ] [-M mbuf size] 

-h,–help: Viewing the Help document, displaying command options
-v,–version: View Nutcracker version
-t,–test-conf: Testing the correctness of the configuration script
-d,–daemonize: Run as Daemon
-d,–describe-stats: Print Status description
-v,–verbosity=n: Set Log level (Default:5, min:0, max:11)
-o,–output=s: Set log output path, default to standard error output (DEFAULT:STDERR)
-c,–conf-file=s: Specifying a Profile path (DEFAULT:CONF/NUTCRACKER.YML)
-s,–stats-port=n: Setting the Status monitoring port, default 22222 (default:22222)
-a,–stats-addr=s: Set Status monitoring IP, default 0.0.0.0 (default:0.0.0.0)
-i,–stats-interval=n: Set the state aggregation interval (default:30000 msec)
-p,–pid-file=s: Specify process PID file path, default off (Default:off)
-m,–mbuf-size=n: Set mbuf block size, default 64K, meaning see below 0 copies ;

0 copy (Zero copy)
In Twemproxy, the request and response are assigned to a piece of memory called Mbuf, and the mbuf receiving the request is used to forward to backend, similarly, backend receiving the response from Mbuf is also used to forward to the client, which avoids the memory copy.
In addition, MBUF uses a pool of memory that, once allocated, is no longer released, and when a request ends, the mbuf it uses is put back into the memory pool. A mbuf of 16K, this size needs to be in the I/O performance and connection concurrency between the choice, mbuf size, the socket read and write system calls less, but the entire system can support the smaller number of concurrent. If you want to support a higher number of client concurrent requests, you can set the size of the mbuf smaller (via the-M option).

Twemproxy Configuration

Twemproxy is configured through a YAML file, for example:

alpha:listen:127.0.0.1:22121 hash:fnv1a_64 Distribution:ketama auto_eject_hosts:true redis:true server_retry_t imeout:2000 server_failure_limit:1 Servers:-127.0.0.1:6379:1beta:listen:127.0.0.1:22122 hash:fnv1a_64 hash_t   AG: "{}" Distribution:ketama Auto_eject_hosts:false timeout:400 redis:true servers:-127.0.0.1:6380:1 Server1 -127.0.0.1:6381:1 server2-127.0.0.1:6382:1 server3-127.0.0.1:6383:1 server4gamma:listen:127.0.0.1:22123 has h:fnv1a_64 Distribution:ketama timeout:400 backlog:1024 preconnect:true auto_eject_hosts:true Server_retry_tim  eout:2000 server_failure_limit:3 Servers:-127.0.0.1:11212:1-127.0.0.1:11213:1delta:listen:127.0.0.1:22124 hash:fnv1a_64 Distribution:ketama timeout:100 auto_eject_hosts:true server_retry_timeout:2000 Server_failure_lim It:1 Servers:-127.0.0.1:11214:1-127.0.0.1:11215:1-127.0.0.1:11216:1-127.0.0.1:11217:1-127.0.0.1:1121 8:1-127.0.0.1:11219:1-127.0.0.1:11220:1-127.0.0.1:11221:1-127.0.0.1:11222:1-127.0.0.1:11223:1omega:listen:/tmp/gamma Hash: Hsieh Distribution:ketama auto_eject_hosts:false servers:-127.0.0.1:11214:100000-127.0.0.1:11215:1

Description

Listen
Twemproxy listening address, supports UNIX domain sockets.

Hash
The hash algorithm of the key value can be selected:

    • One_at_a_time
    • Md5
    • Crc16
    • CRC32 (CRC32 implementation compatible with libmemcached)
    • CRC32A (correct CRC32 implementation as per the spec)
    • Fnv1_64
    • fnv1a_64, default options
    • Fnv1_32
    • Fnv1a_32
    • Hsieh
    • Murmur
    • Jenkins

Hash_tag
Hash_tag allows you to calculate the hash value of a key based on one part of the key. Hash_tag consists of two characters, one is the beginning of the Hash_tag, the other is the end of the Hash_tag, between the beginning and the end of the Hash_tag, is the part that will be used to calculate the hash value of the key, and the result of the calculation will be used to select the server.

For example, if Hash_tag is defined as "{}", then the hash value of the key value "User:{user1}:ids" and "User:{user1}:tweets" is based on "user1" and will eventually be mapped to the same server. The "User:user1:ids" will use the entire key to calculate the hash and may be mapped to a different server.

Distribution
There are optional configurations for Ketama, Modula, and Random3. It has the following meanings:

    • Ketama, a consistent hash algorithm, constructs a hash ring based on the server and allocates a hash range for the nodes on the ring. The advantage of Ketama is that once a single node is added and deleted, the key value of the cache in the entire cluster can be reused to the fullest extent.
    • Modula, According to the hash value of the key value of the model, according to the results of the model to select the corresponding server;
    • Random, regardless of the key value of the hash is what, randomly select a server as the key value of the operation of the target;

Timeout

The unit is milliseconds and is the time-out value to connect to the server. The default is to wait forever.

Backlog
Listen for the length of the TCP backlog (connection waiting queue), which is 512 by default.

Preconnect
is a Boolean value that indicates whether Twemproxy should pre-connect the server in the pool. The default is False.

Redis
is a Boolean value that identifies whether the communication protocol for the server is Redis or memcached. The default is False.

server_connections
The number of connections per server that can be opened. By default, each server opens a connection.

auto_eject_hosts
is a Boolean value that controls whether Twemproxy should rebuild the cluster based on the server's connection state. This connection state is controlled by the server_failure_limit threshold.
The default is False.

Server_retry_timeout
The unit is milliseconds, which controls the time interval of the server connection, which is useful when Auto_eject_host is set to true. The default is 30000 milliseconds.

Server_failure_limit
Controls the number of times the server is connected, which is useful when Auto_eject_host is set to true. The default is 2.

Servers
A list of the addresses, ports, and weights of a server in a pool, including the name of an optional server, and if the name of the server is provided, it will use it to determine the order of the server, thus providing a corresponding hash ring of consistent hashes. Otherwise, the order in which the server is defined is used.

Twemproxy Monitoring

The TWEMPROXY monitoring port defaults to 22222 and collects information every 30s.

Nutcracker--describe-stats

The information in the report is as follows:

Pool Stats:client_eof "# EOF on client Connections" Client_err "# Errors on client connections" Clie       Nt_connections "# Active client Connections" Server_ejects "# times backend server was ejected" Forward_error "# times we encountered a forwarding error" fragments "# fragments created from a multi-vector request" serve R stats:server_eof "# EOF on server Connections" Server_err "# Errors on server connections" Server_  TimedOut "# Timeouts on server connections" Server_connections "# Active Server Connections" Requests "# Requests "request_bytes" Total Request Bytes "Responses" # Responses "response_bytes" Total Respo NSE bytes "In_queue" # Requests in incoming queue ' in_queue_bytes ' current request bytes in incoming que UE "Out_queue" # Requests in outgoing queue ' out_queue_bytes ' current request bytes in outgoing queue '

Pipelining

Twemproxy can receive many client requests at the same time, and only one or several connections back to the source, a structure that is ideal for pipelining requests and responses, thus saving TCP round-trip time.
For example, Twemproxy is acting on 3 client requests at the same time: ' Get key\r\n ', ' set key 0 0 3\r\nval\r\n ' and ' delete key\r\n ', twemproxy These 3 requests can be packaged into a message sent to the back-end Redis: ' Get key\r\nset key 0 0 3\r\nval\r\ndelete key\r\n '.

Pipelining is also one of the reasons for Twemproxy performance.

Twemproxy Cache Proxy Server

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.