MMKV – Fast and Encrypted React Native Storage

Photo of Mateusz Matyska

Mateusz Matyska

Updated Jan 5, 2023 • 3 min read
mmkv_storage_react_native

Developed by WeChat, MMKV is an efficient and reliable key/value storage framework for React Native.

Written in C++ using JSI, MMKV is easy to install and use, and it’s possible to encrypt your data with and without a custom key. Continue reading for more details.

What is MMKV storage?

MMKV storage is a library that provides a fast solution to store your data in a React Native application. The library has many pros. For example, it’s fast and efficient (0.0002s read/write speed), because everything is written in C++. Another advantage? The library is simple and lightweight (~ 50K Android/30K iOS), and even smaller when packaged. MMKV also allows you to store any kind of data type with or without encryption, and has support for redux-persist.

Installation

Installation is very easy, using the ‘yarn add’ command. From the Android side, there are extra steps around the native part of your project. You can find the necessary code for Android on react-native-mmkv-storage.

How to use MMKV storage

The first step when using MMKV storage in your application is creating a storage instance:

const MMKV = new MMKVStorage.Loader().initialize(); 

After initialization, you can save and read values from storage. The write and read process looks very similar to the popular AsyncStorage library.

To save data, use:

await MMKV.setStringAsync("key", value);

The read data method is similar to saving:

1const value = await MMKV.getStringAsync("string");

How to encrypt your data

The MMKV library allows you to encrypt your data in two ways: with and without a custom key.

To encrypt data without a custom key, use:

const MMKVwithEncryption = new MMKVStorage.Loader()
.withEncryption()
.initialize();

To encrypt with a custom key:

const MMKVwithEncryptionKey = new MMKVStorage.Loader()
.withEncryption()
.encryptWithCustomKey("encryptionKey")
.initialize();

Please remember to set a custom encryption key in secret const values, because it is sensitive data. For example, this react-native-dotenv library should help keep a custom key as a secret const value.

Debugging

As documentation says: “You cannot attach a chrome debugger if you are using >=0.5.0 version of this library, since debugging is not available when JSI modules are used. You can use Flipper to debug if necessary.”

How about performance?

The encryption write and read methods are storage costly, but with this library, don’t be afraid about impairing application performance. The library uses the JSI engine for all methods. Plus, using C++ for all calculations makes your storage element faster.

Summary

MMKV storage is a good solution to keep your data safe, but please remember this library gives you value even when you don't need encryption. Using JSI for data operations makes your application faster and increases performance.

Photo of Mateusz Matyska

More posts by this author

Mateusz Matyska

React Native Developer at Netguru
Codestories Newsletter  Check what has recently been hot in European Tech.   Subscribe now

We're Netguru!

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency
Let's talk business!

Trusted by: