“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

7. The queue

• Using queues is a powerful tool to solve high concurrency and large traffic

• Queues are for asynchronous processing/traffic peak peaking/system decoupling

• Asynchronous processing is one of the main reasons to use queues, such as successful registration, sending coupons/points/red envelopes/SMS/email operations can be handled asynchronously

• Use queue traffic peak clipping, for example, placing concurrent orders or killing seconds. You can consider using queues to temporarily queue the requests and reduce the traffic into flat requests to avoid crushing the application system due to sudden heavy pressure

• Use queues to decouple systems, such as payment success, sending messages to the logistics system, invoice system, inventory system, etc., without directly calling these systems;

• Queue application scenarios

Not all processing has to be real-time;

Not all requests have to tell the user the results in real time;

Not all requests must be successful 100% of the time;

I do not know which system needs my assistance to realize its business processing and ensure final consistency, which does not need strong consistency.

Common message queue products: ActiveMQ/RabbitMQ/RocketMQ/kafka

• ActiveMQ is a well-established message-oriented middleware/message server under the JMS specification

• RabbitMQ/RocketMQ provides excellent data reliability and performance, and is widely used in some financial and e-commerce fields; RocketMQ is owned by Alibaba;

• Kafka is mainly used in the field of big data, data analysis, log analysis and other processing, it may cause message loss problems, it pursues performance, performance is excellent, not the pursuit of data reliability

8. Pooling

In actual development, we often use some pooling techniques to reduce resource consumption and improve system performance.

(1) the object pool

Reduce the resource overhead of object creation and garbage collector collection by reusing objects;

Commons-pool2 can be used;

Object pooling is not common in real projects and is mostly used when developing frameworks or components.

⑵ Database connection pool

Druid/DBCP/C3P0/BoneCP

⑶ Redis connection pool

JedisPool(internal commons-pool2 implementation)

⑷ HttpClient connection pool

Core implementation class: PoolingClientConnectionManager

Hc.apache.org/httpcompone…

5] thread pool

Java provides the java.util.Concurrent package to implement thread pooling

Executors.newFixedThreadPool(8); Fixed number of threads

Executors.newSingleThreadExecutor(); There is only one thread to avoid shutdown situations

Executors.newCachedThreadPool(); Automatic capacity expansion

Executors.newScheduledThreadPool(10); How often to perform

9. Optimization

(1) the JVM to optimize

Setting JVM Parameters

-server -Xmx4g -Xms4g -Xmn256m

-XX:PermSize=128m

-Xss256k

-XX:+DisableExplicitGC

-XX:+UseConcMarkSweepGC

-XX:+CMSParallelRemarkEnabled

-XX:+UseCMSCompactAtFullCollection

-XX:LargePageSizeInBytes=128m

- A server VM runs in two modes: Server and Client. The difference between the two modes is that the Client mode starts quickly while the Server mode starts slowly. However, after entering the stable period and running for a long time, the Server mode program runs much faster than the Client. -XMx2G Maximum heap size -xMS2G Initial heap size -xmn256m The size of the young generation in the heap; -xx :PermSize Sets the initial value of non-heap memory. The default value is 1/64 of physical memory. -xss Stack size for each thread -xx :+DisableExplicitGC. This parameter disables the display of calling GC in code. How does the code show that GC is called, through the system.gc () function. If this JVM startup parameter is included, calling system.gc () in the code has no effect, as if this line of code were absent. -xx :+UseConcMarkSweepGC Concurrent Mark Clearing (CMS) collector, also known as transient pause concurrent collector; -xx :+ cmsparallelEnabled RemarkEnabled - XX: + UseCMSCompactAtFullCollection: when using the concurrent collector, open to the compression of old generation. - XX: LargePageSizeInBytes specified Java heap of paging page size - XX: + UseFastAccessorMethods primitive type fast optimization - XX: + UseCMSInitiatingOccupancyOnly use manually define the initial definition of CMS collection - XX: CMSInitiatingOccupancyFraction began after the CMS use CMS as recycling use 70% collection;Copy the code

2 Tomcat optimization

• To set JVM parameters, refer to JVM optimization parameters

Set JVM parameters in catalina.sh in tomcat bin:

JAVA_OPTS=”-server -XX:+PrintGCDetails -Xmx4g -Xms4g -Xmn256m

-XX:PermSize=128m

-Xss256k

-XX:+DisableExplicitGC

-XX:+UseConcMarkSweepGC

-XX:+CMSParallelRemarkEnabled

-XX:+UseCMSCompactAtFullCollection

-XX:LargePageSizeInBytes=128m

-XX:+UseFastAccessorMethods

-XX:+UseCMSInitiatingOccupancyOnly

-XX:CMSInitiatingOccupancyFraction=70″

• Set the tomcat thread pool size

• Set the I/O mode

• configuration APR