Vertx Shared Data
In this tutorial, you'll learn about Fix Vert.x Shared Data Not Synchronizing. We cover key concepts, practical examples, and best practices.
The Problem
Shared data values written in one verticle are not visible in another verticle.
Quick Fix
Use SharedData API
Wrong:
// No shared data
static Map<String, String> cache = new ConcurrentHashMap<>();
Output:
Not distributed across cluster
Right:
SharedData sharedData = vertx.sharedData();
sharedData.getLocalMap("my-map").put("key", "value");
Output:
Local map shared across verticles
Use clustered maps
Wrong:
// Local map only
sharedData.getLocalMap("my-map");
Output:
Only in same JVM
Right:
sharedData.<String, String>getClusterWideMap("my-map")
.onSuccess(map -> map.put("key", "value")); // Clustered
Output:
Async clustered map
Configure clustering
Wrong:
// No cluster config
Vertx.clusteredVertx(new VertxOptions());
Output:
Cluster not formed
Right:
VertxOptions options = new VertxOptions();
ClusterManager mgr = new HazelcastClusterManager();
options.setClusterManager(mgr);
Vertx.clusteredVertx(options);
Output:
Cluster formed
Prevention
- Use sharedData.getLocalMap() for in-process sharing
- Use getClusterWideMap() for cross-node sharing
- Configure a cluster manager (Hazelcast, Infinispan, Zookeeper)
Common Mistakes with shared data
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world VERTX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
This quick fix is part of the DodaTech Spring & JVM ecosystem series. Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro