Interview Java Server Development

Source: Internet
Author: User
Tags jboss

1. How to Implement cache using Java?

Java has its own cache Input and Output classes, such as inputstream and fileoutputstram, which can be viewed by the API. If you want to implement it by yourself, you can set a byte array that is large enough, put what you need into the cache.

1. Why does cache exist?
Generally, a website or an application requests the application server in the form of a browser. The application server then requests the database after performing a bunch of calculations,
After receiving the request, the database returns the data to the application server after a heap of computation. Then, the application server returns the data to the browser after a heap of computation.
This is a standard process, but with the popularization of Internet connection, there are more and more people accessing the internet, and the amount of information on the Internet is also increasing.
Our applications need to support an increasing number of concurrent requests. Then, our application servers and database servers are making more and more calculations,
However, our application server resources are usually limited, and the number of requests received by the database per second is also limited (who is it that the hard drive speed of our users is limited ).
If limited resources are used to provide as much throughput as possible, one way is to reduce the computing workload and shorten the request process (reduce network I/O or hard disk I/O ),
At this time, the cache can be used. The basic principle of the cache is to break the standard process described in. In this standard process,
All links can be disconnected. requests can be retrieved from the cache and directly returned. This not only saves time, but also improves the response speed,
It also saves hardware resources and enables our limited hardware resources to serve more users.

2. Where can the cache exist?
Java code

  1. Browser --- Between the browser and the app --- the app-database through the Layer

Browser --- Between the browser and the app --- the app-database through the Layer

In, we can see the general process of a request. Next we will redraw this figure to make our structure a little more complex.
(Layer Apps)
Browser --- Between the browser and the app --- the app-database through the Layer

Theoretically, any part of the request can be cached. The first part is the browser. If the data is stored in the browser,

The speed is the fastest for users, because there is no need for network requests at this time. The second step is between the browser and the app,

If the cache is added to this place, the cache is transparent to the app, and the cache stores the complete page. The third node,

There are several layers in the app, so the cache can be placed on different layers. This part is a complex part of the situation or scenario.

Exercise caution when selecting the cache. In the fourth step, the database can also have a cache, for example, querycache of MySQL.

That is to say, we can add cache at any point in the request process. But can all the data be stored in the cache.

Of course not. The data to be cached always has some characteristics. It is necessary to clearly determine whether the data can be cached,

How data can be cached must begin with the changing features of data.

What are the changing features of data? The simplest two types are change and unchanged. We all know that data that will not change does not need to be computed every time.

The problem is that all data will change theoretically, and changes are the eternal theme of the world. That is to say, it is wrong to divide data into two types: change and change,

Then let's add another condition: time. Then we can sum up the data features as changing or changing within a period of time. Then, based on this data feature,

We can cache the data in a suitable location and a suitable cache type.

3. What are the cache attributes?
From the object-oriented perspective, the cache is an object, so it is an object and there must be attributes. Next we will discuss the attributes of the cache.

The following lists the three common attributes.
(1) hit rate
The hit rate is the ratio of the number of requests cached to the number of times the cache returns the correct results. The higher the proportion, the higher the cache usage.

Hit rate is a very important issue in cache. We all hope that our cache hit rate can reach 100%, but it is often counterproductive,

Besides, the cache hit rate is an important indicator to measure the cache validity.

(2) maximum element
The maximum number of elements that can be stored in the cache. Once the number of elements in the cache exceeds this value, the cache clearing policy will be used,

Setting the maximum element value based on different scenarios may increase the cache hit rate to a certain extent and cache more effectively.

(3) clearing Policy

1 FIFO, first in first out, the data first enters the cache. When the cache space is insufficient (when the maximum element limit is exceeded), the data is first cleared.
2 LFU, less frequently used, the elements that have been least used will be cleared.

This requires that the cached element has an hit attribute. When the cache space is insufficient, the minimum hit value will be cleared.
2 LRU, least recently used, least recently used. The cached element has a timestamp. When the cache capacity is full,

