Lecture
MySQL and Redis are often compared to each other, since both systems are capable of storing data in RAM and providing very high access speeds. However, their purpose and operating principles differ substantially. MySQL with the MEMORY engine is a relational database in which tables are temporarily placed in RAM and accessed through the familiar SQL language. Redis, in contrast, is a specialized NoSQL in-memory database optimized for the fastest possible key-based access and for working with various data structures.
In this article we will examine the key differences between MySQL MEMORY and Redis, compare their performance, data storage methods, behavior when RAM fills up, and scaling capabilities, and also explore in which scenarios each technology is the most suitable choice.
MySQL requires a defined and structured schema.
NoSQL allows you to store any data in a «document».
MySQL is backed by a huge community.
NoSQL has a small but rapidly growing community.
NoSQL is distinguished by ease of scaling.
MySQL requires more management.
MySQL uses SQL, which is applied across many types of
databases. NoSQL is a database
based on a design with popular implementations
MySQL uses a standard query language (SQL).
NoSQL does not use a standard query language.
MySQL has many excellent reporting tools.
NoSQL offers a few reporting tools that are difficult to standardize.
MySQL may run into performance issues with big data.
NoSQL delivers excellent performance on big data.
When comparing MySQL with the MEMORY engine (formerly HEAP) and Redis, it is important to understand that these are solutions for different tasks. Even though both use RAM, their architectures differ greatly.
| Criterion | MySQL MEMORY | Redis |
|---|---|---|
| Data model |
Relational tables, however it is possible to store a key and field in JSON, which is equivalent to Redis |
NoSQL (key → value and complex data structures) |
| Query language | Full SQL | Redis commands (GET, SET, HSET, ZADD, etc.) |
| Relations (JOIN) | Yes | No |
| Indexes | B-tree and Hash | Not needed — O(1) key-based access |
| Speed | Very high | Usually higher than MySQL MEMORY |
| Transactions | SQL transactions (with MEMORY engine limitations) | MULTI/EXEC, Lua scripts |
| Data types | Table | Strings, lists, sets, hashes, sorted sets, streams, and more |
| Persistent storage | No (data is lost on restart) | Optionally available (RDB, AOF) |
| Replication | Through MySQL | Built-in |
| Clustering | Through MySQL Cluster and others | Built-in Redis Cluster |
| Situation | MySQL MEMORY | Redis |
|---|---|---|
| RAM runs out | New records are not inserted, the query returns an error | Behavior depends on the maxmemory setting |
| Already existing data | Remains accessible | May be deleted automatically or the write will be rejected |
| Server operation | MySQL keeps running | Redis keeps running, but acts according to the memory policy |
ENGINE=MEMORY tables use RAM to store rows.
If there is no memory left:
Example error:
ERROR 1114 (HY000): The table 'cache' is full
Moreover, the cause may lie not only in a lack of physical memory, but also in reaching the max_heap_table_size or tmp_table_size limits.
Redis also stores data in RAM, but has more flexible memory management.
If memory is full and the maxmemory parameter is set, various scenarios are possible:
Therefore Redis can work as a true cache, freeing up memory on its own.
If all of the computer's RAM is occupied:
MySQL MEMORY
Example:
CREATE TABLE cache (
id INT PRIMARY KEY,
value VARCHAR(255)
) ENGINE=MEMORY;
Redis
Example:
SET user:1 "Alex" GET user:1
or
HSET user:1 name Alex age 25
For a simple key-based read:
In other words, Redis is almost always faster for operations of the «get value by key» kind.
Therefore they are not so much competitors as complements to each other: in real-world systems MySQL is often used as the primary data store, while Redis — as a high-performance cache or a database for temporary data.
Comments