Client/Server Optimization
4D provides optimizations for ORDA requests that use entity selections or load entities in client/server architectures. These optimizations speed up the execution of your 4D application by reducing drastically the volume of information transmitted over the network. They include:
- the optimization context
- the ORDA cache
- the binary encoding of ORDA requests between 4D remote applications and 4D Server
Supported architectures
ORDA client/server architectures that support the optimization are:
- Server datastores accessed by 4D remote desktop applications through
ds, - Remote datastores, accessed via
Open datastore(client REST requests).
Optimization context
The optimization context is based upon the following implementations:
-
When a client requests an entity selection from the server, 4D automatically "learns" which attributes of the entity selection are actually used on the client side during the code execution, and builds a corresponding "optimization context". This context is attached to the entity selection and stores the used attributes. It will be dynamically updated if other attributes are used afterwards. The following methods and functions trigger the learning phase:
-
Subsequent requests sent to the server on the same entity selection automatically reuse the optimization context and only get necessary attributes from the server, which accelerates the processing. For example, in an entity selection-based list box, the learning phase takes place during the display of the first row. the display of the next rows is optimized. The following functions automatically associate the optimization context of the source entity selection to the returned entity selection:
-
An existing optimization context can be passed as a property to another entity selection of the same dataclass, thus bypassing the learning phase and accelerating the application (see Reusing the context property below).
-
You can build optimization contexts manually using the
dataStore.setRemoteContextInfo()function (see Preconfiguring contexts).