When new elements need to be cached, the elements whose time stamp is the farthest from the current time in the existing cached elements will be cleared out of the cache.

4. cache Media
There will be no more than two types of hardware media, memory and hard disk (corresponding to the Application Layer Program, do not consider the register and other issues ).

However, we often do not divide data from hardware. The general division method is technically divided into several types, including memory, hard disk files, and databases.
(1) memory. Storing the cache in the memory is the fastest choice. Direct Memory Operation by any program is much faster than hard disk operation,

However, if your data needs to take into account the break down problem, because the data stored in the memory is called no persistent data.

If there is no backup on the hard disk, it is difficult or cannot be recovered after the machine is down.

(2) hard disks. In general, many cache frameworks use both memory and hard disks. For example, when the allocated memory is full,

This will allow the user to choose to persistently store the data that needs to be withdrawn from the memory space to the hard disk. Of course, the user also chooses to directly store the data to the hard disk.

(One copy in the memory, one copy in the hard disk, not afraid to go down). Other caches also directly place data on the hard disk.

(3) when talking about databases, some people may think that we didn't talk about reducing the number of database queries,

Reduce the pressure on database computing. How can we use the database as the cache medium now? This is because there are many types of databases,

For example, berkleydb, which does not support SQL statements, does not have an SQL engine, but only stores the key and value structures, so the speed is very fast,

In contemporary PCs, there are no problems with queries over a dozen times per second (of course, this is determined based on business characteristics,

If the data you access is evenly distributed, ahuaxuan cannot guarantee this speed ).

In addition to the cache medium, ahuaxuan divides the cache into local cache and remote cache based on the coupling between the cache and the application.
Local cache refers to the cache component included in the application, while remote cache refers to the cache component decoupled from the application outside the application.

Typical local caches include ehcache and Oscache, while remote cache has a well-known memcached.

The biggest advantage of localcache is that the application and cache are in the same process, and the request cache is very fast,

No network overhead, etc. Therefore, a single application,

It is appropriate to use the local cache when the cache node does not need to notify each other in the case of no cluster or cluster.

This is why ehcache and Oscache are so popular in Java.
However, local cache has some disadvantages. Generally, such cache frameworks (such as ehcache or Oscache in Java) are both local cache.

That is to say, as applications follow, multiple applications cannot directly share the cache. In the case of Application Clusters, this problem becomes more obvious,

Of course, some cache components provide the function for cluster nodes to notify each other of cache updates. However, this function is broadcast or loop update,

When the cache is updated frequently, the network I/O overhead will be very high, and the normal operation of the application will be affected in severe cases.

In addition, if the cache contains a large amount of data, using localcache means that each application has such a large cache, which is definitely a waste of memory.

In this case, we often choose remote cache, such as memcached.

Each application can share data in memcached. These applications use the SOCKET protocol and the memcached protocol based on the TCP/IP protocol.

Directly connect to memcached. An app updates the value of memcached, and all applications can get the latest value.

Although there is a lot of network overhead at this time, this solution is usually more common than localcache broadcast or loop update cache nodes,

And the performance is higher than the latter. Because only one copy of data needs to be saved, the memory usage is also improved.

From the above analysis, we can see that both local cache and remote cache have their own places in the cache field,

Therefore, ahuaxuan recommends that you determine the cache to be used based on the cache characteristics and our business scenarios when selecting or using the cache so that the cache function can be fully used.

Ahuaxuan believes that the use of cache is a required skill for architects. A good architect can,

Business scenarios can accurately determine the type of cache used and how to use this type of cache.

There is no silver bullet in the cache world. Currently, there is no cache that can solve any business scenario or data type,

If this technology appears, the architect will be less valuable.

Oscache
  
Oscache is a widely used high-performance J2EE cache framework. Oscache can be used in common cache solutions for any Java application.
  
Oscache has the following features:
  
Cache any object. You can cache part of JSP pages or HTTP requests without restriction. Any Java object can be cached.
  
