Redis

CACHE TO REDUCE API CALLS

What is Redis?

Redis is an open-source, in-memory data structure store that serves as a highly performant and versatile solution for caching, messaging, and real-time analytics. It supports various data structures like strings, lists, sets, hashes, and more.

  • Can be used as cache

  • Can be used as primary database

When to Use Redis?

Use Redis when:

  • High Performance is Crucial: Redis excels in scenarios where fast read and write operations are essential, thanks to its in-memory storage.

  • Caching Requirements: It's an excellent choice for caching frequently accessed data to reduce database load and improve application performance.

  • Real-time Analytics: Redis can be used for real-time analytics and counters, such as tracking user activity or monitoring system metrics.

  • Pub/Sub Messaging: Redis provides Pub/Sub (publish/subscribe) capabilities, making it suitable for building real-time messaging systems.

  • Session Storage: It's often used to store session data in web applications due to its speed and simplicity.

How to Use Redis (Example)

Setting up a Basic Web Application with Node.js and Express

  1. Install Dependencies:

  2. Create Express Application (app.js):

  3. Run the Application:

  4. Test the Endpoint:

    • Open your browser and navigate to http://localhost:3000/example.

    • The example demonstrates checking for data in Redis cache. If found, it returns cached data; otherwise, it fetches fresh data (for example from database) and stores it in Redis.

Last updated