Cassandra - Is there a way to limit the number of asynchronous requests?

I would like to know if there is a way to limit the number of requests executed simultaneously by the java cassandra driver?

I am currently doing a lot of queries like this:

... 
PreparedStatement stmt = session.prepare("SELECT * FROM users WHERE id = ?");
BoundStatement boundStatement = new BoundStatement(stmt);
List<ResultSetFuture> futures = Lists.newArrayListWithExpectedSize(list.length);

for(String id : list ) {
     futures.add(session.executeAsync(boundStatement.bind(id)));
}

for (ListenableFuture<ResultSet> future : futures) {
ResultSet rs = future.get();
... // do some stuff
}

      

Unfortunately, this can lead to a NoHostAvailableException.

Thank.

+2


source to share


1 answer


You can use a semaphore to throttle the number of concurrent requests:

final Semaphore semaphore = new Semaphore(numberOfConcurrentQueries);
...
semaphore.acquire();
try {
    ResultSetFuture future = session.executeAsync("...");
    Futures.addCallback(future, new FutureCallback<ResultSet>() {
        @Override
        public void onSuccess(ResultSet result) {
            semaphore.release();
        }

        @Override
        public void onFailure(Throwable t) {
            semaphore.release();
        }
    });
} catch (Exception e) {
    semaphore.release();
}

      



But at the end of the day, it's not that strong: instead of getting NoHostAvailableException

when you exceed the capacity, the semaphore blocks (or throws if you're using a temporary version of get). Therefore, you probably want to apply back pressure to the component that initiates these requests.

You can also configure connection pools to configure capacity, see our docs (for version 2.1 use the dropdown at the top of the page if you're in 2.0).

+8


source







All Articles