Aiven for PostgreSQL + Valkey: Building a High‑Performance, Cache‑Accelerated Architecture

Aiven for Valkey and Aiven for PostgreSQL, a duo that brings together the reliability of PostgreSQL’s database with the  performance of Valkey’s in-memory caching. In this blog post, we’ll explore how to leverage these managed services to build a scalable, high-performance application.

Aiven for Valkey

Use Aiven for Valkey and its in-memory data store technology as a supplementary data system alongside primary databases like PostgreSQL. Ideal for transient data, caching values for quick access, and data that can be reestablished, such as session data.

Aiven for Valkey provides a fully managed Valkey service with:

  • Automated backups and updates.
  • High availability with automatic failover.
  • Built-in monitoring and alerting.
  • Enterprise-grade security with VPC peering and encryption.
  • Multi-cloud deployment options (AWS, Google Cloud, Azure,Digital Ocean).

Ways to use Valkey

  • Use Aiven for Valkey and its in-memory data store technology as a supplementary data system alongside primary databases like PostgreSQL.
  • Ideal for transient data, caching values for quick access, and data that can be reestablished, such as session data. While this service is not inherently a persistent storage solution, it can be configured for persistence.
  • Perfect for high-performance applications requiring fast data access, real-time analytics, session management, and distributed caching scenarios.

Why Combine Valkey with PostgreSQL?

PostgreSQL excels as a persistent, reliable data store with strong consistency guarantees and complex querying capabilities. However, disk-based databases have inherent latency limitations. Valkey complements PostgreSQL by:

  • Reducing Database Load: Cache frequently accessed data to minimize expensive database queries.
  • Improving Response Times: Serve data from memory (sub-millisecond) instead of disk (milliseconds).
  • Scaling Read Operations: Handle thousands of concurrent read requests without overwhelming PostgreSQL.
  • Session Management: Store user sessions and temporary data efficiently.
  • Rate Limiting: Implement API rate limiting and throttling mechanisms.

Aiven’s highlight PostgreSQL + Valkey as a classic “database + cache” pattern:

  • Use PostgreSQL as the system of record for durable, relational data (for example, orders, users, products).
  • Use Aiven for Valkey as a cache layer in front of PostgreSQL to speed up read-heavy workloads, such as product detail pages, user profiles, or frequently-run analytics queries. A concrete example use case:

We  have an e‑commerce app backed by Aiven for PostgreSQL:

  • Product details and inventory are stored in PostgreSQL.
  • The app first checks Valkey for product:
    • If present, it returns the cached JSON (very fast).
    • If missing, it queries PostgreSQL, returns the result, and writes it into Valkey with a short TTL.
  • This reduces PostgreSQL load and improves response times for popular products, while PostgreSQL remains the authoritative source of truth.

A brief steps to add caching to your PostgreSQL database using Valkey

  1. Create Aiven services
    • Create an Aiven for PostgreSQL service (your primary database).
    • Create an Aiven for Valkey service (your cache). 
  1. Decide what to cache
    • Identify read-heavy, not‑too‑sensitive data from PostgreSQL (for example, product details, user profiles, or query results) that can safely be cached in Valkey. 
  1. Connect your app to both services
    • Keep your existing PostgreSQL connection.
    • Add a Valkey client in your app using one of the language guides (Go, NodeJS, Python, PHP, Java, or valkey-cli) and the Valkey service URI from the Aiven Console. 
  1. Implement the cache‑aside pattern
    For each read you want to speed up:
    • Check Valkey first (for example, key like user:123 or product:42).
    • If found, return the cached value.
    • If not found, query PostgreSQL, then write the result into Valkey (optionally with a TTL) and return it. 
  1. Handle writes and invalidation
    • When you update or delete data in PostgreSQL, either:
      • Delete or update the corresponding key in Valkey, or
      • Let the cache expire naturally via TTL if your use case tolerates slightly stale data. 
  1. Tune Valkey memory and eviction
    • In the Aiven Console, configure maxmemory and an eviction policy such as allkeys-lru or allkeys-random so old or less‑used cache entries are evicted automatically when memory is full.

Because the detailed “Add caching to your PostgreSQL database using Valkey” blog steps are not included in the provided sources, I can’t reproduce that exact tutorial flow, but the steps above reflect the documented way to use Valkey as a cache alongside PostgreSQL.

OSDB achieves by using Aiven for Valkey and PostgreSQL

  • Reading or writing relational data in Aiven for PostgreSQL (for example, rows in a table), and
  • Returning fast responses backed by Aiven for Valkey cache hits (like the “hello world” examples above)
Example of Valkey with PostgreSQL:

Typical runtime behavior and outcomes

  • First request (cache miss):
SELECT title, description FROM film WHERE film_id = 42;
  • It then writes the result into Valkey, for example using a Valkey client:
    valkey_client.set(‘film:42’, ‘<JSON from PostgreSQL>’)
    • Outcome: page loads a bit slower (DB hit), but data is now cached.
  • Subsequent requests (cache hit):
    • The app reads directly from Valkey:
    • key = valkey_client.get(‘film:42’).decode(‘utf-8’)
print('The value of key is:', key)
  • Output pattern (for example):
    The value of key is: hello world

Conclusion

Combining Aiven for Valkey with Aiven for PostgreSQL provides a powerful, production-ready architecture for building high-performance applications. The managed nature of both services eliminates operational overhead, letting you focus on building features rather than managing infrastructure.

For our TechGear e-commerce use case, we achieved 90%+ improvements in response times, dramatically reduced database load, and handled traffic spikes effortlessly—all while maintaining data consistency and reliability.

Whether you’re building an e-commerce platform, a social network, a SaaS application, or any data-intensive system, this architecture pattern can be adapted to your needs. The key is understanding your data access patterns and implementing intelligent caching strategies.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top