when you need high write throughput, then cassandra should be coming to your mind.
lets see how writes are so efficient in cassandra.
when a cassandra node receives a write request, it becomes the coordinator node and is responsible for writing data to the concerned replicas.
the node first will see the partition key and will locate the replicas where data should be written to.
cassandra follows LSM storage design to write data.
it does not write data directly to the main disk storage.
the write happens to a commit-log file (write-ahead log file kind of), so that the write is durable and not lost in case the node goes down. Its fast operation
and simultaneously, it also writes to the memtable, which is an in-memory data structure, writing to in-memory is fast too.
and once the write is done at these two places, then depending on the consistency level, the coordinator waits for the acknowledgement from the nodes and sends a response to the client.
note that from client's pov data has already been written and its durable too.
we haven't written the data to the main disk storage yet where the SSTables are stored.
now when the memtable reaches a specific threshold, then that entire data is flushed to the disk and creates an SSTable.
data is stored as immutable SSTables, meaning once an SSTable is written, it doesn't get modified.
so now data has been written to the disk.
since data is not directly written to the main SSTable storage and the write is durable too, so it's quite fast and hence throughput is high.
1
18
now internally if you want to know how does cassandra’s read path works then you need to know its storage design.
basically cassandra nodes are arranged in a ring and each node contains multiple token-ranges. token is the hashed value of the partition key.
the all work of token-range is to identify the node or nodes where data is located, that's it.
so the flow is, request comes, cassandra hashes the partition-key and see in which token range does it fall into and look for the node where it is located.
after that each node has its in-memory storage where the data is stored temporarily before being flused to the actual disk.
that in-memory storage is called the memtable and the disk where finally data is stored as sstables.
when a read query comes, it looks at both places: the memtable and the sstables and in case of conflict it picks up the latest data and return to the user.
also it does not read all tables, it uses bloom filters and indexing to read only relevant tables to serve the query.
its kind of a little complex, but that's how it is.
how does a read request get served in cassandra?
it's kind of straightforward, not a complex path.
cassandra consists of nodes/servers arranged in a circle. when a client makes a read request, one of the nodes will receive the request.
it may or may not have the data that the client asked for.
the node that receives the request becomes the coordinator, meaning it simply coordinates with the responsible nodes to serve the request.
when the request comes, it uses the partition key to figure out which nodes are responsible for the requested data.
and depending on the configured consistency level, it gets responses from the replica nodes and returns the data to the user.
if the consistency level is not satisfied, it will throw an error and the request won't be served.
so this is quite an intuitive approach to serving read requests.
1
13
lets understand cassandra's write path today, how does write happen at high level.
when client sends a write query, one of the cassandra nodes receives the write request and it acts as the coordinator.
now cassandra sees the partition key and hashes its value and sees in which token range does it fall into.
as we know, one node can contain multiple token ranges, so it finds the replica nodes responsible for that token range and sends the write to those replicas. depending on the consistency level, it waits for the required number of replicas to acknowledge the write before responding with success to the user.
if you have configured a higher consistency level, then write latency will be more because cassandra will respond only after receiving acknowledgements from the required no. of replicas.
this is the tradeoff that we have here.
1
12
how does cassandra decide where to store data?
it comes down to partitions.
each node is responsible for storing many number of partitions within it. so when a request comes, cassandra needs to figure out which partition this request belongs to and based on that it knows where to store it.
now the question is, how does it know which partition this request belongs to?
cassandra has keyspaces, tables, rows and columns.
and this is where the partition key comes into the picture.
the partition key is one or more columns that cassandra uses to decide where the data should go.
for example, user_id can be a partition key. similarly, product_category can be a partition key.
when a request comes, cassandra takes the partition key and hashes it. the hashed value gives us a token, and based on that token, cassandra knows which token range the partition belongs to.
each node is assigned a token range, which basically tells cassandra which partitions belong to that node.
for example, if a node is responsible for the token range 0 - 100 and your partition key hashes to 55 or 80, that partition will be stored on that node.
that's basically how cassandra decides what data should be stored on what node.
1
12
before jwt became popular, apps used session based authentication.
user logs in with credentials, server verifies them and stores session data in a db or cache.
this session usually had things like:
- session id
- user id
- expiration time
server sends the session id to the client, and the client sends it back with every request.
now on every request, the server checks session storage to see if the user is still authenticated and the session is valid.
this works, but at large scale it means:
- you will extra network calls
- it will have more load on db or cache
- you need centralized session storage so that any server can check the session state
then came the idea: what if instead of storing state on the server, we send a signed token that already has the needed data?
that is where jwt became useful.
flow becomes:
- user logs in
- server verifies credentials
- server creates a jwt and signs it
- jwt is sent to the client
- client sends jwt with every request
- server verifies the jwt locally
this removes the need for a session lookup on every request.
that is why jwt is useful in distributed systems and microservices where stateless auth makes scaling easier.
one heavy query should not bring your entire system down.
database replication means keeping a copy of your database on another server called a replica.
what problem does this solve?
assume you have a service with a single database handling all reads and writes. one day your analytics team runs a heavy read query for analysis, and it overloads the db. now your app is down because user requests cannot be served.
this is where replication helps. you have a primary db and one or more replicas.
the common setup is: writes go to the primary, and reads go to replicas (often called read replicas).
now if someone runs heavy read queries, it does not affect the main db.
systems can have multiple read replicas to handle user traffic and scale reads.
this also helps with availability. if a replica goes down, others can still serve reads. if the primary goes down, a replica can be promoted, though this may not be instant.
when one service goes down, it should not take everything else down with it.
when a service is failing, stop calling it for a while instead of hammering it with requests and making things worse.
let’s see the problem it solves.
assume you have 3 services: A calls B, B calls C.
C goes down: now B waits for C and eventually times out. meanwhile A keeps calling B and also waits. now requests start piling up, threads get exhausted, connection pools get full.
one service went down and pulled everything else down with it: this is cascading failure.
circuit breaker stops this chain by failing fast and giving the system time to recover.
how it works: it works in 3 states
- closed: everything normal, requests go through
- open: failures cross a limit here you stop sending requests and return error immediately
- half open: after some time allow a few requests to check if it recovered
if it works: back to closed
if it still fails: back to open
gracefully failing is also necessary part of any system.
1
25
you don’t need every service to solve the same problems.
api gateway is a single entry point in front of all your servics that handles common things so services do not have to.
suppose you have 5 services, user, auth, order, payment, product.
now you need authentication, so you add it in all 5 services.
tomorrow you need rate limiting, same thing again.
every cross cutting concern gets repeated across services. that is the pain api gateway solves.
what api gateway does:
✅ authentication: verify token once, then forward
✅ rate limiting: enforce limits at one place
✅ request routing: /orders to order service, /payments to payment service
✅ logging: one place for all traffic
✅ ssl termination: https at gateway, internal stays simple
✅ request transformation: modify headers or request before it hits service
api gateway keeps services focused on what they should do.
1
29
common patterns when indexes are not used effectively
sometimes queries do not use indexes, not always because indexes are missing but because optimizer thinks full scan is cheaper or query is not index friendly
You created an index on salary.
SELECT * FROM employees WHERE salary + 1000 > 50000;
Will the index be used in above query?
1
10
















