|
For projects where I know the team will remain small (less than let's say 15 developers), I usually push to keep the architecture as simple as possible. I've used something similar in the past, but kept the expiration code in the app code (Python) instead of using "fancy" Postgres features, like stored procedures. It's much easier to maintain since most developers will know how to read and maintain the Python code, that's also commited to the git repository. Also, instead of using basic INSERT statements, you can "upsert". INSERT INTO cache_items
(key, created, updated, key, expires, value)
VALUES (...) ON CONFLICT ON CONSTRAINT pk_cache_items
DO UPDATE SET updated = ..., key = ..., expires = ..., value = ...; And since you have control over the table, you can customize it however you want. Like adding categories of cache that you can invalidate all at once, etc. Postgres is also pretty good at key/values. In other words, I agree that using Postgres for things like caching, key/values, and even maybe message queue, can make sense, until it doesn't. When it doesn't make sense anymore, it's usually easy to migrate that one thing off of Postgres and keep the rest there. Also, one benefit that's not often talked about is the complexity of distributed transactions when you have many systems. Let's say you compute a value inside a transaction, cache it in Redis, and then the transaction fails. The cached valued is wrong. If everything is inside of Postgres, the cached value will also not be commited. One less thing to worry about. |
That just sounds like an application bug. Nothing should be done with the query result anyway until the transaction either completes our rolls back.