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

Summary of some of the most common interview questions in Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Preface

After a week-long rush and interviews, telephone interviews, looking back on today's successful entry, a total of about 10 companies were interviewed, including Ali, JD.com, IBM and so on. JD.com passed the technology, and his education was pass because of non-uniform recruitment. Ah, after two phone interviews, I guess I lost points when I finally asked questions at that time. I also had some offer, either I didn't want to go, or I didn't hear from him. Seeing that the New year was approaching, it was not up to me to pick and choose, so I went directly into my current company. I mainly felt that the company was good, and I didn't care too much about salary. All right, the book comes to the text. Today, the editor sends you the interview questions about Redis that I have prepared meticulously. I hope it can help you who are still looking for a job.

1. What is redis?

Redis is a memory-based high-performance key-value database.

Characteristics of 2.Reids

Redis is essentially an in-memory database of Key-Value type, much like memcached, the whole database is loaded and operated in memory, and the database data is saved to the hard disk by asynchronous operation periodically. Because of its pure memory operation, Redis has excellent performance, which can handle more than 100000 read and write operations per second.

The fastest Key-Value DB.

The excellence of Redis is not only its performance, but the greatest charm of Redis is that it supports the preservation of multiple data structures. in addition, the maximum limitation of a single value is 1GB, unlike memcached, which can only save 1MB data, so Redis can be used to achieve many useful functions, such as using its List to do a FIFO two-way linked list to achieve a lightweight high performance cancellation.

Information queuing service, using his Set can do high-performance tag system and so on. In addition, Redis can also set the expire time for the stored Key-Value, so it can also be used as an enhanced version of memcached.

The main disadvantage of Redis is that the database capacity is limited by physical memory and can not be used for high-performance read and write of massive data, so the suitable scenarios of Redis are mainly limited to high-performance operations and operations with a small amount of data.

3. What are the benefits of using redis?

(1) it is fast, because the data is stored in memory, and the advantage similar to HashMap,HashMap is that the time complexity of search and operation is O (1).

(2) supports rich data types and string,list,set,sorted set,hash

(3) transactions are supported, and all operations are atomic. the so-called atomicity means that changes to the data are either performed or not performed at all.

(4) rich features: can be used for caching, messages, set expiration time by key, and will be deleted automatically after expiration

4. What are the disadvantages of using redis

Analysis: we have used redis for so long, this problem must be understood, basically using redis will encounter some problems, common only a few.

Answer: there are four main questions

(1) consistency of double writes between cache and database

(2) the problem of cache avalanche

(3) the problem of cache breakdown

(4) concurrency competition of cache

Personally, I think these four problems are more often encountered in the project.

What are the advantages of 5.redis over memcached?

(1) all values of memcached are simple strings, and redis, as its replacement, supports richer data types.

(2) redis is much faster than memcached. (3) redis can persist its data.

What are the differences between 6.Memcache and Redis?

1) Storage method Memecache stores all the data in memory, and will hang up when the power is off, and the data cannot exceed the memory size. Some of the Redis is stored on the hard disk, which ensures the persistence of the data.

2) data support type Memcache is relatively simple to support data types. Redis has complex data types.

3) using the underlying model, the underlying implementation between them and the application protocol for communicating with the client are different. Redis directly builds its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.

Common performance problems and solutions for 7.redis:

1) .Master writes memory snapshots, and the save command dispatches the rdbSave function, which will block the work of the main thread. When the snapshot is relatively large, it will have a great impact on performance and will intermittently suspend the service. Therefore, it is best not to write memory snapshots for Master.

2). Master AOF persistence, if you do not rewrite AOF files, this persistence method will have the least impact on performance, but AOF files will continue to grow, and excessive AOF files will affect the recovery speed of Master restart. It is best for Master not to do any persistence work, including memory snapshots and AOF log files. In particular, do not enable memory snapshots for persistence. If the data is critical, a Slave enables AOF to back up data. The policy is to synchronize once a second.

3) when the .Master calls BGREWRITEAOF to rewrite the AOF file, AOF will take up a lot of CPU and memory resources when rewriting, resulting in high service load and temporary service suspension.

4)。 Due to the performance problem of Redis master-slave replication, for the speed of master-slave replication and the stability of connection, Slave and Master should be in the same local area network.

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

Related knowledge: when the redis in-memory dataset size rises to a certain size, the data phase-out strategy (recovery strategy) will be implemented. Redis provides 6 data elimination strategies:

Volatile-lru: select the least used data to phase out volatile-ttl from datasets that have set expiration time (server.db[ I] .expires): select data that will expire from datasets that have set expiration time (server.db[ I] .expires) to phase out volatile-random: select data to phase out allkeys-lru from datasets with expiration time set (server.dbI] .expires: eliminate allkeys-lru from data. Select the least recently used data from the set (server.db [I] .dict) to phase out allkeys-random: eliminate no-enviction from any selection of dataset (server.db [I] .dict): banning the expulsion of data

9. Please use Redis and any language to implement a malicious login protection code, limiting each user's Id to a maximum of 5 logins in an hour. Specific login functions or functions can be used with empty functions, and do not need to be written out in detail.

List implementation: each element in the list represents the landing time, as long as the last fifth login time and the current time difference is not more than 1 hour. The code written in Python is as follows:

#! / usr/bin/env python3import redis import sys import time r = redis.StrictRedis (host='127.0.0.1', port=6379, db=0) try: id = sys.argv [1] except: print ('input argument error') sys.exit (0) if r.llen (id) > = 5 and time.time ()-float (r.lindex (id, 4))

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

Database

Wechat

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

12
Report