Calm and Rational Use of Redis or Memcached

Calm and Rational Use of Redis or Memcached

As webmasters, we often hear about the two server-side caching extensions, Memcached and Redis. However, few of us truly understand their differences and when to use them. In this article, we will delve into the world of caching and explore the benefits and drawbacks of each option.

A Brief Introduction to Memcached and Redis

Memcached is a free, open-source, high-performance, distributed memory object caching system. It was initially developed by LiveJournal’s Brad Fitzpatrick and has since become a crucial component of many web applications, including those of mixi, hatena, Facebook, Vox, and LiveJournal. Memcached is a memory-based key-value store that allows for the caching of database query results, reducing the number of database accesses and improving the speed of dynamic web applications.

Redis, on the other hand, is a Remote Dictionary Server (Redis) that is written in ANSI C and complies with the BSD agreement. It is an open-source, in-memory data structure store that provides a rich set of data types, including strings, lists, hashes, sets, and sorted sets. Redis is designed to be a high-performance, multi-language API that can handle a large number of concurrent requests.

Key Features of Memcached and Redis

Memcached has the following features:

  • Simple protocol
  • Libevent-based event processing
  • Built-in memory storage
  • Distributed caching with high-speed operation

Redis has the following features:

  • High performance, with read speeds of up to 110,000 times per second and write speeds of up to 81,000 times per second
  • Rich data types, including strings, lists, hashes, sets, and sorted sets
  • Atomic operations, ensuring that either a single operation succeeds or fails
  • Support for transactions, ensuring atomicity and consistency

What’s the Difference Between Redis and Other Key-Value Stores?

Redis has a more complex data structure than other key-value stores, providing atomic operations on them. This is different from the evolutionary path of other databases. Meanwhile, Redis data types are based on transparent basic data structures, without the need for additional abstraction. Redis runs in memory but can be persisted to disk, allowing for trade-offs between read and write speeds. Another advantage of in-memory databases is that they operate on simple memory on disk, allowing for complex internal matters.

Choosing Between Memcached and Redis

When choosing between Memcached and Redis, we should consider the following factors:

  • Hardware configuration: Memcached has lower demands on hardware configuration, while Redis requires enough memory to enhance performance.
  • Performance: Redis has a more significant impact on performance, but it also requires more memory and may introduce security risks.
  • Scalability: Both Memcached and Redis can improve scalability, but Redis is more suitable for high-traffic websites.

A Word of Caution

While Memcached and Redis can improve performance and scalability, they also introduce security risks and vulnerabilities. Memcached has a default port of 11211, which has been subject to serious vulnerabilities in the past. Redis also has security risks, including the possibility of memory exhaustion and denial-of-service attacks. Additionally, using Memcached or Redis with a Content Delivery Network (CDN) can lead to increased server load and traffic requests.

Conclusion

In conclusion, Memcached and Redis are both powerful caching extensions that can improve performance and scalability. However, they also introduce security risks and vulnerabilities. When choosing between them, we should consider the factors mentioned above and ensure that our server has enough memory to support the chosen caching extension. By being calm and rational in our approach, we can make informed decisions that benefit our website and users.