Solution to slow response of Tomcat server

Solution to slow response of Tomcat server

1. Analytical thinking

1. Eliminate the machine's own reasons

2. Server performance analysis

3. Analysis of the project itself (not detailed)

4. Virtual Machine Analysis

5. Database Analysis

2. Detailed analysis method

1. Eliminate the machine's own reasons

You can use webmaster tools to test your website speed.

2. Server performance analysis

Use the top command to view the resource usage of the server, mainly analyzing the CPU and memory usage (the top command is a commonly used performance analysis tool under Linux, which can display the resource usage of each process in the system in real time. The process list is refreshed every 5 seconds by default, so it is similar to the Windows Task Manager.):

The third line shows the CPU usage, the detailed meaning is as follows:

us---the percentage of CPU occupied by user space, sy---the percentage of CPU occupied by kernel space, ni---the percentage of CPU occupied by processes whose priority has been changed, id---the percentage of idle CPU, wa---the percentage of CPU occupied by IO wait, hi---the percentage of CPU occupied by hard interrupts (Hardware IRQ), si---the percentage of CPU occupied by soft interrupts (Software Interrupts), st---Steal Time, the actual CPU time allocated to tasks running on other virtual machines on the host, generally only in the virtual machine OS.

The 4th line shows the current memory situation. The total server memory is 8054352k, 2879468k is used, 5174884k is left, and 265728k is buffered.

My personal understanding is: when the us percentage is less than 50%, there is no need to consider the server configuration problem. If the server's us percentage is above 70% for a long time, you can consider strengthening the server's hardware configuration. In addition, you also need to check the network status of the server. You can basically determine the network status by downloading a large file.

3. Analysis of the project itself

If you use a JDBC connection pool, you need to analyze the configuration of the connection pool (analyze the maximum number of thread pools, release time, etc.).

Here, taking C3P0 as an example, the following is the configuration of a project I once worked on, as shown below:

This was originally just a configuration plan for local testing. Due to carelessness, I forgot to modify it after going online. When multiple people visit, the waiting connection timeout will occur. We need to set appropriate configuration data according to the actual situation of the project.

It is also possible that the slow response is caused by unreasonable project design, which I will not explain in detail here.

checkoutTimeout---When the connection pool is exhausted, the client waits for a new connection after calling getConnection(). After the timeout, a SQLException will be thrown. If it is set to 0, it will wait indefinitely. The unit is milliseconds. Default: 0
minPoolSize --- The minimum number of connections retained in the connection pool, the default is: 3
maxPoolSize---The maximum number of connections retained in the connection pool. Default value: 15
maxIdleTime---Maximum idle time. If the connection is not used within the set time, it will be discarded. If it is 0, it will never be discarded. Default value: 0
maxIdleTimeExcessConnections---default: 0 Unit s This configuration is mainly used to reduce the load on the connection pool. For example, the number of connections in the connection pool may create a lot of data connections due to a peak in data access, but the number of database connections required in the subsequent time period is very small. At this time, the connection pool does not need to maintain so many connections. Therefore, it is necessary to disconnect and discard some connections to reduce the load. It must be less than maxIdleTime. If the configuration is not 0, the number of connections in the connection pool will be maintained to minPoolSize. If it is 0, acquireIncrement is not processed - the number of connections that c3p0 acquires at one time when the connection pool is exhausted. Default value: 3

4. Virtual Machine Analysis

Use the top command to view the memory usage of the virtual machine. Sometimes you can find that although the percentage of memory occupied by the virtual machine is not large, there is an obvious upper limit. We need to check the configuration of the virtual machine.

Solution (taking Tomcat as an example):

The specific value depends on the actual situation.

5. Database Analysis (MySql)

There are many aspects to consider when analyzing a database. Here are just a few of the situations I have encountered:

a. Maximum number of connections

show variables like '%max_connections%'; View the maximum number of connections show status like 'Threads%'; Current connection usage

Threads_connected --- Number of open connections

Threads_running---This value refers to the number of active connections, which is generally much lower than the connected value

