Build a simple chat application
How do we build a simple chat application using Redis?
The diagram below shows how we can leverage the pub-sub functionality of Redis to develop a chat application.
š¹Stage 1: Connection Initialization
Steps 1 and 2: Bob opens the chat application. A web socket is established between the client and the server.
Steps 3 and 4: The pub-sub server establishes several connections to Redis. One connection is used to update the Redis data models and publish messages to a topic. Other connections are used to subscribe and listen to updates for topics.Ā
Steps 5 and 6: Bobās client application requires the chat member list and the historical message list. The information is retrieved from Redis and sent to the client application.
Steps 7 and 8: Since Bob is a new member joining the chat application, a message is published to the āmember_addā topic, and as a result, other participants of the chat application can see Bob.
š¹Stage 2: Message Handling
Step 1: Bob sends a message to Alice in the chat application.
Step 2: The new chat message is added to Redis SortedSet by calling āzadd.ā The chat messages are sorted based on arrival time. The pub-sub server then publishes the chat message to the āmessagesā topic so subscribers can pick it up.
Step 3: Aliceās client application receives the chat message from Bob.
š Over to you: What backend stack is commonly used to build a large-scale chat application?
Evolution of Airbnbās microservice architecture
Airbnbās microservice architecture went through 3 main stages. This post is based on the tech talk by Jessica Tai. See the reference link at the end of the thread.
Monolith (2008 - 2017)
Airbnb began as a simple marketplace for hosts and guests. This is built in a Ruby on Rails application - the monolith.Ā
Whatās the challenge?
- Confusing team ownership + unowned code
- Slow deploymentĀ
Microservices (2017 - 2020)
Microservice aims to solve those challenges. In the microservice architecture, key services include:
- Data fetching service
- Business logic data service
- Write workflow service
- UI aggregation service
- Each service had one owning team
Whatās the challenge?
Hundreds of services and dependencies were difficult for humans to manage.
Micro + macroservices (2020 - present)
This is what Airbnb is working on now. The micro and macroservice hybrid model focuses on the unification of APIs.
Over to you - why do you think both Airbnb and Netflix use GraphQL?
Reference: The Human Side of Airbnbās Microservice Architecture
Consistent Hashing
Algorithms you should know for System Design | Algorithm 1
Digital wallet in traditional banks vs wallet in blockchain
How does blockchain change the design of digital wallets? Why do VISA and PayPal invest in blockchains?
The diagram below explains the differences.
In banking systems
š¹Deposit process: Bob goes to Bank of America (BoA) to open an account and deposit $100. A new account B1234 is created in the wallet system for Bob. The cash goes to the bankās vault and Bobās wallet now has $100. If Bob wants to use the banking services of Citibank (Citi,) he needs to go through the same process all over again.
š¹Transfer process: Bob opens BoAās App and transfers $50 to Aliceās account at Citi. The amount is deducted from Bobās account B1234 and credited to Aliceās account C512. The actual movement of cash doesnāt happen instantly. It happens after BoA and Citi settle all transactions at end-of-day.
š¹Withdrawal process: Bob withdraws his remaining $50 from account B1234. The amount is deducted from B1234, and Bob gets the cash.
With Blockchains
š¹Deposit & Withdraw: Blockchains support cryptocurrencies, with no cash involved. Bob needs to generate an address as the transfer recipient and store the private key in a crypto wallet like Metamask. Then Bob can receive cryptocurrencies.
š¹Transfer: Bob opens Metamask and enters Aliceās address, and sends it 2 ETHs. Then Bob signs the transaction to authorize the transfer with the private key. When this transaction is confirmed on blockchains, Bobās address has 8 ETHs and Aliceās address has 101 ETHs.
š Can you spot the differences?Ā
Blockchain is distributed ledger. It provides a unified interface to handle the common operations we perform on wallets. Instead of opening multiple accounts with different banks, we just need to open a single account on blockchains, which is the address.Ā
All transfers are confirmed on blockchains in pseudo real-time, saving us from waiting until end-of-day reconciliations.
With blockchains, we can merge wallet services from different banks into one global service.
Thanks for making it this far! š¤
If you want to learn more about System Design, check out our books:
Paperback edition: https://geni.us/XxCd
Digital edition: https://bit.ly/3lg41jK
I can imagine Airbnb and Netflix use GraphQL so they can have a business layer abstracting micro-services. If there are 10 services involved to respond to a business domain query, sending the request to a GraphQL will hide the complexity of gathering all information from the micro-services. This will also help shield services when their dependencies migrate to macro-services.
so are we going to have different topic for each subscriber , or is it related to the group (like messages, message_history, )