Slow Initial Replication

Hi Everyone,

I’m experiencing very slow initial sync/replication times with Couchbase Mobile and I’m not quite sure what’s causing the problem or weather it is to be expected.

When first testing the implementation, initial sync seemed much faster but it seems to have degraded over time to the point where it takes over 5mins to replicate 5646 documents without attachments totalling ~6Mb.

It appears from the Sync Gateway Log that each document is requested individually from the Sync Gateway, is there any way to force replication to use the POST /{db}/_bulk_get Sync Gateway endpoint?

I’m using the react-native-couchbase-lite library so the problem may lie in it’s implementation. It uses the Couchbase Lite Swagger Spec to initiate replication with the POST /_replicate endpoint

I’m also using bucket shadowing but I’m not sure what impact this has on performance, or if it would explain the delays I’m seeing.

Any suggestions/advice would be much appreciated

You really need to specify what platform and version of Couchbase Lite you’re using, as the implementations are different.

It appears from the Sync Gateway Log that each document is requested individually from the Sync Gateway

Hm, that should only be happening for documents with attachments, i.e. not in your case.

Do you have any middleware between client and Sync Gateway that might be messing with the HTTP headers? In particular, if the Server: response header is removed, CBL won’t know it’s talking to Sync Gateway and will fall back to the CouchDB subset of the replication protocol, which doesn’t include _bulk_get.

Many Thanks Jens,

I’m using CBL 1.4, SG 1.5 Beta 2 and nginx as a reverse proxy and didn’t have the Server: response header pass through. Adding this has greatly reduced replication times (from >5 mins to <30 secs) my nginx config now looks as follows.

    location / {
        proxy_pass_header Accept;
        proxy_pass_header Accept-Encoding;
        proxy_pass_header Server;
        proxy_pass http://xxx.xxx.xxx.xxx:4984/;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_buffering off;
        keepalive_requests 1000;
        keepalive_timeout 360s;
    } 

I’m not sure if setting the upgrade headers is necessary for websocket passthrough as performance seems the same with or without this set.

Are there any other suggestions you could make to improve initial sync times further? Is ~30 sec expected? I know it isn’t a huge amount of wait time for the amount of work achieved but it does degrade the user experience. I’ve seen gzip compression mentioned in a few places and I’m thinking this could help but I can’t tell if it’s running. Is there any way to confirm without using a debugging proxy like Fiddler/Charles?

I’m also considering implementing a hybrid approach for data fetching with REST API fallback when records aren’t yet available but I’m worried this is unnecessary complexity. Is there any possibility of record prioritisation as part of the replication process?

I am pretty sure we have an nginx / proxy setup FAQ somewhere; @jamiltz do you know where this is?

What platform of CBL are you using, and on what device? That makes a big difference.

Is there any possibility of record prioritisation as part of the replication process?

Not explicitly, but you can simulate it by running a replication that’s limited to certain channels, or to explicit docIDs.

@danlannz Here’s the link to the NGINX setup guide https://developer.couchbase.com/documentation/mobile/1.4/guides/sync-gateway/nginx/index.html#nginx

Cheers Guys,

You’ve been a great help, good idea re. limited replication for expedited results @jens, hadn’t thought of that.
With react-native-couchbase-lite I’ve built for both iOS and Android, both using the OSX simulators. Have tested on an iPhone 7, where I get the ~30sec result. The Android simulator seems quite a bit slower but haven’t been able to test on a device yet.

@jamiltz thanks for the NGINX guide, I’m about to start testing sync with attachments and I can see its already solved a problem I would have faced.

Dan

No problem, keep us posted on how the performance turns out with the react-native plugin. But as mentioned on the readme, it’s not a plugin that’s officially supported by Couchbase :slight_smile: Mostly community driven at this point.

You might find this sample to help as well https://github.com/couchbaselabs/mobile-training-todo/tree/master/react-native