Layout: Post title: “memcache” subtitle: “cache middleware “” Date: 2018-11-03 06:00:00 author:” Bluetown “header-img: “img/post-bg-2015.jpg” catalog: true tags: – cache

The history of

role

What does it replace

Memory-based databases.

It replaces the relational database, because the relational database is too heavy, the preparation work needs to create databases, tables, and the operation of data needs to establish connections, connections are very expensive.

Memcache is designed to replace relational databases, to be precise, to relieve (not really replace) the burden of relational databases by putting some of their data into an in-memory database.


What kind of data fits into an in-memory database? 1. Non-persistent data is stored in a relational database if it needs to be persisted.

Temporary storage. Data that don’t change very often. Data with low real-time requirements.

2. Keep your data small. Store only the most important data in memory, because memory is usually small.

Meet the above conditions, it is more suitable for memory database.


Application Scenario 1. Mobile verification code // is also an ID

2. Various identification IDS // Prevent duplicate order submission

3. The user ID // can also be saved slowly, but the best practice is to put it in the session

Why use it

Because it is an in-memory database, it is faster than a relational database.

How to use

1. Written in the SERVER C language. Start the server.

2. Multiple client modes.

Can GUI client, Web, JAR package // Java


Java – The most common open source JAR package is XMemCached

Application scenarios

The data type

General data types are supported. For example: string objects

distributed

Support distributed.


Can save memory how to save? ?


How to install multiple nodes? The relationship with the Web server? ?


Multiple nodes, how is the data stored? What are the backups like? 1. The server is distributed memcache’s distribution is only the server’s distribution.

2. Data is not distributed. Data on each server is different, and each server maintains its own data. If the server is down, the server’s data is down.

What about the data now? The cache server data is from the database, the first request, the cache does not have data from the database, then from cache access. Now the cache is down and the data is gone, and the server is unavailable. If a new request is made, it is fetched from the database as it was when the first request was made, but a different cache server is used in this case, and subsequent requests are fetched from this new cache server.

How do I know if I need a new cache server? There are routing algorithms that specifically select an available cache server.

In summary, memcache data is not distributed, that is, it is not consistent across all cache servers. If one cache server updates data, it notifies all other cache servers to update data simultaneously. Not so. Jboss cache is a distributed unified management/unified synchronous update.

In the end, the running steps of memcache are as follows: 1. Select a cache server routing algorithm.

2. The hash algorithm is used to write data keys. The hash value contains the unique identifier (IP+ port) of the target cache server.

3. Read data Select the same server to read data based on the hash algorithm of the key.

Support for programming languages

Basically all of them.

Including: Java, C, C ++ and so on.

The underlying implementation

Map Data structure.

Access speed

1 // This is a MAP data structure

Number of concurrent

Millions of requests per second.

Queries on slow machines should run in well under 1ms. High end servers can serve millions of keys per second in throughput.

Run and play

1. The production environment is installed on multiple servers

2. If the test environment is installed on the same server, use different ports.

Work practice

1. The system with small users has hundreds of thousands of users.


Number of nodes Server middleware // Multiple servers, cluster/distributed. The number of nodes is in digits.

Database // branch, but each branch has only one node, no cluster. Only data backup.

Memory // Average gigabytes per service, dozens of services, add up to hundreds of gigabytes.

2. Large user system – the core payment system has hundreds of thousands of merchants. Millions and millions of users.


Number of nodes Server middleware // Multiple servers, cluster/distributed. The number of nodes is ten digits.

Database // sub-library, and each sub-library cluster. Live in the same place, that is, multiple computer rooms. Data backup is available.

Memory // Average gigabytes per service, hundreds of services add up to thousands of gigabytes.

Source code analysis

Underlying principles and implementation

reference

Github.com/memcached/m… www.cnblogs.com/xrq730/p/49…