Redis: Beyond Simple Caching

Redis is an in-memory data structure store that serves as a cache, message broker, and real-time data engine. At Nexis Limited, Redis is a critical component across all our products — Bondorix uses it for real-time shipment tracking, Ultimate HRM for session management, and Digital Menu for live order updates.

Caching Patterns

Cache-Aside (Lazy Loading)

The application checks the cache first. On a cache miss, it reads from the database, stores the result in the cache, and returns it. This is the most common pattern and works well for read-heavy workloads. The main risk is cache stampede — when many concurrent requests miss the cache simultaneously and overwhelm the database. Mitigate with cache locking or request coalescing.

Write-Through Cache

Every write to the database also writes to the cache. This ensures the cache is always consistent with the database but adds latency to write operations. Best for data that is read frequently and updated infrequently, like user profiles or configuration settings.

Write-Behind (Write-Back) Cache

Writes go to the cache first and are asynchronously flushed to the database. This provides the fastest write performance but introduces a window where data could be lost if Redis crashes before flushing to the database. Suitable for applications that can tolerate brief data loss, like analytics counters.

Cache Invalidation Strategies

Cache invalidation is famously one of the hardest problems in computer science. Common approaches include:

  • Time-based expiry (TTL): Set a time-to-live on each cache entry. Simple and effective for data where slight staleness is acceptable.
  • Event-based invalidation: Invalidate cache entries when the underlying data changes. More complex but ensures consistency.
  • Versioned keys: Include a version number in cache keys. Increment the version to invalidate all entries of a particular type.

Redis Data Structures for Real-Time Features

  • Sorted Sets: Leaderboards, time-series data, and priority queues. We use sorted sets in Bondorix to rank vendor bids by price in real time.
  • Pub/Sub: Real-time messaging between services. Digital Menu uses pub/sub to push order updates from the kitchen to the frontend instantly.
  • Streams: Append-only log data structure for event sourcing, activity feeds, and message queues with consumer groups.
  • HyperLogLog: Probabilistic data structure for counting unique elements (unique visitors, unique events) with minimal memory usage.

Redis in Production

For production deployments, use Redis Sentinel for high availability or Redis Cluster for horizontal scaling. Monitor memory usage, connection counts, and command latency with Prometheus and Grafana. Set maxmemory policy to control behavior when Redis reaches its memory limit — allkeys-lru is the safest default.

Conclusion

Redis is far more than a simple key-value cache. Its rich data structures, pub/sub capabilities, and streams make it a versatile tool for building real-time, high-performance applications. Choose the right caching pattern for your use case, and invest in proper cache invalidation to avoid stale data issues.

Optimizing your application's performance? Our team can help design your caching layer.