How can I use MongoDB as a cache for Postgresql?

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.


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