Has a comprehensive API-Oscache API to give you a comprehensive program to control all the Oscache features.
  
Permanent cache-the cache can be written to the hard disk at will, so expensive data can be created (expensive-to-create) to keep the cache, or even restart the application.
  
Supports parameter configuration for cluster-cluster cache data that can be individually configured without modifying the code.
  
Cache record expiration-you can control the expiration of cache objects to the maximum extent, including pluggable refresh policies (if default performance is not required ).
  
Official Website http://www.opensymphony.com/oscache/
  
  Java Caching System 
  
JSC (Java Caching System) is a distributed cache system and a server-based Java application.

. It accelerates dynamic Web applications by providing management of various dynamic cache data.
  
Like other cache systems, JCS is also an application for high-speed reading and low-speed writing.
  
Dynamic Content and report systems provide better performance.
  
If a website has a duplicate website structure and uses a database with intermittent updates (instead of continuously updating the database ),

If the same results are repeatedly searched, the performance and scalability can be improved by executing the cache.
  
Official Website http://jakarta.apache.org/turbine/jcs/
  
  Ehcache 
  
Ehcache is a pure Java cache in the process. It has the following features: fast, simple,

Acts as a pluggable cache for hibernate2.1, with minimal dependencies, comprehensive documentation and testing.
  
Official Website http://ehcache.sourceforge.net/
  
  Jcache 
  
Jcache is an open source program, is trying to become a JSR-107 open source specification, JSR-107 specification has not changed for many years.

This version is still built on the original function definition.
  
Official Website http://jcache.sourceforge.net/
  
  Shiftone 
  
Shiftone Java object cache is a Java lib that executes a series of strict object cache policies,

It is like a Lightweight Framework for configuring cache working states.
  
Official Website http://jocache.sourceforge.net/
  
  Swarmcache 
  
Swarmcache is a simple and effective distributed cache that uses IP multicast to communicate with other hosts in the same LAN,

It is specially designed to drive web applications for clusters and data. Swarmcache provides better performance support for typical read operations that greatly exceed write operations.
  
Swarmcache uses javagroups to manage subordination and distributed cache communication.
  
Official Website http://swarmcache.sourceforge.net
  
  Treecache/jbosscache 
  
Jbosscache is a replication transaction processing cache that allows you to cache enterprise-level application data to better improve performance.

Cache data is automatically copied, allowing you to easily work in clusters between JBoss servers.

Jbosscache can run an mbean service through the JBoss application service or other J2EE containers. Of course, jbosscache can also run independently.
  
Jbosscache consists of two modules: treecache and treecacheaop.
  
Treecache-a transaction processing cache for tree structure replication.
  
Treecacheaop-is an "Object-Oriented" cache, which uses AOP to dynamically manage pojo (plain old Java objects)
  
Note: AOP is a continuation of OOP. It is short for Aspect Oriented Programming, meaning Aspect-Oriented Programming.
  
Official Website http://www.jboss.org/products/jbosscache
  
  Whirlycache 
  
Whirlycache is a fast, configurable cache for objects that exist in the memory.

It can accelerate the speed of websites or applications by caching objects, otherwise it must be created by querying the database or other costly processing programs.


2. What do the design patterns know? What is Singleton mode? In Singleton mode, how does one read the content of the XXX. properties file?

The general mode has four basic elements: Pattern name, problem, solution, and effect ).

The creation mode is used to process the object creation process. It mainly includes the following five design modes:
Factory method pattern)
Abstract Factory Pattern)

Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Builder Pattern)
Prototype Pattern)
Singleton pattern)
 The structure mode is used to process the combination of classes or objects.It mainly includes the following seven design modes:
Adapter Pattern)

Converts an interface of a class to another interface that the customer wants. The adapter mode allows classes that cannot work together due to interface incompatibility.

Bridge pattern)

Separate the abstract part from its implementation part so that they can all change independently.

Composite pattern)