If the value of the maximum number of connections is too small, you can modify it according to the actual situation. Generally, it can be changed to 1000. There are two ways to set it:

1. Temporary settings, will become invalid after restarting the service

2. Modify the database configuration file

Add or delete a line under [mysqld] in the /etc/my.cnf file: max_connections = 1000

b. Timeout control

MySQL has a property called "wait_timeout", the default value is 28800 seconds (8 hours), the value of wait_timeout can be set, but the maximum value can only be 2147483, no larger. That is about 24.85 days, which can be viewed through the show global variables like 'wait_timeout'; command.

The meaning of wait_timeout is: if a connection is idle for more than 8 hours, Mysql will automatically disconnect the connection. In layman's terms, if a connection is inactive for 8 hours, it will be automatically disconnected. Since dbcp does not check whether the connection is valid, exceptions will occur when using it to perform data operations.

If the problem is caused by timeout control, it is not recommended to modify the wait_timeout value. The problem can be solved by adding "&autoReconnect=true&failOverReadOnly=false" after the database connection URL.

c. DNS reverse resolution

When the MySQL database receives a network connection, it first obtains the other party's IP address, and then performs reverse DNS resolution on the IP address to obtain the host name corresponding to the IP address. Use the host name to determine permissions in the permission system. Reverse DNS resolution is time-consuming and may appear slow to the user. Sometimes, the host name resolved by reverse analysis does not point to this IP address, and the connection cannot be successful. You can disable MySQL from performing reverse DNS resolution in the configuration file by adding the following line to the [mysqld] section of my.cnf:

skip-name-resolve (same for Windows and Linux)

d. Table Cache

show global status like 'open%tables%'; View the number of open tables:

open_tables: is the number of open tables currently in the cache.

opened_tables: is the number of tables opened since MySQL was started.

When the Opened_tables value is very large, it means that the cache is too small, resulting in frequent table opens. You can check the current table_open_cache setting:

show variables like 'table_open_cache'; View the upper limit of the cache

There are two ways to set the value of table_open_cache (if the server has about 4G memory, it is recommended to set it to 2048):

1. Temporary settings, will become invalid after restarting the service

set global table_open_cache=2048;

2. Modify the database configuration file

Add or delete a line under [mysqld] in the /etc/my.cnf file: table_open_cache = 2048

e. Slow query log

The purpose of recording slow query logs is to confirm whether the slow response of the server is caused by the slow execution of certain statements.

I won’t go into detail about slow queries, you can find a lot of them online.

However, in the end, according to my actual project analysis, there is no problem with these. It is MongoDb's CPU that is directly full. Just comment it out.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed tutorial on installing Tomcat8.5 in Centos8.2 cloud server environment
  • Alibaba Cloud Server Tomcat cannot be accessed
  • Detailed steps for configuring Tomcat server in IDEA 2020
  • Detailed steps for installing JDK and Tomcat on Linux cloud server (recommended)
  • Tomcat Server Getting Started Super Detailed Tutorial

<<:  Simply understand the differences in the principles of common SQL delete statements

>>:  Singleton design pattern in JavaScript

Recommend

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

A complete list of commonly used Linux commands (recommended collection)

Table of contents 1. System Information 2. Shutdo...

About Generics of C++ TpeScript Series

Table of contents 1. Template 2. Generics 3. Gene...

Solution to Apache cross-domain resource access error

In many cases, large and medium-sized websites wi...

Some basic instructions of docker

Table of contents Some basic instructions 1. Chec...

HTML Tutorial: Collection of commonly used HTML tags (6)

Related articles: Beginners learn some HTML tags ...

Detailed explanation of the principle and function of Vue list rendering key

Table of contents The principle and function of l...

404 error occurs when accessing the homepage of tomcat started in Docker mode

Scenario: When starting tomcat in docker (version...

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...

Summary of seven MySQL JOIN types

Before we begin, we create two tables to demonstr...

Two practical ways to enable proxy in React

Two ways to enable proxy React does not have enca...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...