Everything you need as a full stack developer

Caching Basics (Redis/Memcached)

- Posted in Junior Developer by

TL;DR Caching is a crucial technique for optimizing application performance, storing frequently accessed data in a location that allows for quicker retrieval, resulting in improved response times, decreased load on databases and APIs, and enhanced overall system performance. Two popular caching solutions are Redis and Memcached, which can be used as a cache layer, message broker, or even a database, offering incredible speed, simplicity, and versatility.

Caching Basics: Unlocking Performance with Redis and Memcached

As a full-stack developer, you're no stranger to the importance of optimizing your application's performance. One crucial technique in achieving this goal is caching – storing frequently accessed data in a location that allows for quicker retrieval. In this article, we'll delve into the basics of caching, exploring two popular caching solutions: Redis and Memcached.

Why Caching?

Before diving into the details, let's discuss why caching is essential. Imagine your application makes repeated requests to a database or an external API. Each request incurs latency, consuming valuable resources and affecting user experience. By storing frequently accessed data in a cache layer, you can significantly reduce the number of requests made to these slower systems. This results in:

  • Improved response times
  • Decreased load on databases and APIs
  • Enhanced overall system performance

Redis: An In-Memory Data Store

Redis (Remote Dictionary Server) is an in-memory data store that can be used as a cache layer, message broker, or even a database. Its popularity stems from its incredible speed, simplicity, and versatility.

Installing Redis

To get started with Redis, you'll need to install it on your system. For Ubuntu-based systems, run:

sudo apt-get install redis-server

For other platforms, refer to the official Redis documentation.

Basic Redis Commands

Once installed, open a terminal and connect to the Redis server using redis-cli:

redis-cli

Now, let's explore some basic Redis commands:

  • SET key value: Sets a key-value pair in the cache.
SET hello world
  • GET key: Retrieves the value associated with a given key.
GET hello

Output: "world"

  • EXPIRE key ttl: Sets a time-to-live (TTL) for a key, after which it will expire and be removed from the cache. TTL is specified in seconds.
EXPIRE hello 30

This sets the hello key to expire in 30 seconds.

Using Redis as a Cache Layer

Imagine you're building a web application that displays a list of popular articles. Instead of querying the database on each request, you can cache the result using Redis:

import redis

# Connect to Redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)

def get_popular_articles():
    # Check if the result is cached
    cached_result = r.get('popular_articles')
    if cached_result:
        return cached_result.decode('utf-8')

    # If not cached, query the database and cache the result
    articles = db.query("SELECT * FROM articles ORDER BY views DESC LIMIT 10")
    r.setex('popular_articles', 3600, json.dumps(articles))  # Cache for 1 hour
    return articles

print(get_popular_articles())

In this example, we first check if the popular_articles key exists in Redis. If it does, we return the cached result. Otherwise, we query the database, cache the result with a TTL of 1 hour, and return the articles.

Memcached: A High-Performance Caching System

Memcached is another popular caching solution that's widely used in web applications. It's a high-performance, distributed memory object caching system.

Installing Memcached

To install Memcached on Ubuntu-based systems, run:

sudo apt-get install memcached

For other platforms, refer to the official Memcached documentation.

Basic Memcached Commands

Memcached uses a binary protocol for communication. We'll use the telnet command to interact with the Memcached server:

telnet localhost 11211

Now, let's explore some basic Memcached commands:

  • set key flags exptime bytes [noreply] value: Sets a key-value pair in the cache.
set hello 0 30 5
world

This sets the hello key with a TTL of 30 seconds and stores the value "world".

  • get key: Retrieves the value associated with a given key.
get hello

Output: "world"

Using Memcached as a Cache Layer

Let's revisit the previous example, this time using Memcached:

import memcache

# Connect to Memcached
mc = memcache.Client(['localhost:11211'])

def get_popular_articles():
    # Check if the result is cached
    cached_result = mc.get('popular_articles')
    if cached_result:
        return cached_result

    # If not cached, query the database and cache the result
    articles = db.query("SELECT * FROM articles ORDER BY views DESC LIMIT 10")
    mc.set('popular_articles', articles, 3600)  # Cache for 1 hour
    return articles

print(get_popular_articles())

In this example, we first check if the popular_articles key exists in Memcached. If it does, we return the cached result. Otherwise, we query the database, cache the result with a TTL of 1 hour, and return the articles.

Conclusion

Caching is a crucial technique for optimizing application performance. Redis and Memcached are two popular caching solutions that can help you achieve this goal. By understanding the basics of these technologies and incorporating them into your workflow, you'll be well on your way to building faster, more efficient applications.

Key Use Case

Here's a meaningful example:

A popular e-commerce website displays a list of top-selling products on its homepage. This list is updated daily based on sales data from the previous day. However, generating this list requires a complex database query that takes around 5 seconds to execute.

To improve performance, the developer decides to cache the result using Redis. They set up a Redis instance and create a script that runs daily to update the cached list of top-selling products.

When a user visits the homepage, the application first checks if the cached list exists in Redis. If it does, the application returns the cached result instantly, reducing the response time from 5 seconds to under 100 milliseconds. If not, the application queries the database, caches the result with a TTL of 24 hours, and returns the list.

This caching solution significantly improves the user experience, reduces the load on the database, and enhances overall system performance.

Finally

Caching Strategies

When implementing a cache layer, it's essential to consider your caching strategy. This involves deciding what data to cache, how long to cache it, and when to invalidate the cache. A well-designed caching strategy can make a significant difference in application performance.

One popular strategy is the "cache-aside" approach, where the cache is updated independently of the main database or API. Another strategy is the "write-through" approach, where writes are simultaneously made to both the cache and the underlying data store. Each strategy has its advantages and disadvantages, and the choice ultimately depends on your specific use case and requirements.

Recommended Books

• "Design Patterns" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides • "Clean Architecture: A Craftsman's Guide to Software Structure and Design" by Robert C. Martin • "Building Evolutionary Architectures: Support Constant Change" by Neal Ford, Patrick Kua, and Paulo Bernardino

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more