High Concurrency Summary

High concurrency

Solutions and methods

  • Expansion Horizontal expansion, vertical expansion
  • Introduction and use of cache Redis, Memcache, Guava, Cache
  • Queue Kafka, RabbitMQ, RocketMQ and other queue features introduction and the focus of using queue
  • Application Split Service Dubbo and Microservice Spring Cloud Introduction
  • Introduction and use of current limiting Guava RateLimiter, commonly used current limiting algorithms, distributed current limiting implemented by yourself
  • Various options for service degradation and circuit breaker service degradation, introduction and use of hystrix
  • Database and database cutting, sub-database, sub-table Introduction to database and database-slicing, sub-database, and sub-table support the principle and implementation of multiple data sources
  • Some means of high availability Task scheduling distributed elastic-job, implementation of active and standby curator, monitoring and alarm mechanism

    Thread-safe handling of concurrency and concurrency

    Thread safety is manifested in atomicity, visibility, ordering, atomic package, cas algorithm, synchorized and lock, volatile, happy-before

    Security manifests itself as correct behavior

    atomicity
    Atomic can only update one value
  • CAS AtomicInteger uses the getAndAddInt() method of an unsafe class, which uses the CAS lock for parameter operations compareAndSwap
  • AtomicLong is accurate, jdk8 has added a LongAdder
  • LongAdder spreads the high concurrency pressure of a single point to each point
  • AtomicReference

    private static AtomicReference count = new AtomicReference<>(0);
    If atomicReference.compareAndSet(0, 2) is 0, update it to 2, otherwise it will not be executed, the updated field

  • AtomicReferenceFieldUpdater

    private static AtomicIntegerFieldUpdater updater =
    AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class, "count");
    @Getter
    public volatile int count = 100;//Update the volatile field in an instance of the class
    atomicReference.compareAndSet(example5, 100, 120)

  • AtomicStampReference solves the ABA problem of CAS plus version number
  • AtomicLongArray array updates the value of the index
  • AtomicBoolean is suitable for code execution only once in the case of high concurrency

    Synchronized Uninterruptible
  • decorated code block
  • Modified method (alternate execution)
  • The modified static method acts on all objects (executed in sequence)
  • Decorative class

    Lock is interruptible
  • ReentrantLock

    visibility
  • Thread cross execution
  • Reordering combined with thread interleaving
  • The updated value of the shared variable is not updated in time between the working memory and the main memory

    orderliness

    Safely Publishing Objects Safely Publishing Methods, Immutable Objects, Final Keyword Use, Immutable Methods, Thread-Unsafe Classes and Writing

    Thread safety means stack closure, ThreadLocal thread closure, JDBC thread closure, synchronization container, concurrent container, JUC

    AQS and other JUC components CountDownLatch, Semaphore, CyclicBarrier, ReentrantLock and lock, Condition, FutureTask, Fork/Join framework, BlockingQueue

    Disadvantages of thread pool newThread, benefits of thread pool, ThreadPoolExecutor, Executor framework interface

    Additional supplementary deadlock generation and prevention, best practices for multi-threaded concurrency, thread safety in spring, in-depth explanation of hashmap and concurrentHashMap


    Basic knowledge explanation and core knowledge preparation

    Concurrency, high concurrency related concepts

    CPU multi-level cache cache coherence out-of-order execution optimization

    cpu multilevel cache

  • The reason for the appearance of cpu cache alleviates the problem that the speed of cpu and hard disk does not match
  • The meaning of cpu cache time locality, space locality

    Cache Coherent MESI

  • M (modified) E (exclusive) S (shared) I (invalid) to ensure the consistency of cache shared data among multiple CPU caches
  • local read,local write,remote read,remote write

    Out-of-order execution optimization

    Optimizations made by the processor to improve the operation speed against the original order of the code

    Java memory model JMM regulations, abstract structure synchronization operations and rules

    java memory model (JMM)

  • stack The local variables of the two stack garbage collection method areas are quickly and dynamically allocated to save the object
  • heap heap static variable basic data type reference object shared

    Synchronized eight operations
  • locklock
  • unlockunlock
  • read read
  • load
  • use use
  • assignassignment
  • store storage
  • writewrite

    Concurrency advantages and risks

    risk

  • Security Multi-threaded operation data inconsistency
  • Liveness problems occur when an operation cannot continue, such as deadlocks, starvation
  • Excessive use of performance threads leads to frequent cpu switching, increased scheduling time, synchronization mechanism, and excessive memory consumption

    Advantage

  • Speed ​​processing multiple requests at the same time, faster response, complex operations can be divided into multiple processes at the same time
  • Design programming is simpler in some cases and can also have more options
  • Resource utilization cpu can do other things while waiting for IO

    Concurrency simulation Postman, JMeter, Apache Bench, code

    Postman

  • Just download
  • Click on the collections on the left side border to create a collection
  • Add test addresses to this collection (click save on the right)
  • Click the right-click arrow on the collection and click run
  • Configuration parameters Iterations times iterations delay time

    Apache Bench

  • Install
  • Change all paths in httpd.conf file to put path instead
  • Configure ServerName=127.0.0.1

    Command .\ab -n 1000 -c 50 http://127.0.0.1:8080/test 1000 requests, 50 concurrent

    Detailed analysis of the returned result
  • Concurrency level Concurrency
  • Time taken for tests
  • complete requests The number of completed requests
  • failed requests Number of failed requests
  • total transferred The sum of the response data lengths of all requests, including http
  • html transferred The sum of the data in the body of all requests, which is the result of subtracting http from the above
  • requests per second throughput is related to the number of concurrency, it is complete requests/Time taken for tests
  • time per request Average waiting time for users
  • time per request server average time
  • transfer rate The length of the request unit time to get the data from the server is total transferred/Time taken for tests

    JMeter

  • Download and run jmeter.bat
  • Right-click the test plan in the left sidebar and click add threads treadown-thread-group in turn

    Specific parameters
  • name test name
  • number of threads number of virtual users
  • ramp-up period virtual user growth time, set how many seconds to complete the operation
  • loop count How many times the user sends the request
  • Configure http request Right-click add sampler http-request to configure
  • Configure the listener right click add listener graph-result (graph result) and view results tree (view result tree)
  • Configure log Click options Click log view

    code

  • CountDownLatch outputs after the control thread is executed
  • Semaphore controls the execution of threads

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325298320&siteId=291194637