• notice
  • Congratulations on the launch of the Sought Tech site

nginx+tomcat+redis*+mysql (redis focus)

One, nginx

Nignx is a high-performance http and reverse proxy web server, and also provides IMAP/POP3/SMTP services.

In the traditional web server architecture, each client access must be processed by a separate process or thread. As more and more people visit the website and the number of concurrent connections increases, the web server becomes slow. , Thereby reducing the user experience.

Individual processes/threads will consume memory and CPU time.

The goal of NGINX development is to achieve 10more than twice the performance, optimize the use of server resources, and at the same time be able to expand and support the dynamic growth of the website. Therefore, NGINX has become one of the most well-known modular, event-driven, asynchronous, single-threaded web servers and web agents.

Second, tomcat

Tomcat is a servlet container developed by Apache, which supports Servlet and JSP, and provides some unique functions as a Web server, such as Tomcat management and control platform, security domain management and Tomcat valve. Since Tomcat itself also contains an HTTP server, it can also be regarded as a separate Web server. However, you cannot confuse Tomcat with the Apache HTTP server.

There are three containers in tomcat

Web container: We talked about servlet can understand the Java applet that processes data on the server side, so who is responsible for managing the servlet? At this time we will use the web container. It helps us manage servlets and so on, so that we only need to focus on business logic.

jsp container: The jsp container is based on web container management, which translates Java files into binary files, a language that the system can recognize   

serverlet container: process the client's request and fill the response object

The Servlet container implements the Container interface. There are 4 levels of containers in Tomcat: Engine, Host, Context and Wrapper.

Engine: The entire Catalina Servlet engine;

Host: a virtual host containing one or more Context containers;

Context: represents a web application, which can contain multiple wrappers;

Wrapper: Represents an independent Servlet;

Three, Redis

1 advantages and disadvantages

Advantages: Redis runs based on memory and supports persistence (memory operation: excellent processing of hot data, does not occupy too many processes)

The data reading speed can reach up to 110,000 times/s, and the data writing speed can reach up to 81,000 times/s

Atomicity: All redis operations are atomic and support data backup, that is, data backup in master-salve mode

Disadvantages: It does not have automatic fault tolerance and recovery functions. The database is easily limited by physical memory and cannot be read and written with high thermal performance for massive data.

Usage: do partial pressure in front of the database. The three are divided into master-slave mode, sentry, and cluster mode. It is a non-relevant database.

2 The difference between relational and non-relational databases

(1) Different storage methods MySQL uses records as rows and fields as columns to form a table for storage

(2) Non-related database data types hash, string, list, etc.

Different according to the expansion method:

(1) Relational type: horizontal expansion (node expansion, one more node can be added) database sub-database sub-table

(2) Non-related: vertical expansion (hardware resources to expand CPU, disk, or directly upgrade the server)

The Redis cluster does not use a consistent hash, but uses a hash slot method. The redis cluster has 16,384 hash slots. After each key passes the algorithm a bit, it will decide which slot to put in. (The size of each slot is about 512). equally distributed! !

  Note: By default, it is evenly distributed! ! ! In actual production, you need to look at the node performance! !

The support for things is different:

(1) Relational database: especially suitable for high transactional requirements and tasks that need to control the execution plan

(2) Non-relationship: It will be slightly weaker here, and its value lies in high scalability and large data processing

Relational databases and non-relational databases have their own characteristics and application scenarios. The combination of the two will bring new ideas to the web2.0 database.

MySQL pre-cache database--"redis

Cache some backends for front-end services--"Cache pictures for nginx (i/o and network resources are under more pressure)

MySQL hot database--"redis

ps: Non-related databases do not need to manually create databases and collections (the non-related databases themselves contain 0-15, a total of 16 numbers)

Is the redis server program single-process or multi-process

The epull mechanism is used to execute a single process to implement an operation with high concurrency, but when redis improves high concurrency processing capabilities, it will put a lot of pressure on the server's cpu. It is recommended to start 2 processes in production:

1 backup

2 Try not to put pressure on the cpu while resisting high concurrency

3What is a business?

A transaction is a separate isolated operation: all commands in the transaction will be serialized and executed in order. During the execution process, it will not be interrupted by messages sent by other clients.

The essence of Redis transactions is a collection of commands such as MULTI, EXEC, and WATCH. The transaction supports the execution of multiple commands at once, and all commands in a transaction will be serialized. During the transaction execution process, the commands in the execution queue will be serialized in order, and the command requests submitted by other clients will not be inserted into the transaction execution command sequence.

To sum up: Redis transaction is a one-time, sequential and exclusive execution of a series of commands in a queue.

 During the client's visit, because the webpage is not loaded and the client keeps refreshing, the same session will be established. When accessing redis, there will be repeated sessions. Redis can be controlled by the session session pool.

Redis has rich features-redis also supports publish (publish message)/subscribe (subscribe), notification, set key validity period and other features.

redis concept penetration, breakdown, avalanche

4 distributed lock

         After a process comes in, the first thread will be processed in redis. At this time, a lock will be added to the first place to prevent subsequent processes from entering and causing congestion. Here is a watch dog to monitor the lock. If the request is processed, it will be automatically unlocked (for example, it will be automatically disconnected after 30s, and the lock will automatically release time). If the specified time is not processed, the watch dog will not be unlocked. Wait for completion manually or automatically Perform unlocking. Put it into the next process for processing.

