Why MMKV Is the Fastest Mobile Key‑Value Store for iOS and Android
MMKV is a high‑performance, mmap‑based key‑value library open‑sourced by Tencent, offering cross‑platform support for iOS and Android with features like multi‑process sharing, anonymous memory, AES encryption, and superior read/write speeds compared to SharedPreferences, NSUserDefaults, and SQLite, as demonstrated by benchmark tests.
MMKV is a mobile‑focused, mmap‑based key‑value component whose serialization uses protobuf, delivering high performance and strong stability. It has been used internally at Tencent for several years on iOS and was recently ported to Android, then open‑sourced.
MMKV Origin
In the WeChat client, occasional special characters caused crashes. The solution involved adding counters before and after critical code to detect abnormal text, requiring a high‑performance, persistent key‑value store. Existing options such as SharedPreferences, NSUserDefaults, and SQLite did not meet the strict performance needs, so mmap was chosen to implement a custom component.
MMKV Principle
Memory Preparation MMKV uses mmap to map a file into memory, providing a writable memory region that the OS flushes to disk, ensuring data is not lost on crash.
Data Organization Data is serialized with protobuf, which offers good speed and compact size.
Write Optimization For frequent updates, MMKV appends serialized incremental kv objects to the end of the memory region.
Space Growth Appending can cause uncontrolled file growth, so MMKV balances performance with space consumption.
More detailed design principles are described in the original article "MMKV – a high‑performance, mmap‑based key‑value component for iOS".
MMKV for Android Specific Features
During the Android migration, additional platform‑specific features were added.
Multi‑process Access Because Android's SharedPreferences has poor multi‑process support, MMKV leverages mmap's inherent shared‑memory capability to provide what may be the industry’s most efficient multi‑process data‑sharing component.
Anonymous Memory For sensitive data that should not be persisted to disk, MMKV uses Android’s Ashmem anonymous shared memory, which disappears when the process exits.
Data Encryption MMKV encrypts data with AES‑CFB‑128, a stream‑oriented mode better suited to its append‑only design than the more common CBC mode. This encryption feature is also available on iOS.
MMKV Usage
iOS usage is described elsewhere; the following shows Android usage.
Android Quick Start
MMKV is hosted on JCenter. Add the dependency in build.gradle:
dependencies {
implementation 'com.tencent:mmkv:1.0.10'
}MMKV works immediately without calling sync or apply. Initialize it in MainActivity (or any Application class):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String rootDir = MMKV.initialize(this);
Log.d("mmkv", "root: " + rootDir);
// ...
}Obtain a global instance and use it:
import com.tencent.mmkv.MMKV;
// ...
MMKV kv = MMKV.defaultMMKV();
kv.encode("bool", true);
boolean bValue = kv.decodeBool("bool");
kv.encode("int", Integer.MIN_VALUE);
int iValue = kv.decodeInt("int");
kv.encode("string", "Hello from mmkv");
String str = kv.decodeString("string");Separate instances can be created for different business domains:
MMKV mmkv = MMKV.mmkvWithID("MyID");
mmkv.encode("bool", true);
// ...SharedPreferences Migration
MMKV provides importFromSharedPreferences() to migrate data easily.
It also implements the SharedPreferences and SharedPreferences.Editor interfaces, allowing migration with only a few lines of code while keeping existing CRUD calls unchanged.
More usage details are available in the GitHub wiki.
MMKV Performance
iOS Performance Comparison
MMKV was compared with NSUserDefaults by performing 10,000 read/write cycles. The test code resides in iOS/MMKVDemo/MMKVDemo/. Results are shown below.
MMKV’s write performance far exceeds NSUserDefaults, and its read performance is comparable or better.
Android Performance Comparison
MMKV was compared with SharedPreferences and SQLite using 1,000 read/write cycles. Test code is in Android/MMKV/mmkvdemo/. Results are shown below.
Single‑process Performance MMKV’s write speed far surpasses SharedPreferences and SQLite, with read speed also competitive or superior.
Multi‑process Performance MMKV dramatically outperforms MultiProcessSharedPreferences, SQLite, and other solutions in both write and read operations, making it the clear choice for Android multi‑process key‑value storage.
Click the original article to access the GitHub source code.
WeChat Client Technology Team
Official account of the WeChat mobile client development team, sharing development experience, cutting‑edge tech, and little‑known stories across Android, iOS, macOS, Windows Phone, and Windows.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
