You Don’t Need a Dedicated Cache Service — PostgreSQL as a Cache
PostgreSQL became a go-to SQL database for many developers over the past couple of years. While being an SQL database, Postgres also includes a lot of features that make it suitable for other uses, e.g. using it as NoSQL database (JSON and HStore datatypes) or vector database.
Another — more unusual and possibly unexpected use-case for Postgres — is using it as a cache!
In this article we will explore how we can leverage features of PostgreSQL to turn it into fully featured and very efficient caching service.
Why?
You might be asking: “Why would anyone do this, though?”, To answer that question, let’s take a look at what we might expect from a traditional caching service:
Expiration — Ability to set expiration times for cached data so that the cache doesn’t store outdated information
Eviction — Remove less frequently used data when the cache is full
Invalidation — Overwrite data when it changes
Performance — Main reason to use cache is to avoid slow database queries. SQL databases generally also have slower writes compared to caches
No persistence — Caching services will generally have limited or no persistence
Key-value storage
Well, as we will see in the next section, we can get all of the above when using PostgreSQL, on top of that we also get the following as a bonus:
0 Comments