Lazy loading

from Wikipedia, the free encyclopedia

Lazy loading , literally “lazy loading” (more aptly: “idle loading”), describes a design pattern in software development in which data objects generally provide values ​​or other dependent objects, but only fetch them from the data source when a specific request is made.

The opposite is called "eager loading"; all foreseeable required data is retrieved immediately and as efficiently as possible.

Reasons for application

The reason for the application may be that it is time-consuming or resource-consuming to fetch the content, for example in the case of a database query , a web service request or complex processing, and it is not yet clear at the beginning which data is actually required. With strong networking of data structures, otherwise U. extensive cascades triggered more and more charging processes.

Implementation options

Lazy initialization
mostly implemented with properties in the program code; From the outside, these are values ​​or objects, the initializing program code only being executed when they are queried or changed. The retrieval of an expected content of a property then leads to the execution of this initialization code which actually obtains this content, e.g. B. from a database.
Virtual proxy
an externally identical object with the same interface takes the place of the real object; the first time it is used, it obtains the necessary data and makes it available, or it is replaced by a fully-fledged object.
Ghost
Special form of a proxy that contains individual, known properties of the data object to be fetched from the start (e.g. ID number), so that it can be used to a limited extent without having to fetch the real data.
Value holder
an auxiliary object that manages the delayed data acquisition and outputs data if necessary, so that it can e.g. B. stand behind the value properties of a data object and can provide the values ​​for this in the background.

Risks

Lazy loading is often an attempt to minimize the number of data accesses, but mostly at the expense of efficiency in the case of multiple accesses. If a large number of values ​​or sub-objects are required and fetched via lazy load, for example by going through the object tree in the program and initializing the values ​​and objects in this way, there are a large number of individual queries. Many data sources, such as databases, are designed to process a lot of data with as few queries as possible and to return complete results. If many data records are read out with a large number of individual queries instead of a few descriptive queries, this is often a considerable burden on the data sources and network structure. In the database context, the SELECT N + 1 problem is therefore also used: with one (1) query, N objects are initially loaded; then, for each of these objects, that is N times, individual queries for dependent values ​​and objects are called.

See also

Individual evidence

  1. ^ Martin Fowler, Patterns of Enterprise Application Architecture , Addison-Wesley, 2003, pp. 200-214, ISBN 0-321-12742-0 .