Intermittent MMKV Data Loss after Background/Kill (Encrypted) with InvalidProtocolBuffer truncatedMessage Decode Error
Hi Team,
We are seeing an intermittent but impactful issue where the encrypted MMKV instance sometimes comes back empty after the app is backgrounded or force-closed for a period, resulting in user logout and loss of cached session/context. Looking for guidance to diagnose and confirm whether this is a corruption, flushing, or decoding/encryption edge case.
Summary
- Normal operation: Reads/writes work reliably while the app is active.
- Problem: After backgrounding or the OS/user killing the app (and reopening minutes later), the MMKV store occasionally initializes empty.
- Frequency: Sporadic but frequent enough to affect production users.
- Error observed (Android & iOS logs):
- MiniPBCoder.cpp:591::decodeOneMap InvalidProtocolBuffer truncatedMessage
- [E] MiniPBCoder.cpp:591::decodeOneMap InvalidProtocolBuffer truncatedMessage
- Encryption is enabled.
- One stored value is a JSON string (~4.9 kB). All other keys are very small (tokens, flags, etc.).
Steps to (Intermittently) Reproduce
- Launch app; write several small keys plus one ~4.9 kB JSON string.
- Confirm keys exist via getAllKeys().
- Background app OR swipe-kill.
- Wait 10–30+ minutes (sometimes less).
- Relaunch.
- Observe: storage is empty; decode error logged natively.
Guten Tag, Hans here.
[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by
@mrousavyin his free time. To support@mrousavy, please consider 💖 sponsoring him on GitHub 💖. Sponsored issues will be prioritized.
@mrousavy facing the same issue. Only for IOS. The Data is suddenly getting wiped out while using encryption key with MMKV. Please look into it.
we disabled the encryption and the storage is still getting wiped
Which version are you using? I think it's time we need issue templates for react-native-mmkv.
Try V4 and let me know if you can reproduce it there
I am using the react-native-mmkv v3.3.1 let me try the beta version.
This problem also occurs here. I tested it on v4, and it happens too. I have around 500 users who are active daily with a good amount of screen time. I only see this issue about ten cases a day. So it's quite sporadic, there's no pattern, and it's not reproducible, but it does happen.
Apparently, if the app crashes or suddenly terminates, sometimes due to the phone's own system, because it's running in the background, etc., and by chance, at the same time, mmkv is in the middle of an operation, it ends up corrupting the file.
Or even during a read/write operation on a large amount of data that the phone doesn't have enough memory to handle, it ends up corrupting the file too. And because it doesn't have atomicity mechanisms or rowbacks like traditional databases, all the data is lost.
But only if it's encrypted, right?
Yes in our case it is only happening when we are using encrypted MMKV
we are facing the same issue but not in Encrypted does any one face this issue?
@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?
@nazmeln Actually, yes — I did have the same issue. It happened exactly as I described earlier. I ended up having to migrate to a custom implementation using blob files and integrate it with redux-persist.
@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?
Yes, we are using redux-persist.
@Ankitinara @ninad-joshi-asurion @PatrickWalter387 @pratikSeven just wondering, does everyone above having this issue use redux-persist?
Yes we are using it redux persist. Faced this for both encrypted and non encrypted storage. Facing the same issue for V4 version as well
Same here we are using redux-persist. We are facing the issue with non encrypted storage.
It has to do with encryption, so every time you try to use storage.getString(key) you need to recrypt first.
const instance = createMMKV({
id: mmkvId,
encryptionKey: key,
mode: 'single-process',
readOnly: false,
});
instance.recrypt(key);
Are you sure the key is exactly 16 bytes?
I'm not using redux persist or encryption but faced this issue too firsthand. My app crashed due to another issue, and when I restarted, previously stored user preferences were gone.
Experiencing the same issue here, with a very minimal default setup (new MMKV() / createMMKV()). Seems to be fine in v3.3.0, but it occurs on both higher v3.3.x and v4.