CouchDB
filtered replication
document removal
database management
NoSQL

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:

  1. Source Database: The database from which documents are copied.
  2. Target Database: The database to which documents are copied.
  3. 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:

javascript
1{
2  "_id": "_design/myfilter",
3  "filters": {
4    "type_filter": "function(doc, req) { 
5      return doc.type === 'user';
6    }"
7  }
8}

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:

bash
1curl -X POST http://localhost:5984/_replicate -d '{
2  "source":"source_db",
3  "target":"target_db",
4  "filter":"myfilter/type_filter"
5}'

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:

  1. Adjusting the Filter: Modify the filter function to exclude documents matching certain criteria.
  2. 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:

javascript
1{
2  "_id": "_design/myfilter",
3  "filters": {
4    "active_docs": "function(doc, req) { 
5      return doc.status !== 'inactive';
6    }"
7  }
8}

Removing a document is simple; however, CouchDB does not actually delete documents but marks them as deleted:

bash
curl -X DELETE http://localhost:5984/source_db/document_id?rev=revision_id

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 PointsDescription
Replication TypesUnidirectional & Bidirectional
Filter PurposeSelectively replicate documents based on criteria
Filter SyntaxJavaScript function within a design document
Initiating ReplicationUse POST request to /_replicate with source, target, and filter details
Document RemovalAdjust filters or delete documents from source to omit from replication
Performance ConcernComplex filter functions may slow down replication
Conflict ManagementNecessary 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.


Course illustration
Course illustration

All Rights Reserved.