How can we use MongoDb with postgresql?

How can I use MongoDB as a cache for Postgresql?

  • I have an application that can not afford to lose data, so Postgresql is my choice for database (ACID) However, speed and query advantages of MongoDB are very attractive, but based on what I've read so far, MongoDB can report a successful write which may not have gone to disk, so I can't make it my mission critical db (I'll also need transactions) I've seen references to people using mysql and MongoDB together, one for the transactions and the other for queries. Please not that I'm not talking about keeping some data in one DB and the rest in another. I want to use Postgresql as a gateway to data entry, and MongoDB for reads. Are there any resources that offer an architecture/guide for Postgresql + MongoDB usage in this way? I can remember seeing this topic in Postgresql conference agenda, but I could not find the link.

  • Answer:

    One way to achieve this would be to set up a master-slave replication with the PostgreSQL database as master, and the MongoDB database as slave. You would then do all reads from MongoDB, and all writes to PostgreSQL. This post discusses such a setup using a tool called Bucardo: http://blog.endpoint.com/2011/06/mongodb-replication-from-postgres-using.html You may also be able to do it with Tungsten Replicator, although it seems designed to be used with MySQL: http://code.google.com/p/tungsten-replicator/wiki/TRCHeterogeneousReplication

sarikan at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I don't think you'll get much speed using MongoDB just as a cache. It's strengths are replication and horizontal scalability. On one computer you'd make Mongo and Postgres compete for memory, IO bandwidth and processor time. As you can not afford to loose transactions you'll be better with Postgres only. Its has efficient caching, sophisticated query planner, prepared queries and wide indexing support cause that read-only queries will be very fast - really comparable to MongoDB on a single computer. Postgres can even scale horizontally now using asynchronous, or, from version 9.1, synchronous replication.

Tometzky

I can remember seeing this topic in Postgresql conference agenda, but I could not find the link. Maybe, you are talking about this: https://www.postgresqlconference.org/content/hybrid-applications-using-mongodb-and-postgres

Pavel Shvedov

Depending how important transactions are to you, one option is to use MongoDb driver's safe mode and drop Postgresql. http://www.mongodb.org/display/DOCS/getLastError+Command

lttlrck

you may find some replacement for mongo in http://en.wikipedia.org/wiki/NoSQL or http://nosql-database.org/ that is safer and fast as well. but I advise to simplify your solution instead of making a complicated design. http://blog.nahurst.com/visual-guide-to-nosql-systems lucky

ehsan

How can you expect transactional consistency from Postgres but trust MongoDB for reads? How would you support rollbacks in this scenario? How do you detect when they've gotten out of sync? I think you're better off going with memcache and implementing a higher level object cache. Alternatively, you could consider a replication slave for reads. If you have performance needs beyond what a dedicated read slave can provide, consider denormalizing your tables on your slave system. Make sure that any of this is actually needed. For thin tables with PK lookups most modern database engines like Postgres or InnoDB are going to generally keep up with NoSQL solutions. Don't fall into the ROFLSCALE trap http://www.youtube.com/watch?v=b2F-DItXtZs

Jeremiah Gowdy

I think you can run a mongo replica set.. Let say http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial.. Then in your app you should run all write transactions on Postgresql and then on Mongo ReplicaSet.. After that you can query read operations on Mongo Replica set.. But Synchronizing will be a problem, you should work on it..

tylerdurden

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.