Limit record replication on client device

Hello Team,
For time now, I have had issues related to an increase in the number of records replicated to client device. I have tried to use a replication depth and a purging script to manage the increase in the number of records but in vain.
Please guide me on how I can manage device replication in order to enhance the efficiency of client device.
Thanks,
Job

1 Like

Hi @Job_Isabai

The two configurations that you have mentioned are the tools to use to control the number of records that are downloaded to the client device.

Can you please share what you have tried with purging and replication_depth and how it did not work?

Thanks

Hello @diana
My replication:

"replication_depth": [
    {
      "role": "chw",
      "depth": 1
    },
    {
      "role": "chw_logout",
      "depth": 1
    },
    {
      "role": "chw_supervisor",
      "depth": 2
    }
  ]

My Purging Script:

module.exports = {
  text_expression: 'at 1 am every Monday',
  run_every_days: 7,
  cron: '0 1 * * 1', // Every Monday at 1 AM
  fn: function(userCtx, contact, reports, messages, chtScriptApi, permissions) {
    // Targeted roles
    const allowedRoles = ['chw-supervisor', 'chw', 'chw-logout'];

    // Validate role
    const hasPermission = userCtx.roles.some(role => allowedRoles.includes(role));

    // Log user roles
    console.log('Purge triggered by user with roles:', userCtx.roles);

    if (!hasPermission) {
      console.log('User does not have permission to purge.');
      return []; // Exit if user lacks the right role
    }

    // Purge threshold: 30 days ago
    const threshold = Date.now() - (1000 * 60 * 60 * 24 * 30);

    // Filter old reports
    const reportsToPurge = reports
      .filter(r => r.reported_date < threshold)
      .map(r => r._id);

    // Log how many reports are being purged
    console.log(`User with role ${userCtx.roles.join(', ')} is purging ${reportsToPurge.length} reports older than 30 days for contact ${contact._id}`);

    return reportsToPurge;
  }
};

Thanks for the update @Job_Isabai .

Can you share which of these roles you have performance issues with?
Users downloading only the last month of work seems like a reduced amount of reports, so I’m unclear what performance issues you are facing.
Could you please provide more details into what you are seeing?

Thanks!

Hello Diana,
I have a user with role chw_supervisor.
When signing in, the user downloads more than 35000 records, I have two more users with the same role experiencing the same issue.
Issue is: Increased among to records slows down the performance of CHT Mobile.
Thanks,
Job

My suspicion is that the chw_supervisor replication_depth of 2 is too much. Especially when reports are downloaded too. Most deployments I’ve seen with supervisor workflows use a replication_depth of 1 for the supervisors. You could also try using report_depth setting to only limit contact downloads: CouchDB replication – Community Health Toolkit

Also, as of 4.18 there is a setting that will allow downloading of primary contacts beyond replication depth: CouchDB replication – Community Health Toolkit , which could also be an option for you.