What is a redis

Redis is an open source (BSD-licensed) store of in-memory data structures used as databases, caches, and message brokers. It supports data structures such as strings, hashes, lists, collections, sorted sets with range queries, bitmaps, super logs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU expulsion, transactions and different levels of disk persistence, and provides high availability through Redis Sentinel and automatic partitioning using Redis Cluster.

Redis is an open source key-value database written in C language.

Redis features

  • Excellent read/write performance
  • persistence
  • Data type richness
  • Single thread
  • Automatic data expiration
  • Release subscription
  • distributed

What are the benefits of using Redis?

  1. Fast, because the data is in memory, similar to a HashMap, which has the advantage of having O(1) time for both lookup and operation.

  2. Support rich data types, support string, list, set, sorted set, hash

  3. Transactions are supported, and operations are atomic, meaning that changes to data are either all or none

  4. Rich features: can be used for cache, message, by key set expiration time, will be automatically deleted after expiration

What can redis be used for

1. The cache

Cache is the most common application scenario of Redis, mainly because of its excellent read and write performance. Redis can process more than 100,000 read and write operations per second, making it the fastest key-value DB. Redis can store multiple data structures, and a single value is limited to 1GB. Memcached can store only 1MB of data.

Memcached is also a cached database. Click to see the difference.

2. Distributed lock

Because of the single-threaded nature of Redis, an important application scenario is distributed locking. For high-concurrency systems, which are deployed with multiple servers, distributed locks can be used to limit the concurrency of programs for logical processing.

3. Automatically expires

Redis can set expiration time for all data and automatically clean up expired data. Common application scenarios include SMS verification codes and product display with time, such as event start and deadline

4. Counters and leaderboards

Redis has good support for incrementing or decrementing numbers in memory. Set and Sorted Set also make performing these ranking operations very simple.

5. Second kill buffer layer

Seckill is a common marketing mode in the Internet system, but it usually brings great difficulties to the development due to the high concurrency, program blocking and other reasons. Redis can be used as a cache platform to control concurrency by using the single-thread feature of Redis. Because of its strong reading and writing ability, redis is not easy to produce program blocking.

6. Handle check-in and other states (big data processing)

When the number of users is large, how to count the status of users in a very short time, such as QQ punch, check the status of friends and so on. Bitmaps in Redis can solve this problem well.

Publish/subscribe

Redis can be used to publish subscriptions to achieve social chat system.