Core technology: setnx+distributed lock

5. RDB! ! ! !

Once save is triggered, all tasks of the child process will be blocked.
Triggering methods:
1 Manual trigger: Directly execute the bgsave persistence command.
2 Automatic trigger: Trigger according to the number of statements executed in the specified period. For example, save mn ---- in the configuration file -"When save 90 10
bgsave is executed, it will control the location of the RDB Forbidden City to create .rdb files. The characteristic is that the files produced by RDB persistence are only recorded in
the full backup. In order to improve the processing speed and reduce the landing The volume occupied by the disk, RDB data will be compressed.
The data in the memory disappears. Once the data fails to load, it will fail to start.
Special trigger method: Actively close redis, but the RDB persistence priority is lower.
Application scenarios:
1 When actively closed, trigger RDB (redis turns on RDB by default)
2 Main During replication, when the master synchronizes to SLAVE, it will trigger RDB (full replication), and then trigger the incremental replication of AOF.
3 When redis restarts, when AOF is not turned on, data will be restored based on the RDB file and redis will run.

What is added is to add records every day, synchronize the data in the buffer to the disk, and then make it persistent.

6 trigger conditions

1always One is synchronized every time one is generated. This may cause high data pressure, resulting in frequent writes. Each write to the buffer will cause the I/O to occupy too much,
and the additional part will cause the buffer to be full and the synchronization rate will decrease. (Almost not used in production)
2no does not automatically trigger    
3everysecond Synchronize once per second. It can reduce the pressure at a certain level, but it may also cause the buffer to be full.
Distribute before redis:
1 token bucket algorithm (that is, a bucket slows down tokens, One call can put 100 tokens, set the token speed to pass)
2 leaky bucket algorithm (capacity 100, will fill, can control the filling and falling speed), current limit.
3 The firewall controls the flow or number of times.
4 The lru algorithm controls the storage time of the hot data in the memory (redis stores all the hot data when it is used as the MySQL cache database.)

7 Elimination strategy

The default is lazy deletion

Eliminated immediately

Eliminate data with fewer recent hits. As time goes by, the end value will decrease. In the so-called data, under the premise of doing high-hot data, or the set of data with fewer recent hits (relatively speaking, it is not important data) ).

Periodically delete ———— "When the two types of persistence are guaranteed to be completed, and the business is not busy/terminated at the same time, delete the function module: AOF persistence in order to solve the pressure of the cache.

8 principle of master-slave replication

9 Principles of Sentinel Mode

1 The sentinel has its own configuration file, as long as it points to the master node IP.

2 The sentinel node will establish two connection command connections and subscription connections with the configured master node

PS: redis publish and subscribe (pub/sub) is an information communication mode: the sender sends a message, and the subscriber receives the message

3 The sentry will send an INFO command every 10s through the command connection. Through the INFO command, the master node will return its own run_id and its own slave node information.

4 The sentry will establish two connection commands for these slave nodes, connection and subscription connection

5 The sentry sends an INFO command to the slave node through the command connection, and obtains some of his information:

run id (redis server id)


Copy offset offset from server


6 Send a message to the server's sentinel: hello channel through a command connection, including your own IP port, run_id, configuration (will be used in subsequent voting, etc.)

7 By subscribing to the sentinel: hello channel of the target server to monitor, so all sentinel messages sent to this channel can be received

8 Analyze the monitored messages, analyze and extract, you can know which other sentinel service nodes are also listening to these master and slave nodes, update the structure to record these sentinel nodes

9 listen to other sentinel nodes observed

Fourth, the MHA of MySQL

It provides an automating master failover (automated master failover) function for the MySQL master-slave replication architecture. When MHA monitors the failure of the master node, it will promote the slave node with the latest data to become the new master node. During this period, MHA will obtain additional information from other slave nodes to avoid consistency problems. MHA also provides the online switching function of the master node, that is, switching the master/slave node on demand.

Five, keepalived

Keepalived is a highly available software based on the vrrp protocol. Keepalived has a primary server and multiple backup servers. The same service configuration is deployed on the primary and backup servers. When a weapon fails, the virtual IP address will automatically drift to the backup server.

VRRP (Virtual Router Redundancy Protocol)

VRRP is to solve the high availability of static routing.

1. Database concept (reading method)

Dirty read: Transaction A can read data that has not yet been committed by transaction B. Once transaction B is rolled back, transaction A will not be able to read it again.

Phantom reading: both a and b are reading the database, a changes the database and saves it, and when b reads again in the same path, some more content will be found.

Non-repeatable read: A transaction reads the same record successively, and the data is modified by other transactions between two reads, the data read twice is different, we call it non-repeatable read.

2. Four characteristics of database transactions

(1) Atomicity: all database operations included in the transaction either all succeed or all fail and roll back

(2) Consistency: A box of transactions must be in a consistent state before and after execution.

(3) Isolation: When multiple users access the database concurrently, the transaction opened by the database for each user cannot be disturbed by the operation data of other transactions. Multiple concurrent transactions must be isolated from each other.

(4) Persistence: Once a transaction is committed, its changes to the data in the database are permanent, and then even if the database fails, it should not have any impact on it.


Technical otaku

Sought technology together

Related Topic


Leave a Reply