The last article of this series, now hastily put an end to this series, looking forward to the rich content in the future.

capacity

Vertical expansion (vertical expansion)

Improve the capability of a single service (server, database)

However, it will increase the dependency and management of other software facilities in a single service and the internal complexity of the service

Horizontal expansion (horizontal expansion)

Add more service members

However, it will increase the network, database IO overhead, and the difficulty of managing multiple servers

Expansion plan for the database

Determine the business type

  • Multiple read operations: Use the vertical capacity expansion solution (REDis and CDN). Horizontal capacity expansion does not make much sense, because the bottleneck of performance is not the write operation, so it does not need to be completed in real time, and using more servers to share the pressure is too cost-effective. So for a single system to enhance its read performance is ok
  • Multiple write operations: Use horizontal capacity expansion (HBase, add servers, and databases). You can also consider vertical expansion to improve the performance of a single database. However, you may find that the funds and I/O capacity of hard disks are limited, so you need to add more databases to share the write burden.

The cache

Applications need to support a large amount of concurrency, but the performance of the database is limited, so caching is used to reduce database stress and improve access performance

Characteristics of the

  • Hit ratio = Hits/(Hits + misses)
  • Maximum space: maximum cache space
  • Empty strategy: FIFO/LFU/LRU/ Expiration time/random

FIFO: The data that is first entered into the cache is cleared when the cache space is insufficient. To ensure that the latest data can be Used, LFU(Least Frequently Used) : LRU(Least Recently Used) is removed based on the number of accesses. Least recently used, removes the most recently unused element from the accessed element based on access time

Local Cache Guava Cache

In fact, programming time to write member variables, static variables, steady on is also considered cache

Distributed cache Redis

High concurrency cache issues

Cache consistency

Full and incremental synchronization

Determine distributed locks based on business requirements

Cache concurrency

When a large number of requests access the same data that has not been cached, a large number of requests will be sent to the database, resulting in too much pressure on the database and consistency problems. Therefore, the solution is to add a lock for a single data when the cache is acquired, until the cache is successfully rebuilt to obtain the latest data

Cache breakdown/penetration

Query a database does not exist in the data, such as commodity details, query a non-existent ID, every time will access DB, if someone malicious damage, it is likely to directly cause the earth pressure on DB.

Solution: When a key is used to query data, if the corresponding data does not exist in the database, we set the value of the key to a default value.

Cache invalidation

In a high-concurrency environment, if the cache corresponding to the key fails, multiple processes will query the DB at the same time and then set the cache at the same time. At this time, if the key is a hot key in the system or the number of failed keys is large at the same time, the DB traffic volume increases suddenly, causing excessive pressure.

Solution:

  1. Evenly stagger the cache expiration times of keys in the system
  2. When we use key to query data, we first query the cache. If the cache cannot be queried at this time, we use distributed lock to lock the data

Hot key

The challenge is that the value of some Key in the cache (possibly for an application and a promotional item) is stored on one machine in the cluster, causing all traffic to flow to the same machine and becoming a bottleneck in the system, which cannot be solved by increasing machine capacity.

Solution:

  1. Client hotspot key cache: Caches the value corresponding to the hotspot key on the client and sets an expiration time.
  2. The hotspot key is divided into multiple sub-keys and stored on different machines in the cache cluster. The value of these sub-keys is the same as that of the hotspot key.

The message queue

Message queues are designed to solve problems caused by inconsistent production and consumption rates and have the following benefits:

  1. Reduce request response time. For example, the registration function needs to call the third-party interface to send SMS messages, and it may take a lot of time to wait for the response from the third party
  2. Services are decoupled. The master service only cares about the core process, and does not need to know how other unimportant and time-consuming processes are handled and completed, but can only be notified
  3. Flow cutting edge. Requests that do not need to be processed in real time can be cached in the message queue and then sent to the corresponding service for processing

If you want to implement a message queue, the simplest message queue is a message forwarder. There are only three basic functions: message storage, message sending, message deletion. You can use LinkedBlockingQueue and ConcurrentLinkedQueue to implement this

Application of split

Current limiting

Traffic limiting is to solve the problem of high concurrency, when a large number of requests cause the database or server to be overloaded with delays or errors. In the figure, if more than 1 million data is sent to the master library at one time, the IO performance of the server and database will be occupied greatly, resulting in the database being unavailable to other services. It takes a long time for the master library to synchronize data to the slave library

Controlling how many times a piece of code is executed in a given period of time can be done with Guava or Semaphore

Downgrade and circuit breaker

Database cutting, sub-database, sub-table

Cut the library

Database cutting: Database switchover caused by database read/write separation

When the read/write performance of a database reaches its bottleneck, you can determine the read/write ratio based on services, set the database to the master-slave mode to complete read/write separation, and configure read/write permissions for all libraries. When the query service is redundant, load balancing is implemented to distribute the query operation to different secondary libraries to reduce the pressure on the primary library.

This can be done through Spring annotations

Depots table

When the performance of a single library has reached a bottleneck, or when the capacity of a single table has reached a bottleneck, it is still very slow after SQL and index optimization, then the need to separate tables

Horizontal table: the table structure remains unchanged. Data is divided into different tables according to fixed ids. Vertical table routing with IDS is required when writing and querying: The table structure is divided into multiple tables according to the activity of data to improve the processing capacity of different single tables

Question:

  1. Transaction issues. After repository splitting, database transaction management becomes difficult because the data is stored in different libraries. If you rely on the distributed transaction management function of the database itself to execute transactions, it will pay a high performance cost. If the application program to assist control, the formation of program logic transactions, and will cause programming burden.
  2. Join problems across libraries and tables. After the implementation of database and table division, it is inevitable that the data with strong logical correlation will be divided into different tables and different libraries. Therefore, we cannot join the tables in different databases or join the tables with different granularity of table division. As a result, the business that can be completed by one query may need to be completed by multiple queries.
  3. Additional data management burden and data computation stress. The additional data management burden, most notably the location of data and the repeated execution of data additions, deletions, changes, and reviews, can be solved by the application, but inevitably leads to additional logical operations.

okokokokok

Recently summarized some knowledge points related to Java interview, interested friends can maintain together ~ address: github.com/xbox1994/20…