I assume that you are creating a relatively typical web application that:
- has one server used to save
- multiple web servers
- connects authenticated users to a single server through sticky sessions through a load balancer
Now that we will answer your questions. Most persistences, a database, or NoSQL probably have some kind of caching built in such a way that if you repeat the same simple query (for example, extracting by primary key), it can cache the result. However, the more complex the request, the less likely persistence can perform caching on it. In addition, if there is only one server to save (i.e. no fragments or master / read slave entries), it quickly becomes a bottleneck. Thus, the caching of the application level that you want to do should usually occur on web servers to reduce the load on the database.
Regarding caching, heuristics are elements that are often available and / or expensive to generate (in terms of processing / memory of the database / web server). Typical candidates are the home page and any other landing page of the site - often the best approach for them is to generate a static file and maintain it. The following parts depend on your application, but as a rule, the most effective strategy is to cache as close as possible to the final result - often used HTML code. For your social network, this may be a list of recognized updates, or some.
Regarding user sessions, this is certainly a good candidate for caching. In this case, you are likely to get a lot of mileage from judicious use of the session area of ββthe web server (assuming the JSP server). This data is stored in memory and is a good place to store user information displayed after user authentication on each page (for example, name and surname).
Now, the last thing to consider is the invalidity of the cache and is indeed an integral part of all this ( Naming the material is another difficult thing in computer science ). In this case, using this type of memcached or ehcache that others have talked about is the right approach. ehcache can be easily started while working with your Java application and works well with expiring things, with policies that are used recently and least used, and allow you to use both memory and disk for caching. What you need to think about is situations where you need to expire something from the cache before this graph, because the data has changed. In this case, you need to work with these dependencies in the architecture of your application so that it reads / writes to the cache as necessary.
source share