The following content is excerpted from Luo hao’s post on Java interview questions (below) for frequent reading. Some interview questions about Java are strongly recommended to visit his personal blog, which is quite wonderful.
- layeredLayering is one of the most common approaches to dealing with any complex system. It divides the system horizontally into several layers, each with a single responsibility, and then forms a complete complex system through the infrastructure and services provided by the lower layer to the upper layer and the call from the upper layer to the lower layer.Computer networkThe open Systems Interconnection Reference Model (OSI/RM) and the TCP/IP model of the Internet are both layered structures. The software system of large websites can also be divided into persistence layer (providing data storage and access services), business layer (processing business logic, The core part of the system) and the presentation layer (system interaction, view presentation). It needs to be pointed out that :(1) layering is a logical division. Physically, different functional modules can be deployed on the same device or on different devices. In this way, more computing resources can be used to cope with concurrent access by users.
(2) There should be clear boundaries between layers, so that the layers are meaningful and more conducive to software development and maintenance.
- Segmentation: Segmentation is the vertical segmentation of software. We can separate the different functions and services of a large website into functional modules (units) with high cohesion and low coupling. At the beginning of the design can be a coarse-grained segmentation, the site is divided into several functional modules, each module can be further to late on granular segmentation and so on the one hand helps software development and maintenance, on the other hand help distributed deployment, provide website concurrent processing capability and function extension. – Distributed: In addition to the content mentioned above, static resources (such as JavaScript, CSS, and images) on a website can also be independently distributed and have independent domain names. In this way, the load on application servers can be reduced and the browser can load resources faster. Data access should also be distributed. Traditional commercial-grade relational database products almost all support distributed deployment, while new NoSQL products are almost all distributed. Of course, the business processing of the website background also needs to use distributed technology, such as query index construction, data analysis, etc. These business computing scale is huge, which can be processed by Hadoop and MapReduce distributed computing framework.
- Clustering: Clustering enables more servers to provide the same service, which can better support concurrency.
- Caching: Caching is the technology of trading space for time, placing data as close to the calculation as possible. Using caching is the first law of website optimization. CDN, reverse proxy, hot data are all uses of caching technology.
- Asynchrony: Asynchrony is another important means of decoupling software entities. Asynchronous architecture is a typical producer-consumer model. There is no direct call relationship between the two. As long as the data structure remains unchanged, each other’s function implementation can be changed at will without affecting each other, which is very beneficial to the expansion of the website. Using asynchronous processing can also improve system availability, speed up site response times (loading data with Ajax is an asynchronous technique), and provide peak clipping (dealing with instantaneous high concurrency). ; Can delay processing to delay processing “is the second law of website optimization, and asynchronous is an important means to practice the second law of website optimization.
- Redundancy: Redundant servers should be provided for each type of server so that the site can work properly if one or more servers go down, and disaster recovery is possible. Redundancy is an important guarantee of high availability.
- Browser Access optimization:
- Reduce the number of HTTP requests: Merge CSS, Merge JavaScript, merge images (CSS Sprite)
- Use browser caching: Cache CSS, JavaScript, and images in the browser by setting cache-Control and Expires attributes in HTTP response headers. When static resources need to be updated, references in HTML files can be updated to make the browser request new resources
- Enable compression
- CSS front, JavaScript back – reduces Cookie transfer
- CDN acceleration: The essence of a Content Distribute Network (CDN) is cache. Data is cached in the nearest place to users. A CDN is usually deployed in the equipment room of a Network carrier, which not only improves the response speed but also reduces the pressure on application servers. Of course, CDN caches are usually static resources.
- Reverse proxy: A reverse proxy is a front of an application server. It protects website security and implements load balancing. Most importantly, it caches hotspot resources accessed by users and directly returns certain contents to users’ browsers through the reverse proxy.
- Distributed cache: The essence of cache is hash table in memory. If a high quality hash function is designed, then theoretically the asymptotic time complexity of hash table reading and writing is O(1). The cache is mainly used to store data with a high read/write ratio and little change. In this way, the application program reads data from the cache first. If there is no data or the data is invalid, the application program accesses the database or file system and writes the data to the cache according to the proposed rules. The access to website data also conforms to Pareto distribution (power law distribution), that is, 80% of the access is concentrated on 20% of the data, if the 20% of the data can be cached, then the performance of the system will be significantly improved. Of course, using caching requires solving the following problems:
- Frequently modified data;
- Data inconsistency with dirty read;
- Cache avalanches (which can be overcome by clusters of distributed cache servers, memcached being a widely used solution);
- Cache preheating;
- Cache penetration (malicious persistent requests for nonexistent data).
- Asynchronous operation: You can use message queues to asynchronize invocations and use asynchronous processing to store short, highly concurrent event messages in message queues for peak shaving. E-commerce websites can store users’ order requests into message queues when carrying out promotional activities, which can resist the impact of a large number of concurrent order requests on the system and database. At present, the vast majority of e-commerce websites use message queues to process order systems even if they do not carry out promotional activities.
- Use clusters.
- Code optimization:
- Multithreading: Java-based Web development basically responds to users’ concurrent requests through multithreading. Using multithreading technology in programming to solve thread safety problems, we can mainly consider the following aspects: A. Design objects as stateless objects (which contradicts the object-oriented view of programming and is considered bad design in the object-oriented world) so that there is no problem of inconsistent object states during concurrent access. B. Create objects within the method so that the object is created by the thread that enters the method, without the problem of multiple threads accessing the same object. It is also good practice to use ThreadLocal to bind objects to threads, as discussed earlier. C. Use a proper locking mechanism when concurrently accessing resources.
- Non-blocking I/O: The use of single-threaded and non-blocking I/O is currently recognized as a more efficient application mode than multi-threading, which is used in node.js-based servers. Java’s introduction of NIO (non-blocking I/O) in JDK 1.4 and the concept of asynchronous servlets in the Servlet 3 specification provide the necessary foundation for non-blocking I/O adoption on the server side.
- Resource reuse: there are two main ways for resource reuse, it is a singleton, 2 it is object pooling, the database connection pool, we use the thread pool is object pooling, this is typical of using space for time strategy, on the other hand also realize the reuse of resources, so as to avoid the unnecessary to create and release resources brought about by the overhead.