import org.junit.Assert; import org.junit.Test; import org.springframework.data.annotation.Id; import org.springframework.data.keyvalue.annotation.KeySpace; import org.springframework.data.keyvalue.core.KeyValueTemplate; import org.springframework.data.map.MapKeyValueAdapter; import java.util.LinkedList; import java.util.concurrent.CompletableFuture; import static org.hamcrest.CoreMatchers.is; public class KeyValueThreadSafety { @Test public void test() { KeyValueTemplate template = new KeyValueTemplate(new MapKeyValueAdapter()); LinkedList> futures = new LinkedList<>(); for (int index = 0; index < 10; index++) { TestEntity entity = new TestEntity(String.valueOf(index)); futures.add(CompletableFuture.runAsync(() -> template.insert(entity))); } futures.forEach(CompletableFuture::join); Assert.assertThat(template.count(TestEntity.class), is(10L)); } @KeySpace("test") public static class TestEntity { @Id final String value; public TestEntity(String value) { this.value = value; } } }