CouchDB filtered replication - remove a document
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
CouchDB offers an effective way to manage databases with its filtered replication feature, allowing users to selectively replicate data. This is especially useful when working with large datasets, enabling the synchronization of only the desired information. In this article, we'll delve into the technical aspects of CouchDB filtered replication, focusing on how to handle the removal of documents.
Understanding CouchDB Replication
Replication in CouchDB is the process of synchronizing databases across different servers or devices. This can be unidirectional or bidirectional. By default, CouchDB replicates all database documents, but with filtered replication, you can apply specific criteria to choose which documents get replicated.
Basic Replication Process:
- Source Database: The database from which documents are copied.
- Target Database: The database to which documents are copied.
- Replication Document: A special document containing replication criteria, often specifying sources, targets, and filters.
Filtered Replication
Filtered replication allows the user to define which documents should be replicated based on custom criteria. This is particularly useful when it is desirable to exclude certain types of documents from replication, such as when they contain sensitive data or are irrelevant for the target database's purpose.
Setting up a Filter
A filter function needs to be defined on the source database. This function determines which documents pass the criteria and consequently should be replicated.
Example Filter Function
In CouchDB, a filter function is written in JavaScript and looks like this:
In this example, only documents with a type field equal to 'user' will be replicated.
Executing Filtered Replication
You can initiate replication by sending a POST request to /_replicate. Here's an example CURL command:
Removing a Document from Replication
Filtered replication can also help when the goal is to remove certain documents from further replication processes. This can be done in a couple of ways:
- Adjusting the Filter: Modify the filter function to exclude documents matching certain criteria.
- Document Deletion: Alternatively, remove a document so it does not appear in replication anymore.
Example for Removing a Document:
If you wish to exclude documents where doc.status is inactive, modify the filter function:
Removing a document is simple; however, CouchDB does not actually delete documents but marks them as deleted:
Considerations
- Conflict Management: Deleted documents or those not meeting filter criteria will not get replicated, but handling conflicts during replication is crucial, especially when multiple sources modify them simultaneously.
- Performance: Keeping filters efficient is critical for performance reasons, as complex scripts could slow down the replication process.
Conclusion
Filtered replication in CouchDB is a powerful tool for managing which data gets replicated across databases. Whether you're excluding certain documents to protect sensitive data or optimizing database performance by omitting unnecessary data, understanding and implementing filtered replication can significantly enhance your CouchDB workflows.
Summary Table
| Key Points | Description |
| Replication Types | Unidirectional & Bidirectional |
| Filter Purpose | Selectively replicate documents based on criteria |
| Filter Syntax | JavaScript function within a design document |
| Initiating Replication | Use POST request to /_replicate with source, target, and filter details |
| Document Removal | Adjust filters or delete documents from source to omit from replication |
| Performance Concern | Complex filter functions may slow down replication |
| Conflict Management | Necessary when multiple sources modify documents, even those marked for deletion |
Filtered replication, when employed correctly, provides fine-tuned control over your databases, ensuring that only relevant data is synchronized across systems and enhancing the overall management process.

