Background:
Hi, I am running a MongoDB replica set on Azure and have connected to it remotely from within an Android app. I\'ve gotten reads to work gre
If the instances are all sitting behind a single load-balanced Input
endpoint (e.g. 27017), then each time your client machine connects to the endpoint, it will be connected to potentially different nodes in the replicaset cluster (and you'd have no control over which instance you went to). This might explain why you sometimes attempt to write to the non-master and get an error, yet all your reads work (since you probably set up the MongoDB cluster to allow reads on secondary nodes).
Worker roles also support InstanceInput
endpoints, which allow you to set an externally-facing port range (say, 27017-27019), mapping to a single port on the worker instances themselves (e.g. 27017). If you do this, your client app can now connect to all three instances directly (27017, 27018, 27019). Many drivers support replicaset connections, so it would be able to figure out which node is the master, directing all writes to it. I don't know if the driver you're using on Android supports replicasets. If the driver doesn't support replicasets, then you'll probably want to consider putting up an API tier which then does all communication to the database (a good practice to follow in general, anyway, and you can look at Azure's Mobile Services for a quick way to implement this).
So... if your replicaset cluster's endpoint is configured as Input
, this likely explains the issue you're seeing, which should be resolvable by switching the endpoint type to InstanceInput
.