Akshaya Kumar T
Akshaya Kumar T

Reputation: 144

Good way to a multi get of keys in couchbase with bucket of memcached type

I have a couchbase bucket of memcached type. I want to bulk get keys from the bucket. N1QL will not work as it is of memcached type. What is a good way to do this? I want to avoid multiple calls since per request, I have to fetch for around 100 keys at a time and don't want to make so many connections at scale.

Upvotes: 1

Views: 225

Answers (2)

dnault
dnault

Reputation: 8899

As Graham Pople commented, if you know the document IDs, you can efficiently fetch many documents at once using the Couchbase Java SDK's Reactive API.

The documentation: Batching using the Couchbase Java SDK's Reactive API

Here's a slightly modified version of the most general code example from that documentation:

List<String> docsToFetch = List.of("doc1", "doc2", "doc3");
Map<String, Throwable> failedResults = new ConcurrentHashMap<>();

List<GetResult> successfulResults = Flux.fromIterable(docsToFetch)
    .flatMap(key -> collection.reactive().get(key)
      .onErrorResume(e -> {
        failedResults.put(key, e);
        return Mono.empty();
      })
    )
    .collectList()
    .block();
}

Upvotes: 1

Perry Krug
Perry Krug

Reputation: 675

If you can, it would be best to migrate to an "Ephemeral" bucket which still runs in memory but can be replicated, rebalanced, indexed, etc.

Upvotes: 2

Related Questions