Frequently Asked Questions about Redis

How is Redis different from other keystores?

Redis has different evolutionary paths in key-value databases, where values can contain more complex data types, and where atomic operations are defined on these data types.Redis data types are closely related to the underlying data structure and are directly available to programmers, without the need for an additional abstraction layer.

 

Redis is an in-memory database but at the same time the data also exists in a disk database, so it represents a different kind of compromise, achieving very high write and read speeds within the constraints of a data set that is not too large for in-memory. Another advantage of an in-memory database is that the in-memory representation of a heterogeneous data structure is much easier to manipulate than the same data structure on disk, so Redis can do a lot with very little internal heterogeneity. Also, the two disk storage formats (RDB and AOF) do not need to be suitable for random access, so they are compact and always generated append-only (even the AOF log rotation is an append-only method, since the new version is generated from a copy of the data in the memory). However, this design also involves different challenges compared to traditional disk storage. As the primary data representation in the memory, Redis operations must be handled carefully to ensure that there is always an updated version of the dataset on disk.

What is the memory footprint of Redis?

A few examples (the following are all based on 64-bit systems):

 

  • An empty instance takes up about 3MB of memory.
  • One million key->string-value pairs take up about 85MB of memory.
  • One million key->hash values, representing an object with 5 fields, occupies about 160MB of memory.

The size of the test case is trivial. We can use the redis-benchmark tool to generate random datasets and then check the space used with the INFO memory command.

 

A 64-bit system will use more memory than a 32-bit system to store the same key, especially if the key and value are small. This is because the pointer requires 8 bytes in a 64-bit system. The advantage is that you can have a lot of memory on a 64-bit system, so in order to run large Redis servers, a 64-bit system is more or less necessary.

Why does Redis keep its entire dataset in memory?

In the past, Redis developers have experimented with virtual memory and other systems to allow the use of data sets larger than RAM, and we expect to do a good job of "memory for data, disks for storage". So there are no plans to create a disk backend for Redis. After all, most of Redis' functionality is a direct result of its current design.

 

If your real problem is not the total memory required, but rather that you need to split your dataset into multiple Redis instances, read the documentation in theSections, for more information.

 

Redis Ltd., a sponsor of Redis, has developed a solution called "Redis on Flash" that uses a hybrid RAM/flash approach to handle large datasets with biased access patterns. You can check out their product for more information, but this feature is not part of the Redis open source library.

Can I use Redis with a disk-based database?

The answer is yes, a common design pattern is to get small pieces of data with a large number of writes in Redis (as well as those pieces of data that you need the Redis data structure to model your problem in an efficient way), and to put large chunks of the data into SQL or ultimately consistent disk databases. Similarly, sometimes Redis is used to get another copy of the same subset of data stored in a disk database in internal memory. This may seem similar to caching, but it is actually a more advanced model, as usually the Redis data set is updated along with the data set in the disk database.

How to reduce the overall memory usage of Redis?

Use a Redis 32-bit instance if you can. Also make full use of small hashes, lists, sorted sets, and integer sets, as Redis is able to represent these data types in a more compact way in special cases that contain a small number of elements. Alternatively, you can use theMemory Optimization PageFor more information, please visit the website of the Hong Kong Institute of Certified Public Accountants (HKICPA).

What happens if Redis runs out of memory?

Redis has a built-in protection feature that allows users to set a maximum limit on memory usage, using the options in the maxmemory configuration file to set the maximum amount of memory Redis can use. If this limit is reached, Redis will start replying with an error for written commands (but will continue to accept read-only commands).

 

You can also configure Redis to evict keys when the maximum memory limit is reached.

Backend save failed with fork() error on Linux?

The Redis backend's save model relies on the copy-on-write semantics of the fork call in modern operating systems: Redis forks (which create children) are exact copies of the parent process. The child process dumps the DB to disk and then exits. In theory, the child should use as much memory as the parent, but in practice, since most modern operating systems implement copy-on-write semantics, the parent and child will share common memory pages. A page is only overwritten if it is changed in a child or parent process. Since all pages could theoretically change while the child is saving, Linux cannot know in advance how much memory the child will take up, so if you set overcommit_memory to 0, the fork will fail unless you have enough RAM available to actually override all the parent memory pages. That is, if you have a 3 GB Redis dataset and only 2 GB of memory available, the fork will fail.

 

Setting overmit\u memory to 1 will allow Linux to run the fork in a more optimized way, which is really what you want for Redis.

To learn how Linux virtual memory works and other alternatives to overcommit_memory and overcommit_ratio, you can refer to the Red Hat Magazine classic articleUnderstanding Virtual MemoryYou can also refer to the manual page for an explanation of the available values. You can also refer to the manual page for an explanation of the available values.

How does Redis use multiple CPUs or cores?

Redis is usually limited by memory or network constraints, and it is not uncommon for CPU to be a bottleneck. For example, a Redis instance running on a normal Linux system can send a million requests per second when pipelined, so if your application uses mostly O(N) or O(log(N)) commands, it hardly uses much CPU.

 

However, to maximize CPU utilization, you can start multiple Redis instances in a single box and treat them as different servers. In some cases, a single box may not be enough, so if you want to use multiple CPUs, start thinking about slicing and dicing earlier.

 

Since version 4.0, Redis has been implementing threaded operations. However, this is currently limited to deleting objects in the background and blocking commands implemented through Redis modules. For future releases, the plan is to make Redis more and more threaded.

What is the maximum number of keys a single Redis instance can hold? What is the maximum number of elements in hashes, lists, sets, and sorted collections?

Redis can handle up to 2^32 keys, and has been tested in practice to handle at least 250 million keys per instance.

 

Each hash, list, set and sorted set can hold 2^32 elements.

 

In other words, your maximum limit may be the available memory on your system.

Why does my copy have a different number of keys than the main instance?

If you are using a key with a limited validity (Redis expiration), this is normal. This is because:

 

  • The primary generates an RDB file the first time it synchronizes with the replica.
  • RDB files do not contain keys that have expired in The primary but still exist in memory.
  • These keys remain in the memory of the Redis primary even if they are logically expired. Their memory will later be reclaimed on access, either incrementally or explicitly. Although these keys are not logically part of the dataset, they are counted in the INFO output and the DBSIZE command.
  • This set of keys will not be loaded when the replica reads RDB files generated by the primary database, The primary.

 

Therefore, users with many expired keys will usually see fewer keys in the replica. However, logically, the primary and the replica will have the same content.

Contact Hongke to help you solve your problems.

Let's have a chat