{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
java,high-perf,gridgain,performance,tips and tricks

Efficient Techniques For Loading Data Into Memory

Data loading usually has to do with initializing cache data on start up. However, quite often caches need to be loaded or reloaded periodically, and not only on start up. In cases in which you need to load lots of data, either at start up or at any point afterward, using standard cache put(...) or putAll(...) operations is generally inefficient, especially when transactional boundaries are not important. This is especially true when data has to be partitioned across the network, so you don't know in advance on which node the data will end up.

For fast loading of large amounts of data, GridGain provides a cool mechanism called data loader (implemented via GridDataLoader). The data loader will properly batch keys together and collocate those batches with nodes on which the data will be cached. By controlling the size of the batch and the size of internal transactions it is possible to achieve very fast data loading rates.

The code below shows an example of how it can be done:

// Get the data loader reference.
try (GridDataLoader<Integer, String> ldr = grid.dataLoader("partitioned")) {
    // Load the entries.
    for (int i = 0; i < ENTRY_COUNT; i++)
        ldr.addData(i, Integer.toString(i));

Whenever the data is submitted to the data loader, it is stored in the buffer, which is consumed by loader threads. If the buffer is full, the user thread will block on the addData(...) call until the loader threads free enough room for new entries.

Another method of data preloading is to load it directly from a persistent data store. GridGain supports that via the GridCache.loadCache(...) method. Note that this method of loading data into cache is very efficient as it is local, non-transactional and is usually implemented using bulk data store operations. The reason it can afford to be non-transactional is because it will not override any values in cache, it can only insert new values. This means that if some transaction has already updated an entry, this entry will not be overwritten by the loadCache(...) call.

Whenever the GridCache.loadCache(...) method is called, it will internally delegate to the underlying persistent store implementation by invoking the GridCacheStore.loadAll(...) method. Usually implementation of this method will load from DB either full or partial set of objects depending on requirements.

Here is an example of how the GridCacheStore.loadAll(...) method may be implemented:

public void loadAll(@Nullable String cacheName, 
    GridInClosure2<UUID, Person> closure, Object... args) throws GridException {
    try (Connection conn = getConnection()) {
        // Load all Persons from database (perhaps to warm up cache?)
        try (PreparedStatement st = 
            conn.prepareStatement("select * from PERSONS")) {
            ResultSet rs = st.executeQuery();
            while (rs.next())
                    // Key.
                    // New value.
                    person(rs.getString(1), rs.getString(2),
                        rs.getString(3), rs.getString(4))
    catch (SQLException e) {
        throw new GridException("Failed to load objects", e);
Note that, instead of returning a collection of loaded entries, this method instead passes each load entry into the closure provided by the system, which avoids costly large collection creations and internal resizing. GridGain will then take the values passed into the closure and store them in cache.

Using the above loading routines will often render 10x and above performance improvement over simple put(...) calls.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks