Database Replication and Read/Write Splitting
Think of a school with one head teacher who keeps the official mark book. Only the head teacher writes new marks in it. Every evening, assistants copy the new marks into their own books, so students can ask any assistant for their marks. But if you ask an assistant just after a new mark goes in, the assistant may not have it yet. Database replication works the same way, with the same risk.
5 min read5 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 7 sources7টি সোর্স · updated Nov 2026আপডেট: Nov 2026
A primary, its replicas, and a stale readprimary, replica আর একটি stale read
Write a payment, read it at once from a replica, and see old data.একটি payment লিখুন, সঙ্গে সঙ্গে replica থেকে পড়ুন, আর পুরোনো data দেখুন।
You’ll learnআপনি শিখবেন
- Why writes go to the primaryকেন write যায় primary-তে
- What replication lag isreplication lag কী
- Why a replica read can be staleকেন replica read stale হতে পারে
- How to split reads by needপ্রয়োজন অনুযায়ী read কীভাবে ভাগ করবেন
In plain words
Database replication means that one database copies its data to one or more other databases. Each copy is a replica Source 5সোর্স 5. The database that can change data is the primary. The copies that follow its changes are standby or secondary servers Source 3সোর্স 3. A read replica is a read-only copy of the database Source 1সোর্স 1.
Read/write splitting means that the application sends each query to the right place. Writes go to the primary, such as registration, payment, enrollment, and updates. Reads that are heavy but not urgent go to replicas, such as course lists, reports, analytics, and dashboards. Clients can read and write on the primary, but they can only read on the replica Source 1সোর্স 1. When you send queries to the replica, the primary gets less load Source 1সোর্স 1.
The copy can be synchronous or asynchronous:
- Synchronous: a change is done only when all servers have it. A failover then loses no data Source 3সোর্স 3.
- Asynchronous: the replica gets the change a little later. A failover can lose some changes, and replicas can return slightly old ("stale") results Source 3সোর্স 3. Systems use it when synchronous copying is too slow Source 3সোর্স 3.
Amazon RDS, for example, copies changes to a read replica asynchronously Source 1সোর্স 1. The delay is called replication lag. On Amazon RDS, you can watch it with the ReplicaLag metric Source 2সোর্স 2. The PostgreSQL manual says that the same query on the primary and the standby, at almost the same time, can return different results Source 4সোর্স 4. It calls the standby "eventually consistent": it gets the change, but not at once Source 4সোর্স 4.
Lag grows when the primary cannot send changes fast enough, the replica cannot receive them fast enough, or the replica cannot apply them fast enough Source 7সোর্স 7.
Open the image in full sizeছবিটি পূর্ণ আকারে খুলুনসহজ English
Database replication means that one database copies its data to one or more other databases. Each copy is a replica Source 5সোর্স 5. Only the primary database can change data. The other servers follow its changes Source 3সোর্স 3. A read replica is a copy that you can only read from Source 1সোর্স 1.
With read/write splitting, the app sends writes, such as payments and enrollments, to the primary. It sends heavy reads that are not urgent, such as reports and dashboards, to replicas. This gives the primary less load Source 1সোর্স 1.
With synchronous copying, a change is done only when all servers have it, so a failover loses no data Source 3সোর্স 3. With asynchronous copying, the replica gets the change later. A failover can lose some changes, and a replica can return old ("stale") data Source 3সোর্স 3.
Amazon RDS copies changes to a read replica asynchronously Source 1সোর্স 1. The delay is called replication lag. On Amazon RDS, you can watch it with the ReplicaLag metric Source 2সোর্স 2. Lag grows when the primary cannot send changes fast enough, or the replica cannot receive or apply them fast enough Source 7সোর্স 7.
বাংলা ব্যাখ্যা
Database replication মানে হলো, একটি database তার data এক বা একাধিক অন্য database-এ copy করে। প্রতিটি copy-কে replica বলে Source 5সোর্স 5। যে database data পরিবর্তন করতে পারে, সেটি primary। যে copy-গুলো তার পরিবর্তন অনুসরণ করে, সেগুলো standby বা secondary server Source 3সোর্স 3। Read replica হলো database-এর একটি read-only copy Source 1সোর্স 1।
Read/write splitting-এ application প্রতিটি query সঠিক জায়গায় পাঠায়। Registration, payment, enrollment আর update-এর মতো write যায় primary-তে। Course list, report, analytics আর dashboard-এর মতো ভারী কিন্তু জরুরি নয় এমন read যায় replica-তে। Client primary-তে read ও write দুটোই করতে পারে, কিন্তু replica-তে শুধু read করতে পারে Source 1সোর্স 1। Replica-তে query পাঠালে primary-এর load কমে Source 1সোর্স 1।
Copy synchronous বা asynchronous হতে পারে। Synchronous-এ একটি পরিবর্তন তখনই শেষ হয়, যখন সব server সেটি পায়; তাই failover-এ কোনো data হারায় না Source 3সোর্স 3। Asynchronous-এ replica পরিবর্তনটি একটু পরে পায়। তখন failover-এ কিছু পরিবর্তন হারাতে পারে, আর replica কিছুটা পুরোনো ("stale") ফল দিতে পারে Source 3সোর্স 3। Synchronous copy খুব ধীর হলে system asynchronous ব্যবহার করে Source 3সোর্স 3।
উদাহরণ হিসেবে, Amazon RDS read replica-তে পরিবর্তন asynchronously copy করে Source 1সোর্স 1। এই দেরিকে replication lag বলে। Amazon RDS-এ আপনি ReplicaLag metric দিয়ে এটি দেখতে পারেন Source 2সোর্স 2। PostgreSQL manual বলে, প্রায় একই সময়ে primary আর standby-তে একই query ভিন্ন ফল দিতে পারে, এবং standby-কে "eventually consistent" বলে Source 4সোর্স 4। Primary যথেষ্ট দ্রুত পরিবর্তন পাঠাতে না পারলে, অথবা replica যথেষ্ট দ্রুত সেগুলো গ্রহণ বা apply করতে না পারলে lag বাড়ে Source 7সোর্স 7।
When you need it
- Read traffic is very high.
- Reports or analytics slow down the primary database. AWS names business reporting as a use case for read replicas Source 1সোর্স 1.
- You must plan for high availability.
- You want to keep critical writes apart from reads that are not critical.
- You need a failover plan for the future. A replica can become a standalone database if the primary fails Source 1সোর্স 1.
সহজ English
You need replication in these cases:
- The app gets a very high number of reads.
- Reports or analytics make the primary database slow. AWS names business reporting as a use case for read replicas Source 1সোর্স 1.
- You must plan for high availability.
- You want to keep the critical writes apart from the reads that are not critical.
- You need a plan for a failover in the future. If the primary fails, a replica can become a standalone database Source 1সোর্স 1.
বাংলা ব্যাখ্যা
নিচের যেকোনো অবস্থায় আপনার database replication দরকার:
- Read traffic অনেক বেশি।
- Report বা analytics primary database-কে ধীর করে দেয়। AWS read replica-এর একটি use case হিসেবে business reporting-এর কথা বলে Source 1সোর্স 1।
- আপনাকে high availability-এর পরিকল্পনা করতে হবে।
- আপনি জরুরি write-গুলোকে কম জরুরি read থেকে আলাদা রাখতে চান।
- ভবিষ্যতের জন্য আপনার একটি failover পরিকল্পনা দরকার। Primary fail করলে একটি replica একটি standalone database হয়ে যেতে পারে Source 1সোর্স 1।
A real-life example
A student pays for a course in the LMS (learning management system). The payment success is written to the primary database.
Then the app opens the dashboard at once and reads the payment status from a replica. If there is replication lag, the replica does not have the payment yet. So the student sees "Unpaid" and may pay again.
A better design reads critical data, such as payment status, from the primary right after the payment. Another option is to show a "Processing" state until the status is confirmed. Course lists and reports can still come from replicas, where a few seconds of delay is safe.
সহজ English
A student pays for a course in the LMS (learning management system). The app writes the payment success to the primary database.
Then the app opens the dashboard at once. It reads the payment status from a replica. If there is replication lag, the replica does not have the payment yet. So the student sees "Unpaid" and may pay again.
A better design reads critical data, such as the payment status, from the primary right after the payment. Another option is to show a "Processing" state until the status is confirmed. Course lists and reports can still come from replicas. There, a delay of a few seconds is safe.
বাংলা ব্যাখ্যা
ধরুন, একজন শিক্ষার্থী LMS (learning management system)-এ একটি course-এর জন্য payment করেন। Payment সফল হওয়ার তথ্য primary database-এ লেখা হয়।
এরপর app সঙ্গে সঙ্গে dashboard খোলে এবং একটি replica থেকে payment status পড়ে। Replication lag থাকলে replica-তে তখনও payment-টি নেই। ফলে শিক্ষার্থী "Unpaid" দেখেন এবং আবার payment করতে পারেন।
একটি ভালো design হলো, payment-এর ঠিক পরে payment status-এর মতো জরুরি data primary থেকে পড়া। আরেকটি উপায় হলো, status confirm না হওয়া পর্যন্ত একটি "Processing" অবস্থা দেখানো। Course list আর report তবুও replica থেকে আসতে পারে, কারণ সেখানে কয়েক সেকেন্ডের দেরি নিরাপদ।
Common mistakes
- Sending all reads to replicas. This breaks "read after write" flows, where users must see their own change at once. Replicas can return stale results Source 3সোর্স 3.
- Thinking replication is the same as backup. Replication copies every change and keeps no old copies Source 5সোর্স 5. If someone deletes data by mistake, the delete goes to every replica too Source 5সোর্স 5. A backup is a copy from a point in time that you can restore Source 5সোর্স 5.
- Not monitoring replication lag. Lag grows silently. On Amazon RDS, you can watch it with the
ReplicaLagmetric in CloudWatch Source 2সোর্স 2. Set an alert on it. - Not testing failover. With asynchronous copying, some changes can be lost when the system switches to another server Source 3সোর্স 3. Also, not every copy is a read replica. An RDS Multi-AZ standby, for example, is a synchronous copy for high availability, and it cannot serve reads Source 6সোর্স 6. Know which copy does which job, and test the switch.
- Ignoring report load on the primary. Heavy reports on the primary slow down payments and logins. Move them to a replica.
সহজ English
Avoid these mistakes:
- All reads go to replicas. This breaks "read after write" flows, where users must see their own change at once. A replica can return stale data Source 3সোর্স 3.
- Replication is taken as a backup. Replication copies every change and keeps no old copies Source 5সোর্স 5. If someone deletes data by mistake, every replica gets the delete too Source 5সোর্স 5. A backup is a copy from a point in time that you can restore Source 5সোর্স 5.
- Nobody watches the lag. Lag grows silently. On Amazon RDS, the
ReplicaLagmetric in CloudWatch shows it Source 2সোর্স 2. Set an alert on it. - Nobody tests failover. With asynchronous copying, some changes can be lost when the system switches to another server Source 3সোর্স 3. Not every copy is a read replica. An RDS Multi-AZ standby is a synchronous copy for high availability, and it cannot serve reads Source 6সোর্স 6. Know the job of each copy, and test the switch.
- Reports run on the primary. Heavy reports on the primary make payments and logins slow. Move them to a replica.
বাংলা ব্যাখ্যা
এই সাধারণ ভুলগুলোর দিকে খেয়াল রাখুন:
- সব read replica-তে পাঠানো। এতে "read after write" flow ভেঙে যায়, যেখানে user-কে নিজের পরিবর্তন সঙ্গে সঙ্গে দেখতে হয়। Replica stale ফল দিতে পারে Source 3সোর্স 3।
- Replication-কে backup ভাবা। Replication প্রতিটি পরিবর্তন copy করে এবং কোনো পুরোনো copy রাখে না Source 5সোর্স 5। কেউ ভুল করে data delete করলে সেই delete প্রতিটি replica-তেও চলে যায় Source 5সোর্স 5। Backup হলো একটি নির্দিষ্ট সময়ের copy, যা আপনি restore করতে পারেন Source 5সোর্স 5।
- Replication lag monitor না করা। Lag নিঃশব্দে বাড়ে। Amazon RDS-এ আপনি CloudWatch-এর
ReplicaLagmetric দিয়ে এটি দেখতে পারেন Source 2সোর্স 2। এর ওপর একটি alert সেট করুন। - Failover test না করা। Asynchronous copy-তে system অন্য server-এ switch করার সময় কিছু পরিবর্তন হারাতে পারে Source 3সোর্স 3। আবার, প্রতিটি copy read replica নয়। যেমন, RDS Multi-AZ standby হলো high availability-এর জন্য একটি synchronous copy, এবং এটি read serve করতে পারে না Source 6সোর্স 6। কোন copy কোন কাজ করে, তা জানুন এবং switch-টি test করুন।
- Primary-তে report load উপেক্ষা করা। Primary-তে ভারী report চললে payment আর login ধীর হয়ে যায়। Report-গুলো একটি replica-তে সরিয়ে নিন।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Read after write. Make a payment, change a profile, or enroll in a course. Then open the page that shows it at once. The new value must show, or the page must say "Processing".
- Lag on purpose. In a test environment, add replication lag, or pause the replica. Check which pages show old data, and whether any of them is critical.
- Double action. While the status is stale, try to pay or enroll again. The system must not charge or enroll the student two times.
- Write to a replica. Check that no write goes to a replica. A write there must fail with a clear error, not succeed silently in the wrong place.
- Failover drill. Stop the primary in a test environment. Measure how long writes fail, and check that no confirmed payment is lost after the switch.
- Restore from backup. Delete a test record, then restore it from a backup, not from a replica.
সহজ English
These test ideas are the tutorial's own advice.
- Read after write. Make a payment, change a profile, or enroll in a course. Then open the page that shows it at once. The page must show the new value, or it must say "Processing".
- Lag on purpose. In a test environment, add replication lag, or pause the replica. Find the pages that show old data. Check if any of them is critical.
- Double action. While the status is stale, try to pay or enroll again. The system must not charge or enroll the student two times.
- Write to a replica. Make sure that no write goes to a replica. A write there must fail with a clear error. It must not succeed silently in the wrong place.
- Failover drill. Stop the primary in a test environment. Measure how long the writes fail. Make sure that no confirmed payment is lost after the switch.
- Restore from backup. Delete a test record. Then restore it from a backup, not from a replica.
বাংলা ব্যাখ্যা
নিচের test ধারণাগুলো এই tutorial-এর নিজস্ব পরামর্শ।
- Read after write। একটি payment করুন, একটি profile পরিবর্তন করুন, অথবা একটি course-এ enroll করুন। তারপর সঙ্গে সঙ্গে সেই পাতাটি খুলুন, যেখানে এটি দেখায়। পাতায় নতুন value দেখাতে হবে, অথবা "Processing" লেখা থাকতে হবে।
- ইচ্ছা করে lag তৈরি। একটি test environment-এ replication lag যোগ করুন, অথবা replica-টি pause করুন। দেখুন, কোন পাতাগুলো পুরোনো data দেখায়, এবং তার কোনোটি জরুরি কি না।
- দুবার একই কাজ। Status stale থাকার সময় আবার payment বা enroll করার চেষ্টা করুন। System যেন শিক্ষার্থীর কাছ থেকে দুবার টাকা না নেয় বা দুবার enroll না করে।
- Replica-তে write। দেখুন, কোনো write যেন replica-তে না যায়। সেখানে write হলে তা একটি স্পষ্ট error দিয়ে fail করতে হবে, ভুল জায়গায় নিঃশব্দে সফল হলে চলবে না।
- Failover drill। একটি test environment-এ primary বন্ধ করুন। কতক্ষণ write fail করে, তা মাপুন। Switch-এর পরে কোনো confirmed payment হারিয়েছে কি না, তা দেখুন।
- Backup থেকে restore। একটি test record delete করুন, তারপর সেটি replica থেকে নয়, একটি backup থেকে restore করুন।
Key words
| Word | Meaning |
|---|---|
| Primary | The database that accepts writes |
| Replica | A copy of the primary that follows its changes |
| Read replica | A replica that serves read-only queries |
| Read/write splitting | Sending writes to the primary and some reads to replicas |
| Replication lag | The delay before a change on the primary reaches a replica |
| Stale read | A read that returns old data because the replica is behind |
| Synchronous | A change is done only when every copy has it |
| Asynchronous | Copies get the change a little later |
| Failover | Switching to another database when the primary fails |
| Backup | A copy from a point in time that you can restore |