Separates the construction of a complex object from its representation so that different representations can be created during the same construction process.

Decorator pattern)
Facade Pattern)
Flyweight Pattern)
Proxy Pattern)
The behavior pattern is used to describe how classes or objects interact and how responsibilities are assigned., Mainly including the following 11 design modes:
Chain of responsibility pattern)

To cancel the coupling between the request sender and receiver, multiple objects have the opportunity to process the request. Link these objects into a chain,

And pass the request along this chain until an object processes it.

Command pattern)
Encapsulate a request as an object to parameterize the customer with different requests; queue requests or record request logs,
And supports cancelable operations.
Interpreter Pattern)
Iterator Pattern)
Mediator Pattern)
Memento Pattern)
Observer Pattern)
State Pattern)
Strategy Pattern)
Template Method Pattern)
Visitor pattern)

A brief description of the design pattern in Java Development

Design Pattern: A Design Pattern describes a proven feasible scheme. These solutions are very common and are the most commonly used mode with a complete definition. The general mode has four basic elements: Pattern name, problem, solution, and effect ).

Overview of 23 common modes:

1)Abstract Factory): Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

2)Adapter mode ):Converts an interface of a class to another interface that the customer wants. The adapter mode allows classes that cannot work together due to interface incompatibility.

3)Bridge ):Separate the abstract part from its implementation part so that they can all change independently.

4)Construction mode (builder ):Separates the construction of a complex object from its representation so that different representations can be created during the same construction process.

5)Chain of responsibility ):To cancel the coupling between the request sender and receiver, multiple objects have the opportunity to process the request. Connect these objects into a chain and pass the request along the chain until an object processes it.

6)Command mode ):Encapsulate a request as an object to parameterize the customer with different requests, queue requests or record request logs, and support cancelable operations.

7)Merging mode (composite ):Combine objects into a tree structure to represent the "part-whole" hierarchy. It makes the customer's use of a single object and composite object consistent.

8)Decorator ):Dynamically add some additional responsibilities to an object. As far as the extension function is concerned, it can generate subclass more flexibly.

9)Facade ):Provides a consistent interface for a group of interfaces in the subsystem. The facade mode defines a high-level interface, which makes the subsystem easier to use.

10)Factory method ):Defines an interface for creating objects, so that the subclass determines which class to instantiate. Factory method delays the instantiation of a class to its subclass.

11)Flyweight ):Use the sharing technology to effectively support a large number of fine-grained objects.

12)Interpreter mode (Interpreter ):Given a language, it defines a representation of its syntax, and defines an interpreter that uses it to represent sentences in the language of interpretation.

13)Iteration submode (iterator ):Provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

14)Mediator ):An intermediary object is used to encapsulate a series of object interactions. The intermediary does not require explicit internal representation of each object.

15)Memento ):Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the Saved state.

16)Observer mode (observer ):Defines a one-to-many dependency between objects so that when the status of an object changes, all objects dependent on it are notified and automatically refreshed.

17)Original Model mode (prototype ):Use a prototype instance to specify the object type and copy the prototype to create a new object.

18)Proxy mode ):Provide a proxy for other objects to control access to this object.

19)Singleton ):Ensure that a class has only one instance and provides a global access point to it.

20)State ):Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

21)Strategy ):Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode makes the algorithm changes independent of the customers who use it.

22)Template Method ):Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

23)Visitor mode (visitor ):Indicates an operation that acts on each element in an object structure. This mode defines new operations on these elements without changing the classes of each element.


3. How to Integrate SSH or s2sh?
4. What is a transaction? What are its features?
5. Are triggers and stored procedures familiar? Have you used them? How to optimize the database?
What is 6.10 and 8 equal?
7. How to convert a decimal number into a binary number?
8. How is Data Structure learned? How does one calculate the height of a binary tree?
9. Talk about your career plans in the next few years?
10. How do colleagues evaluate your performance in your work? What do you think?
11. What are your advantages and disadvantages?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.