DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Address Non-Functional Requirements: How To Improve Performance
  • Exploring Exciting New Features in Java 17 With Examples
  • Linked List in Data Structures and Algorithms
  • Rust’s Ownership and Borrowing Enforce Memory Safety

Trending

  • LLM Agents and Getting Started with Them
  • Deployment Lessons You Only Learn the Hard Way
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Best Practices for Evaluating LLMs and RAG Systems
  1. DZone
  2. Data Engineering
  3. Data
  4. Efficient Techniques For Loading Data Into Memory

Efficient Techniques For Loading Data Into Memory

By 
Dmitriy Setrakyan user avatar
Dmitriy Setrakyan
·
Sep. 04, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
9.3K Views

Join the DZone community and get the full member experience.

Join For Free

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:

@Override
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())
                c.apply(
                    // Key.
                    UUID.fromString(rs.getString(1)),
                    // 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.


Data (computing) Memory (storage engine)

Published at DZone with permission of Dmitriy Setrakyan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Address Non-Functional Requirements: How To Improve Performance
  • Exploring Exciting New Features in Java 17 With Examples
  • Linked List in Data Structures and Algorithms
  • Rust’s Ownership and Borrowing Enforce Memory Safety

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook