[Log] concurrency problems, optimization measures, FAQ location,

Highly concurrent systems design

  • Atomic operation of
    common and variable multithreaded atom, and other database transaction
    jdk cmpxchgl supported by the cpu command, implements atomicity of CAS operation AtomicInteger

mysql innodb to protect the House by way of write-ahead transaction log atomicity, consistency, persistence
include redo logs and undo logs

redo redo logs transaction operation process when the system needs to restart when the system is down, yet the memory of the rain persisted to disk data recovery
undo log can fail when the transaction execution, using the information to restore data to undo the state before the transaction execution

The transaction log can improve the efficiency of transactions executed by the
storage engine only need to modify the behavior of the transaction log persistence to them,
only make changes in the amount of memory copy of the data,
do not need to modify each time the data will be written back to disk,
the log is written is the order i / o, disk write data back into the database is a small area of random i / o

  • Multi-thread synchronization
    can be achieved synchronization between threads through the synchronized keyword and locks

Using synchronized code block
explicitly specified lock object parameter is
locked non-fair

Use ReentrantLock lock to achieve synchronization between threads
ReentrantLock can specify the wait time, the wait can be interrupted and
can be used in constructing a fair lock lock, the default non-fair

Fair locks: multiple threads at the same time waiting for a lock, the lock must apply in accordance with the order to obtain a lock

  • Data consistency

Distributed systems are often used to improve the reliability and fault tolerance of the system by copying the data,
the data replicas on different machines, they will face the problem of maintaining data consistency

Strong consistency requirements regardless update data is performed on that copy, after the read operation can get to the latest data

This case it is necessary to guarantee the atomicity operation by distributed transaction, and can not be read outside to the intermediate state of the system

Weak consistency: After the data update is also possible to read the contents of the pre-update

Final consistency: to ensure that the user can read at a later time data are consistent

  • Concurrency scenarios
    spike, etc.

https://yq.aliyun.com/articles/618443
https://www.cnblogs.com/andy-zhou/p/5364136.html

Be supported by a data cache and sub-library

Avoid data inconsistencies occur, you can use the actual inventory and inventory browse separate ways

The front view inventory information is stored in the cache
back-end real inventory stored in the database,
these two operations can be performed in a single transaction
and finally the data in the database is synchronized to the cache

Using the read-write lock

When used in the case of row locks, lock contention between threads perform, there will be
the advantages of multi-threaded process occurs wait, can not play

You can line stock split into multiple lines, they can lift the row lock problem

optimization

The long execution time sql print
show variables like 'log_slow_queries'

java optimization

  • java single embodiment, savings memory overhead

  • Use Future

  • Thread pool, make full use of machine resources

  • io selector

  • When too many threads, round-robin cause context switching overhead

  • Reduced lock contention, to shorten the holding time of the lock, the block synchronization code streamlined, reducing lock granularity, an exclusive lock multi-less read-write lock

  • Transmission of data compression

  • Query data cache

sql optimization

mysql indexes are implemented in the storage engine level,
most are using a B-tree data structure,
you can use the explain command to analyze and explain the sql query
try to avoid full table scan

When the query column is not independent, but part of an expression or function,
MySQL will not use the column index, a full table scan

When the column to be queried fuzzy matching, even if indexed, will be a full table scan

Although the consolidation strategy indexing can improve query condition combination under certain conditions,
however, can be avoided by establishing a multi-column index merge index brought overhead to the database

The most left-prefix principle
if the query is not in accordance with the leftmost column index to start a query, you can not use the index to a combination
order by the statement and the group by statements also follow the principle of the most left-prefix

  • Rational use of the index
  • Anti-paradigm design
    will be listed some common queries associated with redundant storage, reducing table associated with the query and bring the full table scan random io
  • Use the query cache
  • Use a search engine
  • Use key-value database
jsp  # 输出jvm虚拟机进程的一些信息
jstat # 对虚拟机各种运行状态进行监控
jinfo # 查看应用程序的配置参数,还能够在运行期修改jvm参数
jstack # 生成jvm当前的线程快照信息,就是每个执行方法堆栈集合
jmap # 查看等待回收对象的队列,查看堆的概要信息

BTrace java program dynamic tracking tool


Memory overflow dump the current stack information


Thread deadlock because of competition for resources, possibly because the semaphore did not release other issues
will result in a long time did not respond to the request, the application of dead
at this time is very low system resources consumption cup load


Class loading conflicts
together at the start jvm -verbose: class, you can view the specific class of how loaded from the jar


Links: https://pan.baidu.com/s/1Kw4T9IVfTPcvG5PcoNG4YQ Password: 6ee9 "large, distributed architecture design website"

Guess you like

Origin blog.csdn.net/java_sparrow/article/details/90340226