Where to put the state of the application?

Where in the code is it best to create objects (stateful objects), and where not? In what layers?

For example, I once put a reference to an object inside the Hibernate DAB class, and I was told that this is not true, since DAO classes should not have state. The state must be inside the "service level".

I was told that I should not create new objects upon repeated calls to methods such as UpdateCart (). Creating objects is expensive and does not have to sit in code everywhere. It should only be in methods such as initialization. For example, if an e-commerce application requires a cart, put it in a session. If it needs some kind of common core object, put it in the initialization code. Create it once there, and let the rest of the application access its instance later. Do not create this instance with every call.

I am confused about this design principle. The strangest thing I've heard is, “the application should not have a state. The state should be stored in the data layer where the database is located. Really? I am completely new to these design concepts and I don’t know where to look to get more educated knowledge. GoF? Design of book templates? The goal is to create high-quality code (i.e. For use in business).

thanks

+6
source share
2 answers

What good practice may vary depending on the type of project.

For most projects, creating objects is not so expensive for the CPU. Cost, which is not always clearly stated, is the cost of design. It looks like your application has a design methodology in which the entire state and facilities must be managed in a controlled and centralized way. This is often done to improve maintainability and simplify the design. I would not assume that you should just know what design is if it was not clearly stated to you.

I suspect that the rest of the team is used to working in a certain way and does not think that they will have to document or teach you this methodology, just say when you got it “wrong.” This is not productive IMHO, but you have to deal with the situation you have and ask them questions when it comes to placing states or data structures.

+2
source

'The application must not have state. The state should be stored in the data layer where the database "

There are projects where this is the norm, precisely called "illiterate architectures." Regardless, each architecture should be stateless, of course, doubtful, and the term itself may also open up a discussion.

Most of the "cityless" applications actually have state, but since the rule is higher than state (no pun intended), this state is stored in one global place; Database. As Peter mentions, convenience and simplification may be the reason for this, but it has also often been heard that this is for scalability . Without the state appearing anywhere, but in the database, it was easy to add additional front-end servers, processing servers, and what you have.

Despite the fact that this does have some merit, I think we need to distinguish between a temporary state and an authoritative state.

The temporary condition may be something like the place where you are in the order process, and the data that you have already entered. In Java EE, you can save this in, for example, @ConversationScoped beans, or @Stateful beans. This means that you are holding inside the web layer accordingly. business level.

The benefits of this are the ease of use, performance, and offloading of a single central database. Of course, you can also store the temporary state in your central database, but you probably want to remove this from regular, non-temporary data, which means that you need some additional programming complexity. It is also generally much faster to retrieve data from the web tier, and it removes some of the load from the database.

In many systems, there is only one primary database (a database that accepts records), so this single database can become a huge bottleneck in this setup.

Depending on your actual architecture and configuration, without storing a temporary state in the database, you may actually improve your ability to scale.

The disadvantages are that you need your client to stick to a single server on which a temporary state is currently stored. This is typically called a “sticky session”. If one server that this client interacts with fails or needs to be restarted or something else, the client will lose this temporary data. There are some schemes, such as state replication to all nodes in a cluster or to neighboring nodes (buddy replication), but this complicates the situation and can overload the network (if all nodes are constantly replicated to each other).

An authoritative state means that it represents general data, which is the only source of information. This state is something that we almost always love in a central place, but sometimes you see that it is stored in, for example, a web node.

For example, suppose we have a list of the latest prices, and instead of storing it in a central place, we save it on the Internet node where it was entered. Now there is the concept of a “one and only” node website that has this information and other servers, may begin to assume that this “only” node website exists. Adding additional nodes is now impossible, since it violates this assumption.

+1
source

Source: https://habr.com/ru/post/926041/


All Articles