Create Docker Container For Cassandra Database

Wiraizkandar
2 min readJan 18, 2024

--

We will create a Cassandra docker container for our access token table. Cassandra has high scalability which is suitable for our use case to has high traffic for access token validation.

We are going to create Cassandra database with credential

  • User = root
  • Password = password
  1. Create docker-compose yml file
version: '3'
services:
cassandra:
image: cassandra:latest
environment:
- CASSANDRA_USER=root
- CASSANDRA_PASSWORD=password
ports:
- "9042:9042"
volumes:
- cassandra_data:/var/lib/cassandra

volumes:
cassandra_data:

2. Run docker compose

docker-compose up -d

to check is your container has been created you may run

docker ps

You should be able to see a container with cassandra:latest image

3. Connect your DB client with Cassandra database and create keyspace and table for our access tokens table.

In here, i’m using DataGrip application from Jetbrain as my Cassandra client . You may connect to your Cassandra database by using host “127.0.0.1” at port “9042” same as in our yml file.

We will create keyspace “oauth” (as in mysql it similar to database name)

CREATE KEYSPACE oauth 
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

- `SimpleStrategy` is a replication strategy that is suitable for a single-datacenter deployment. It places the first replica on a node determined by the partitioner, and additional replicas on the next nodes clockwise in the ring.

- `replication_factor` is an option that specifies the number of replicas of data on multiple nodes. A `replication_factor` of 1 means that there is only one copy of each row on one node.

Create access_tokens table

CREATE TABLE oauth.access_tokens (
user_id varchar,
access_token text,
expiry timestamp,
is_revoke boolean,
PRIMARY KEY (user_id, access_token)
);

Try insert data

INSERT INTO oauth.access_tokens (user_id, access_token, expiry, is_revoke)
VALUES ('100001', '123456', '2024-01-19T12:00:00Z', false);
select * from oauth.access_tokens;

You should able to see the inserted record

You are good to go! :)

--

--