Ignite持久化写入配置疑问:Write Behind是否无需开启Write Through?
setWriteThrough(true) and setWriteBehindEnabled(true)? Great question! Let’s break down exactly how these two Ignite configuration settings work together, so you know exactly what’s needed to enable Write Behind mode.
First, let’s define each setting’s purpose clearly:
setWriteThrough(true): This enables the base Write Through functionality, which means Ignite will synchronously write cache changes to your underlying persistent storage (like a database) right after each cache operation finishes. Without this enabled, Ignite won’t write to the persistent store at all by default.setWriteBehindEnabled(true): This is an extension of Write Through that switches it to an asynchronous, batch-based mode (the Write Behind behavior you want). Instead of writing every single change to storage immediately, Ignite queues up operations and flushes them in batches based on your configured rules (like batch size or time interval).
The Critical Dependency: Write Behind Needs Write Through
You cannot enable Write Behind by only setting setWriteBehindEnabled(true)—it relies entirely on Write Through being turned on first. The example you saw includes both settings because they’re both required:
setWriteThrough(true)tells Ignite it needs to interact with the persistent store in the first place.setWriteBehindEnabled(true)modifies that core behavior to use asynchronous batch writes instead of synchronous ones.
If you only set setWriteBehindEnabled(true) without enabling Write Through, Ignite will ignore the Write Behind setting entirely—your cache changes won’t be written to persistent storage at all.
Correct Configuration Example
Here’s how to properly set up Write Behind mode, including optional tuning parameters to fit your use case:
CacheConfiguration<Integer, MyEntity> cacheCfg = new CacheConfiguration<>("myPersistentCache"); // Required: Enable base Write Through functionality cacheCfg.setWriteThrough(true); // Required: Switch to asynchronous Write Behind mode cacheCfg.setWriteBehindEnabled(true); // Optional: Tune Write Behind behavior for performance cacheCfg.setWriteBehindBatchSize(200); // Flush when queue hits 200 operations cacheCfg.setWriteBehindFlushFrequency(3000); // Flush every 3 seconds (whichever comes first) cacheCfg.setWriteBehindThreadCount(2); // Use 2 threads to handle batch flushes
Quick Recap
- Only
setWriteThrough(true): Synchronous, immediate writes to persistent storage. - Both
setWriteThrough(true)+setWriteBehindEnabled(true): Asynchronous, batch-based writes (the Write Behind mode you’re targeting). - Only
setWriteBehindEnabled(true): No effect—persistent storage writes stay disabled.
内容的提问来源于stack exchange,提问作者Vishal Sharma