Contexts handled in connections established through Open datastore can only be used between similar main versions of 4D. For example, a 4D 20.x remote application can only use contexts of a 4D Server 20.x datastore.
Example
Given the following code:
$sel:=$ds.Employee.query("firstname = ab@")
For each($e;$sel)
$s:=$e.firstname+" "+$e.lastname+" works for "+$e.employer.name // $e.employer refers to Company table
End for each
Thanks to the optimization, this request will only get data from used attributes (firstname, lastname, employer, employer.name) in $sel from the second iteration of the loop.
Reusing the context property
You can increase the benefits of the optimization by using the context property. This property references an optimization context "learned" for an entity selection. It can be passed as parameter to ORDA functions that return new entity selections, so that entity selections directly request used attributes to the server and bypass the learning phase.
You can also create contexts using the
.setRemoteContextInfo()function.
The same optimization context property can be passed to unlimited number of entity selections on the same dataclass. All ORDA functions that handle entity selections support the context property (for example dataClass.query() or dataClass.all()). Keep in mind, however, that a context is automatically updated when new attributes are used in other parts of the code. Reusing the same context in different codes could result in overloading the context and then, reduce its efficiency.
A similar mechanism is implemented for entities that are loaded, so that only used attributes are requested (see the
dataClass.get()function).
Example with dataClass.query():
var $sel1; $sel2; $sel3; $sel4; $querysettings; $querysettings2 : Object
var $data : Collection
$querysettings:=New object("context";"shortList")
$querysettings2:=New object("context";"longList")
$sel1:=ds.Employee.query("lastname = S@";$querysettings)
$data:=extractData($sel1) // In extractData method an optimization is triggered
// and associated to context "shortList"
$sel2:=ds.Employee.query("lastname = Sm@";$querysettings)
$data:=extractData($sel2) // In extractData method the optimization associated
// to context "shortList" is applied
$sel3:=ds.Employee.query("lastname = Smith";$querysettings2)
$data:=extractDetailedData($sel3) // In extractDetailedData method an optimization
// is triggered and associated to context "longList"
$sel4:=ds.Employee.query("lastname = Brown";$querysettings2)
$data:=extractDetailedData($sel4) // In extractDetailedData method the optimization
// associated to context "longList" is applied
Entity selection-based list box
Entity selection optimization is automatically applied to entity selection-based list boxes in 4D client/server desktop applications, when displaying and scrolling a list box content: only the attributes displayed in the list box are requested from the server.
A specific "page mode" context is also provided when loading the current entity through the Current item property expression of the list box (see Collection or entity selection type list boxes). This feature allows you to not overload the list box initial context in this case, especially if the "page" requests additional attributes. Note that only the use of Current item expression will create/use the page context (access through entitySelection\[index] will alter the entity selection context).
Subsequent requests to server sent by entity browsing functions will also support this optimization. The following functions automatically associate the optimization context of the source entity to the returned entity:
For example, the following code loads the selected entity and allows browsing in the entity selection. Entities are loaded in a separate context and the list box initial context is left untouched:
$myEntity:=Form.currentElement //current item expression
//... do something
$myEntity:=$myEntity.next() //loads the next entity using the same context
Preconfiguring contexts
An optimization context should be defined for every feature or algorithm of your application, in order to have the best performances. For example, a context can be used for queries on customers, another context for queries on products, etc.
If you want to deliver final applications with the highest level of optimization, you can preconfigure your contexts and thus save learning phases by following these steps:
- Design your algorithms.
- Run your application and let the automatic learning mechanism fill the optimization contexts.
- Call the
dataStore.getRemoteContextInfo()ordataStore.getAllRemoteContexts()function to collect contexts. You can use theentitySelection.getRemoteContextAttributes()andentity.getRemoteContextAttributes()functions to analyse how your algorithms use attributes. - In the final step, call the
dataStore.setRemoteContextInfo()function to build contexts at application startup and use them in your algorithms.
ORDA cache
For optimization reasons, data requested from the server via ORDA is loaded in the ORDA remote cache (which is different from the 4D cache). The ORDA cache is organized by dataclass, and expires after 30 seconds.
The data contained in the cache is considered as expired when the timeout is reached. Any access to expired data will send a request to the server. Expired data remains in the cache until space is needed.
You can force entity selection data in the ORDA cache to expire at any moment by using the refresh() function.
By default, the ORDA cache is transparently handled by 4D. However, you can control its contents using the following ORDA class functions:
Using the local keyword
By default, ORDA data model functions are executed on the server, which usually provides the best performance since only the function request and the result are sent over the network. However, it could happen that a function processes data that's already in the local cache and is fully executable on the client side. In this case, you can save requests to the server and thus, enhance the application performance by using the local keyword in the function definition.
Note that the function will work even if it eventually requires to access the server (for example if the ORDA cache is expired). However, it is highly recommended to make sure that the local function does not access data on the server, otherwise the local execution could not bring any performance benefit. A local function that generates many requests to the server is less efficient than a function executed on the server that would only return the resulting values. For example, consider the following function on the Schools entity class:
// Get the youngest students
// Inappropriate use of local keyword
local Function getYoungest() : Object
return This.students.query("birthDate >= :1"; !2000-01-01!).orderBy("birthDate desc").slice(0; 5)
- without the
localkeyword, the result is given using a single request - with the
localkeyword, 4 requests are necessary: one to get the Schools entity students, one for thequery(), one for theorderBy(), and one for theslice(). In this example, using thelocalkeyword is inappropriate.
Example: Checking attributes
We want to check the consistency of the attributes of an entity loaded on the client and updated by the user before requesting the server to save them.
On the StudentsEntity class, the local checkData() function checks the Student's age:
Class extends Entity
local Function checkData() -> $status : Object
$status:=New object("success"; True)
Case of
: (This.age()=Null)
$status.success:=False
$status.statusText:="The birthdate is missing"
:((This.age() <15) | (This.age()>30) )
$status.success:=False
$status.statusText:="The student must be between 15 and 30 - This one is "+String(This.age())
End case
Calling code:
var $status : Object
//Form.student is loaded with all its attributes and updated on a Form
$status:=Form.student.checkData()
If ($status.success)
$status:=Form.student.save() // call the server
End if
Binary encoding of ORDA requests
In client/server mode, the ORDA requests that a 4D remote application sends to 4D Server through ds, and the responses of 4D Server, travel in a binary encoding: CBOR (Concise Binary Object Representation, RFC 8949). Binary messages are smaller and faster to process than JSON text.
The binary encoding applies to all ORDA exchanges, in both directions: queries, entity and entity selection loads (including entity selection-based list boxes), saves, drops, locks and unlocks, reloads, ORDA class function calls with their parameters and results, and ORDA cache refreshes. Messages are not compressed.
The binary encoding is transparent for your code:
- Existing code runs unchanged, and every value it receives (attribute values, function parameters and results, status objects, errors returned by
Last errors) is identical to what it receives with the JSON encoding. - The optimization context, the ORDA cache and
localfunctions work as before. The binary encoding reduces the size of each message, not the number of requests. - The ORDA request logs keep the same readable content.
The binary encoding only applies to the main datastore accessed through ds by 4D remote applications. Remote datastores opened with Open datastore and REST requests still use JSON.
Encoding of a connection
The encoding is chosen once, when a 4D remote application opens its connection to 4D Server, and is kept for the whole connection:
- If 4D Server or the 4D remote application is of a release earlier than 4D 21 R6, the connection uses the JSON encoding. The connection opens normally.
- Otherwise, the connection uses the encoding currently set on 4D Server: the binary encoding by default, or the JSON encoding if you switched back to JSON (see below).
Several 4D remote applications connected to the same 4D Server can use different encodings, each one keeping its own.
On a 4D remote application, the useORDABinaryEncoding property returned by Application info tells which encoding the connection uses.
If a binary message cannot be decoded, the ORDA request fails with an error, and an error is written to the diagnostic log.
Switching back to the JSON encoding
You can make ORDA requests use the JSON encoding, for example for compatibility or diagnostic purposes:
- With the Use JSON for ORDA requests option on the "Client-server/Network options" page of the Settings. The option is read when 4D Server starts.
- With the
ORDA JSON encodingselector ofSET DATABASE PARAMETER, called on 4D Server (1 = JSON, 0 = binary). The new value applies to the connections opened afterwards, until 4D Server stops. It is not saved in the settings. On a 4D remote application or a single-user 4D, the selector is ignored.
In both cases, connections already open keep their encoding.
Example 1
On a 4D remote application, you check the encoding of the connection:
var $info : Object:=Application info
var $binary : Boolean:=$info.useORDABinaryEncoding
// $binary is True with a 4D Server of the same release and the default settings
Example 2
You want the 4D remote applications that connect from now on to use the JSON encoding. You call this code on 4D Server, for example in the On Server Startup database method:
SET DATABASE PARAMETER(ORDA JSON encoding; 1)
// existing connections keep their encoding, new connections use JSON