Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the questions of Redis in big data's interview?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the Redis questions in big data's interview". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Redis memory dependent

There are 2000w data in MySQL and only 20w data in redis. How to ensure that the data in redis are all hot data?

When the redis in-memory dataset size rises to a certain size, the data elimination strategy is implemented.

What are the memory elimination strategies of Redis?

Redis's memory elimination strategy refers to how to deal with data that requires new writes and requires additional space when Redis runs out of memory for caching.

Selective removal of global key space

Noeviction: when there is not enough memory to hold new write data, the new write operation reports an error.

Allkeys-lru: when there is not enough memory to hold newly written data, remove the least recently used key in the key space. (this is the most commonly used)

Allkeys-random: when there is not enough memory to hold newly written data, a key is randomly removed in the key space.

Selective removal of key space for setting expiration time

Volatile-lru: when there is not enough memory to hold newly written data, remove the least recently used key from the key space where the expiration time is set.

Volatile-random: when there is not enough memory to hold newly written data, a key is randomly removed from the key space where the expiration time is set.

Volatile-ttl: when there is not enough memory to hold newly written data, key with an earlier expiration time is removed first in the key space where the expiration time is set.

Summary

The selection of memory elimination strategy for Redis does not affect the processing of expired key. The memory obsolescence policy is used to deal with data that requires additional space when there is insufficient memory, and the expiration policy is used to deal with expired cached data.

What are the main physical resources consumed by Redis?

Memory.

What happens when Redis runs out of memory?

If the upper limit is reached, Redis's write command returns an error message (but the read command returns normally. Or you can configure the memory elimination mechanism so that the old content will be washed out when the Redis reaches the memory upper limit.

How does Redis optimize memory?

You can make good use of collection type data such as Hash,list,sorted set,set, because usually many small Key-Value can be stored together in a more compact way. Use a hash table (hashes) as much as possible. A hash table (that is, a small number stored in a hash table) uses very little memory, so you should abstract your data model into a hash table as much as possible. For example, if you have a user object in your web system, do not set a separate key for the user's name, last name, mailbox, and password. Instead, store all the user's information in a hash table.

two。 Thread model

Redis thread model

Redis developed a network event handler based on the Reactor pattern, which is called a file event handler (file event handler). It consists of four parts: multiple sockets, IO multiplexer, file event dispatcher and event handler. Redis is called a single-threaded model because the consumption of the file event dispatcher queue is single-threaded.

The file event handler uses the Iram O multiplexing (multiplexing) program to listen to multiple sockets at the same time and correlate different event handlers for the socket according to the tasks currently performed by the socket.

When the monitored socket is ready to perform operations such as connection reply (accept), read (read), write (write), close, and so on, the file event corresponding to the operation will be generated, and the file event handler will call the event handler associated before the socket to handle these events.

Although the file event processor runs in a single-thread mode, by using the Imax O multiplexer to monitor multiple sockets, the file event processor not only implements a high-performance network communication model, but also interfaces well with other modules in the redis server that also run in a single-thread mode, which keeps the simplicity of the single-thread design within Redis.

3. Business

What is a transaction?

A transaction is a separate isolation operation: all commands in the transaction are serialized and executed sequentially. In the course of execution, the transaction will not be interrupted by command requests sent by other clients.

A transaction is an atomic operation: either all or none of the commands in the transaction are executed.

The concept of Redis transaction

The essence of a Redis transaction is a collection of commands such as MULTI, EXEC, WATCH, and so on. Transactions support the execution of multiple commands at a time, and all commands in a transaction are serialized. During transaction execution, commands in the queue are serialized sequentially, and command requests submitted by other clients are not inserted into the transaction execution command sequence.

To sum up: a redis transaction is an one-time, sequential, exclusive execution of a series of commands in a queue.

The three phases of a Redis transaction

1. Transaction start MULTI

two。 Order to join the team

3. Transaction execution EXEC

During the execution of a transaction, if the server receives a request other than EXEC, DISCARD, WATCH or MULTI, it will queue the request in a queue.

Redis transaction related commands

Redis transaction function is realized by four primitives: MULTI, EXEC, DISCARD and WATCH.

Redis serializes all commands in a transaction and then executes them sequentially.

Redis does not support rollback, "Redis does not roll back when the transaction fails, but continues to execute the remaining commands", so the interior of Redis can be kept simple and fast.

If an error occurs in a command in a transaction, all commands will not be executed

If a run error occurs in a transaction, the correct command is executed.

The WATCH command is an optimistic lock that provides check-and-set (CAS) behavior for Redis transactions. One or more keys can be monitored, and once one of the keys is modified (or deleted), subsequent transactions are not executed and monitoring continues until the EXEC command.

The MULTI command is used to open a transaction, which always returns OK. After the MULTI is executed, the client can continue to send as many commands as possible to the server. These commands are not executed immediately, but are placed in a queue. When the EXEC command is called, the commands in all queues are executed.

EXEC: executes commands within all transaction blocks. Returns the return values of all commands within the transaction block, in the order in which the commands were executed. Returns a null value of nil when the operation is interrupted.

By calling DISCARD, the client can empty the transaction queue and abandon the execution of the transaction, and the client will exit from the transaction state.

The UNWATCH command cancels watch's monitoring of all key.

Overview of transaction Management (ACID)

Atomicity (Atomicity): atomicity means that a transaction is an indivisible unit of work, and either all or none of the operations in the transaction occur.

Consistency: the integrity of the data must be consistent before and after the transaction.

Isolation: when multiple transactions are executed concurrently, the execution of one transaction should not affect the execution of other transactions

Durability: persistence means that once a transaction is committed, its changes to the data in the database are permanent, and then even if the database fails, it should not have any effect on it.

Redis transactions always have the same consistency and isolation as in ACID, and other features are not supported. Transactions also have durability when the server is running in AOF persistence mode and the value of the appendfsync option is always.

Does Redis transaction support isolation?

Redis is a single-process program, and it ensures that when a transaction is executed, the transaction will not be interrupted, and the transaction can run until all commands in the transaction queue have been executed. Therefore, Redis transactions are always isolated.

Does Redis transaction guarantee atomicity? does it support rollback?

In Redis, a single command is executed atomically, but the transaction is not guaranteed atomicity and there is no rollback. The execution of any command in the transaction fails, and the remaining commands will still be executed.

Other implementations of Redis transaction

Based on Lua scripts, Redis ensures that commands within the script are executed one time and sequentially

At the same time, it does not provide the rollback of transaction running errors, and if some commands run incorrectly during execution, the rest of the commands will continue to run.

Based on the intermediate marker variable, another marker variable is used to identify whether the transaction execution is completed. When reading the data, the tag variable is read first to determine whether the transaction execution is completed. But this will require extra code to implement, which is more tedious.

This is the end of the content of "what are the Redis questions in big data's interview". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report