Java performance optimization to create a billion-level traffic spike system: 3. Distributed expansion

3. Distributed expansion

TPS: Transactions Per Second (the number of transactions processed per second)

1. Nginx reverse proxy load balancing

There is a capacity problem for a single machine, and it needs to be extended to multiple machines.

Nginx reverse proxy can expose the back-end server cluster to a unified domain name for users to access.
image.png
image.png

1.Nginx

  • Can be a static web server
  • It can be used as a dynamic and static separation server, directly processing static resource requests, forwarding dynamic requests to the corresponding back-end service, and returning it to the front-end in the form of ajax, that is
  • Be a reverse proxy server

2. Architecture diagram iteration:

image.pngimage.png

3. Compress the consumption of network connection

By default, nginx will use http1.0 to proxy with the backend server, and http1.0 does not support long connections, so you need to change to http1.1 and set the connection to empty (default long connection, set close to short connection )

The application and the database maintain a long connection through the Druid connection pool.

4. Reasons for Nginx's high performance

(1) epoll multiplexing

  1. java bio model

image.png
The client initiates socket.write to write to the buffer first, and then returns, but when the TCP/IP buffer is full, this function is blocked there.

  1. linux select model

image.png
Change to trigger rotation search, 1024 is online

  1. linux epoll model

The change triggers the callback to read directly, which is theoretically unlimited.
image.png

There is a NIO model in java jdk with a select function. When the linux kernel is above 2.6, it will automatically convert the select model into an epoll model.

(2)master worker

The master is the parent thread of the worker and can manage the memory resources of the child threads.
When receiving a request for TCP/IP to establish a connection, the worker door grabs the lock of an accpet area in the memory. Whoever grabs it will establish a connection with the client and be responsible for subsequent communication.
The reason why the reload operation can restart nginx smoothly is also the same. Instead of changing the master, the worker is replaced with a new one.

Why can it support a lot of concurrency without multithreading?

Why should there be multiple threads? It is because some IO blocking methods or time-consuming processing are called, which will cause other operations to block.
Because of the blocking, the use of multithreading can improve the processing capacity, but there is no blocking when using epoll in the nginx worker process, so a single process can handle high concurrency.
image.png

(3) Coroutine mechanism

  • Depends on the memory model of the thread, and the switching overhead is small.
  • Control will be returned in case of blockage, and the code will be synchronized
  • No need to lock

Lua also uses this coroutine mechanism

Two, distributed session management

Session management:

  • Transfer sessionid based on cookie: The realization of session by the tomcat container is stored in the cookie.

However, in many clients, the use of cookies is prohibited on mobile phones, so the following practices are more popular:

  • Similar to sessionid based on token transmission: java code session implementation.

But after migrating to a distributed architecture, there will be no logged-in session information on another machine. Therefore, distributed session management is required.

  • Transfer sessionid based on cookie: the realization of session by tomcat container, migrate to redis
  • Similar to sessionid based on token transmission: java code session implementation. Migrate to redis

Introduction to Redis:

  • Centralized cache middleware
  • Nosql type key-value database
  • Memory cache
  • Very suitable for distributed session storage (there is no need to turn on the redis persistent storage disk function, just use its in-memory database and store the cache)

Specific operation:

  1. Import package

    image.png
    Once the package is imported and the redis address is filled in, session management is automatically performed.

  2. token is a more general approach

Third, use redis to implement distributed session storage

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/108010901