DEV Community

Carlos Azuaje
Carlos Azuaje

Posted on

I created a Monorepo Chat architecture solution

Realtime Chat using Apache Camel, Cassandra and Firestore

Source Code:

https://github.com/CharlyJazz/Chat-Firebase-Apache-Camel

If you one learn about Python, Microservices, Next JS, GCP, Kubernetes you are welcome to check it out the code

Chat system architecture leveraging Apache Camel's aggregation pattern. This architecture incorporates a mechanism to buffer incoming messages for a specific timeout duration, measured in seconds. Subsequently, the system aggregates these buffered messages based on their unique chat IDs. The purpose of this aggregation step is to optimize Firestore usage by reducing the number of write operations. The aggregated message data is then efficiently dispatched to Firestore, with the primary goal of minimizing Firestore billing costs.

The chat pagination utilize Cassandra instead of Firestore. This strategic decision serves a dual purpose:

  • It alleviates the load on Firestore's read operations
  • Harnesses the inherent advantages of Cassandra for optimizing the partitioning using the chat_id as the partition key.

Demo Video

The next content of this paper explain the sistem using a Architectural Map Features approach.

Problem

Create a chat that makes spam difficult, optimizes availability, and manages reading and writing operations in Firestore to reduce expenses.

The Features are:

  1. Create User
  2. Login
  3. Get the list of users
  4. User send messages to other user
  5. Create a new chat room between to users: The user A will find the user B and start a chat.
  6. User get realtime message updating

Mapping Features to Architectural Components

  • Feature Mapping 1: The users management and authentication (features 1, 2 and 3) will be handle by a microservice called Auth Microservice
    • The microservice will have a store.
    • The microservice will use JWT for the Authentication.
  • Feature Mapping 2. Chat creation and Message creation/retrieving (features 4 and 5) will be handle by a microservice called Chat Microservice
    • The microservice will have a store.
  • Feature Mapping 3. Chat Microservice will take care of sending the messages to a queue proccesing in other to satisfy the feature 6

Realtime solution to satisfy feature 6

Instead of using Firestore or another cloud-based solution for a complete chat solution, we are going to use Firestore only to update the UI with the aggregated news messages.

The aggregated news messages refers to a set of messages sent by a user. Instead of displaying each message one by one, there will be a server that will group all the related messages sent by a user into a list. This list will then be retrieved for the chat..

Architecture diagram

This is how it looks like in Firestore:

Firestore Image of the messages

Architectural Components to explicit implementation details

  • The auth and chat microservices will be written in Python using FastAPI.
  • The message aggregating process will be handled by Apache Camel using SpringBoot on Java.
  • User databases will be Postgres.
  • Chat database will be Cassandra to create a fast partition for each chat and fast pagination based on Time UUID.
  • The aggregated news messages will be saved into Firestore and only will retrieve for realtime
  • Chat messages queue processing will be handle by Kafka using a Python Kafka Consumer.
  • Auth validation in Python microservices will be handled by a Python shared library called auth-validator

Chat messages pagination and retrieving of the first page

The Chat will send a HTTP request to the Chat Microserver /chat/<id>?time=<time-uuid>&quantity to retrieve a set of messages

  • Query parameter quatity will be a integer to know the max size of messages to get
  • Query parameter time will be the UUID time of the last message that the chat have to get messages before that time
  • Both query parameter are optional. When there are not quantity you get 10. When there are not time you get the latest messages.

Development Enviroment Details

  • We going to use tilt in order to run the infraestructure on Kubernetes for development purposes.
  • There is a Docker Compose for development purposes.
  • For Python you need Pipenv and version 3.9.7
  • For Java you need:
java 17.0.2 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

Running the project using Docker Compose:

docker-compose rm -svf && docker-compose up
Enter fullscreen mode Exit fullscreen mode

It should create 9 containers:

1.) chat-firebase-apache-camel_zookeeper_1
2.) chat-firebase-apache-camel_cassandra_1
3.) chat-firebase-apache-camel_auth_db_1
4.) chat-firebase-apache-camel_kafka_1
5.) chat-firebase-apache-camel_consumer_1
6.) chat-firebase-apache-camel_auth_microservice_1
7.) chat-firebase-apache-camel_apache_camel_microservices_1
8.) chat-firebase-apache-camel_chat_microservices_1
9.) front_end_next_js_1
Enter fullscreen mode Exit fullscreen mode

Then you can open http://localhost:3000/ and start using the chat.

Run tests in Apache Camel

mvn test -X
Enter fullscreen mode Exit fullscreen mode

How to run the microservices manually?

  • Chat Microservice:
cd chat-microservice && pipenv run server
Enter fullscreen mode Exit fullscreen mode
  • Auth Microservice:
cd auth-microservice && pipenv run server
Enter fullscreen mode Exit fullscreen mode
  • You will need Maven to run the Apache Camel Spring Boot server
cd apache-camel-service-bus && mvn package && mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Docker Development Worflow

Main command to run docker-compose

docker-compose rm -svf && docker-compose up
Enter fullscreen mode Exit fullscreen mode

Migrate using Alembic in the Auth Microservice

In order to use the auth microservice you must migrate using alembic to create the user table. Access to the container and run:

pipenv shell
alembic upgrade head
Enter fullscreen mode Exit fullscreen mode

It should print something like:

dict_keys(['user'])
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> fe1a63894533, create user table
INFO  [alembic.runtime.migration] Running upgrade fe1a63894533 -> d3c6204b9f3c, username unique
Enter fullscreen mode Exit fullscreen mode

Run Kafka CLI:

Create a topic:

docker exec -it CONTAINER_ID kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 3 --topic charlytest
Enter fullscreen mode Exit fullscreen mode

List of topics:

docker exec -it CONTAINER_ID kafka-topics.sh --bootstrap-server localhost:9092 --list
Enter fullscreen mode Exit fullscreen mode

Get topics information

docker exec -it CONTAINER_ID kafka-topics.sh --bootstrap-server localhost:9092 --describe
Enter fullscreen mode Exit fullscreen mode

Console Producer using key and acks

In this case 12345 is the key and hello de message content (Useful to put all the messages in the same partition using the user_id)

docker exec -it CONTAINER_ID kafka-console-producer.sh  --bootstrap-server localhost:9092 --topic charlytest --producer-property acks=all --property parse.key=true --property key.separator=:

>> 12345:hello
Enter fullscreen mode Exit fullscreen mode

Access to Cassandra:

docker exec -it CONTAINER_ID cqlsh
Enter fullscreen mode Exit fullscreen mode

Get the network information

docker network ls | grep "camel"
Enter fullscreen mode Exit fullscreen mode

Firebase integration for Firestore.

You need to download the service_account_key.json from your Firebase project add it to the aggregated-messages-consumer folder.

Also you need to update the .env file in the frontend-dev folder to looks like:

# MICROSERVICES
NEXT_PUBLIC_AUTH_MICROSERVICE = 'http://0.0.0.0:8000'
NEXT_PUBLIC_CHAT_MICROSERVICE = 'http://0.0.0.0:9000'
# FIREBASE
NEXT_PUBLIC_API_KEY="..."
NEXT_PUBLIC_AUTH_DOMAIN="..."
NEXT_PUBLIC_PROJECT_ID="..."
NEXT_PUBLIC_STORAGE_BUCKET="..."
NEXT_PUBLIC_MESSAGING_SENDER_ID="..."
NEXT_PUBLIC_APP_ID="..."
Enter fullscreen mode Exit fullscreen mode

License

Apache License 2.0

Top comments (0)