Language: Java
Data / Embedded Database / Caching
MapDB was created to offer a lightweight, fast, and thread-safe Java storage solution for applications needing persistent collections or in-memory caching. It combines features of a database and a cache, allowing developers to manage large datasets efficiently without relying on external database servers.
MapDB is an embedded Java database and caching library that provides fast, concurrent, and off-heap key-value storage. It supports maps, sets, queues, and other collections with optional persistence to disk or memory-mapped files.
<dependency>
<groupId>org.mapdb</groupId>
<artifactId>mapdb</artifactId>
<version>3.0.9</version>
</dependency>implementation 'org.mapdb:mapdb:3.0.9'MapDB provides concurrent collections (maps, sets, queues) with optional persistence. It supports transactions, snapshots, off-heap memory, and disk-backed storage. Developers can use it as an embedded database for small to medium-sized applications or as a high-performance cache for large datasets.
import org.mapdb.DB;
import org.mapdb.DBMaker;
import java.util.concurrent.ConcurrentMap;
DB db = DBMaker.fileDB("data.db").make();
ConcurrentMap<String, String> map = db.hashMap("myMap").createOrOpen();
map.put("key1", "value1");
System.out.println(map.get("key1"));
db.close();Creates a disk-backed persistent map, adds an entry, retrieves it, and closes the database.
ConcurrentMap<String, Integer> map = DBMaker.memoryDB().make().hashMap("numbers").createOrOpen();
map.put("one", 1);
System.out.println(map.get("one"));Creates a fast in-memory concurrent map for temporary storage.
DB db = DBMaker.fileDB("data.db").transactionEnable().make();
ConcurrentMap<String, String> map = db.hashMap("myMap").createOrOpen();
map.put("key2", "value2");
db.commit(); // persist changes
db.close();Enables transactional updates, allowing commit or rollback of changes.
DB db = DBMaker.memoryDirectDB().make();
ConcurrentMap<String, String> map = db.hashMap("offHeapMap").createOrOpen();Stores data off-heap to reduce garbage collection overhead for large datasets.
import org.mapdb.HTreeMap;
HTreeMap<String, String> map = db.hashMap("queueMap").createOrOpen();MapDB also supports queues and other collection types for concurrent applications.
Always close the DB instance to release file handles.
Use transactions for critical updates to prevent data corruption.
Use off-heap memory for large in-memory datasets to reduce GC overhead.
Regularly back up disk-based databases for safety.
Leverage snapshots for consistent read views in concurrent environments.