The preparatory work

Development environment:

  1. JDK 8
  2. Apache Maven

Relying on the jar:

  1. MySQL JDBC driver
  2. MyBatis
  3. jedis
  4. MySQL database connection pool

Pom to introduce the dependent JAR package, build a Spring Boot application.

The thread pool and memory queue are initialized

  • When the Servlet container starts, a thread pool is created and threads are submitted to the thread pool
  • Each thread is associated with a memory queue
  • Subsequent read and write requests need only be dispatched to the queue, from which the thread will pick up and process the request

Encapsulation of two request objects

public interface Request {

    void process(a);
}
Copy the code
  1. Update the database request object
    1. Delete the cache
    2. Updating the database
  2. The request object that reads the database

Request asynchronous Service encapsulation

  1. Route the request to the corresponding memory queue according to the item ID of each request

    Never route the same piece of data to queue A this time and queue B the next time, then asynchronous serialization of memory queue processing is meaningless, only to route to A fixed queue each time to make the scheme work.

            String key  = String.valueOf(productId);
            int    h;
            int    hash = (key == null)?0 : (h = key.hashCode()) ^ (h >>> 16);
    
    		// Modulates the hash value and routes the hash value to the specified memory queue, for example, 8
            // When modulating the hash value with the number of memory queues, the result must be between 0 and 7
            // Any item ID will be fixed to the same memory queue.Copy the code
  2. The request is placed in the corresponding queue to complete the routing operation

Worker thread encapsulation

  1. Associate a memory queue
  2. Process the request in the queue

Two types of request Controller encapsulation

Write requests are relatively easy, so we’ll just talk about read requests.

Read data request Controller

  1. Try to read an item inventory cache from Redis
  2. If the result is read, return directly
  3. Otherwise, wait a while and continue with step 1
  4. If the wait time (200ms) is not read, fetch the data directly from the database and return it

Read request de-retuning

The database update operation for an item’s inventory is already in the memory queue; Then the read operation on the inventory of this item requires reading the inventory data from the database, and then updating it to the cache, multiple reads; In fact, only one read request operation is pressed into the queue.

All other read operations wait for the read request, refresh the cache, and read the latest data in the cache.

reflection

The above steps of code implementation may have some areas to be improved, there may be bugs, bugs and so on. But the basic idea should be there. The figure below lists a detailed landing plan, and gives some thinking and solutions for loopholes.

Photo address: liutianruo-2019-go-go-go.oss-cn-shanghai.aliyuncs.com/%E4%BA%BF%E…