mongodb - Sync two Mongodb Databases and have the client connect to the closest one
问题描述
I am using mongodb as a database for my game servers. When I started them, they were bound to just one region, I used the free tier of Mongodb Atlas in that region, India, and it worked perfectly with around 20ms of latency even on high load.
Now when I'm trying to scale up my servers and reach other regions like US-East, the latency jumps up to 500ms.
Is there a way I could open up two mongodb servers on both my India and US instances, which would always be in complete sync with each other, while the game server process would just use localhost to connect to the specific replica. I'm using pyMongo.
The players have no connection with the database, it's the game server process that manages it
解决方案
Is there a way I could open up two mongodb servers on both my India and US instances, which would always be in complete sync with each other, while the game server process would just use localhost to connect to the specific replica.
Assuming you are using a replica set, I believe the closest you can come to achieving the stated requirement for reads is by:
- Performing all writes with the write concern w=(# of nodes in the deployment), if you have two servers then use w=2.
- Performing reads with read preference=nearest and read concern=majority.
I say "closest" because I suspect read concern=majority on a secondary could return stale data even with w=(# of nodes in deployment), since I imagine the majority commit point would necessarily lag behind the point when each secondary commits each document. To guarantee that you are reading current data you must read from the primary.
Note that such a setup has (at least) two additional major drawbacks:
- If any of the servers becomes unavailable, your application ceases to be able to write any data to the database.
- Each write will wait for all servers in the deployment to store it, hence all writes will be slow.
Achieving the same requirement for reads and writes is not physically possible. (You essentially want to be able to write a document in, say, India in 20ms and have it be "instantly" available in US, i.e. be able to retrieve it in US 20 ms later, but it takes a minimum of 500 ms for data to travel from India to US.)
推荐阅读
- excel - 用逗号分隔显示 [h]:mm 数字格式数千小时
- python - psycopg2 使用列名而不是列号来获取行数据
- javascript - tabBarNavigation 标题中的菜单按钮:无法调用 this.props.navigation.openDrawer();
- azure-storage - 更新应用服务身份验证问题的诊断日志配置
- node.js - 我们可以通过 sequalize 使用迁移文件生成模型吗?
- python - Scrapy 给出 None 作为输出
- linq - 使用多个连接从数据库中检索数据的最佳 LInq 或 Ado.Net 是哪个?
- reactjs - 反应具有许多回报的 Firebase 云功能
- c - 有没有办法保持函数变量的返回类型?
- networking - 维护后 GCP 外部 IP 掉线