Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/23/2020

Reading time:1 min

DataStax-Examples/concurrent-requests-java

by DataStax-Examples

This example shows three different ways of handling concurrent request execution with Apache Cassandra™ using theJava DataStax Driver.Contributors: Olivier Michallat, Hsu-Kwang Hwang, Tomasz Lelek - derived fromhereObjectivesHow to limit concurrent query executions with Cassandra in blocking and non-blocking ways.How to throttle Concurrent Requests.Project LayoutThe following are the main Java files used by the application.LimitConcurrencyCustom - This example shows executing concurrent queries in blocking ways.The basic java concurrent mechanism such as SEMAPHORE, and Latch are used to control the flow.LimitConcurrencyCustomAsync - This example shows executing concurrent queries innon-blocking ways. Java CompletableFuture is used to achieve non-blocking.LimitConcurrencyRequestThrottler - Throttling allows you to limit how manyrequests a session can execute concurrently. This is useful if you have multiple applications connecting to the same Cassandra cluster, and want to enforce somekind of SLA to ensure fair resource allocation.Setup and RunningPrerequisitesMaven 3 installed on the machine running the examplesJDK 14An Apache Cassandra(R) cluster is running and accessible through the contacts points and data center identified in application.confRunningBuildingAt the project root levelmvn clean packageThis builds the JAR file located at target/java-concurrent-execution-1.0.jarConfiguration changesThe main configuration file is src/main/resources/application.confChange basic.contact-points = ["127.0.0.1:9042"] to the IP and cql port of your Cassandra cluster.Change basic.local-datacenter = dc1 to the datacenter of the Cassandra cluster you want to connect to.ThrottlingFor throttling the configurations are in the following blockadvanced.throttler { class = ConcurrencyLimitingRequestThrottler max-concurrent-requests = 32 max-queue-size = 10000}You are encouraged to experiment the configuration. For more details about throttling please check thefollowing linkRun the programjava -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustomjava -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustomAsyncjava -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyRequestThrottler

Illustration Image

This example shows three different ways of handling concurrent request execution with Apache Cassandra™ using the Java DataStax Driver.

Contributors: Olivier Michallat, Hsu-Kwang Hwang, Tomasz Lelek - derived from here

Objectives

  • How to limit concurrent query executions with Cassandra in blocking and non-blocking ways.
  • How to throttle Concurrent Requests.

Project Layout

The following are the main Java files used by the application.

  • LimitConcurrencyCustom - This example shows executing concurrent queries in blocking ways. The basic java concurrent mechanism such as SEMAPHORE, and Latch are used to control the flow.
  • LimitConcurrencyCustomAsync - This example shows executing concurrent queries in non-blocking ways. Java CompletableFuture is used to achieve non-blocking.
  • LimitConcurrencyRequestThrottler - Throttling allows you to limit how many requests a session can execute concurrently. This is useful if you have multiple applications connecting to the same Cassandra cluster, and want to enforce some kind of SLA to ensure fair resource allocation.

Setup and Running

Prerequisites

  • Maven 3 installed on the machine running the examples
  • JDK 14
  • An Apache Cassandra(R) cluster is running and accessible through the contacts points and data center identified in application.conf

Running

Building

At the project root level

mvn clean package

This builds the JAR file located at target/java-concurrent-execution-1.0.jar

Configuration changes

The main configuration file is src/main/resources/application.conf Change basic.contact-points = ["127.0.0.1:9042"] to the IP and cql port of your Cassandra cluster. Change basic.local-datacenter = dc1 to the datacenter of the Cassandra cluster you want to connect to.

Throttling

For throttling the configurations are in the following block

advanced.throttler {
    class = ConcurrencyLimitingRequestThrottler
    max-concurrent-requests = 32
    max-queue-size = 10000
}

You are encouraged to experiment the configuration. For more details about throttling please check the following link

Run the program

  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustom
  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyCustomAsync
  • java -cp target/java-concurrent-execution-1.0.jar com.datastax.examples.LimitConcurrencyRequestThrottler

Related Articles

spring
rest
api

GitHub - DataStax-Examples/spring-data-starter: ⚡️ A sample Spring Data Cassandra REST API

John Doe

2/14/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

examples