Why Scaling writes is the hardest problem
Sometimes Writing to database is more hard than reading.
Scaling writes is harder compared to reads. Because when you read the data, you are not changing anything. So, using replicas and caching, it becomes easy to make reads fast.
Writes are tough because they change the single source of truth. When multiple people are trying to modify the same document, then the system must coordinate to maintain consistency.
Generally, one primary database handles all the write operations, but it becomes a bottleneck under high load.
Some of the major write bottlenecks are:
- Lock Contention: It means when multiple people are trying to access the same shared resource, they will block each other. Basically, they are trying to change the same row of a table.
- Disk I/O Limits: When you write to disk, it is generally slower than reading because every database write involves disk operations like index updates. Also writes the changes to WAL (Write Ahead Log). So the data can be recovered if the database crashes.
- Hot Key: Even when database is sharded ad you have 100 shards, if all request for that shard goes to same single shard then will will become bottlenecks and this is hot key problem.
How to handle this issue ?
1. Vertical Scaling : We can upgrade the hardware (CPU, or add more RAM) which will give more space to database. We can also optimize the database writes by reducing the number of indexes. Also we can tune the database settings like optimizing the storage engines.
2. Sharding & Partitioning : When single machine is not enough then we move to horizontal scaling.
sharding means splitting the database into multiple pieces ( Partitions).
Each shard handles its own portion. We can shard by userid or region
3. Queuing : If lots of data coming for writes, then one more best way to handle this is put all the data in queue and consume it in steady rate.
4. Batching : Another way to handle write performance is doing more things in one operation. Means bulk write in one shot. Batching means grouping multiple write action together and performing them as one operation.
Related articles
- I never thought Google drive or Dropbox system are built like this
So, I was uploading the file on Google Drive, it was a book of 92 MB in size. Then suddenly I thought how Google Drive is working internally, how does Google Drive upload this file. What will happen if the file is really big like in GBs.