Redis_transaction_lock mechanism_seckill

10. Redis_transaction_lock mechanism_second kill _

10.1 Redis transaction definition

A Redis transaction is a single isolated operation: all commands in the transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.

The main function of Redis transactions is to concatenate multiple commands to prevent other commands from jumping in the queue.

10.2 MultiExecdiscard

From the input of the Multi command, the input commands will enter the command queue in sequence, but will not be executed until the Exec is entered, and Redis will execute the commands in the previous command queue in sequence.

In the process of forming a team, you can give up the team by discarding.  

127.0.0.1:6379> multi
OK
127.0.0.1:6379(TX)> set key1 value1
QUEUED
127.0.0.1:6379(TX)> set key2 value2
QUEUED
127.0.0.1:6379(TX)> exec
1) OK
2) OK
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
127.0.0.1:6379> 

Successful team formation, successful submission


10.3  Transaction Error Handling

If a command in the team reports an error, all queues will be canceled during execution.

 If a command reports an error during the execution phase, only the command that reported the error will not be executed, while other commands will be executed without rolling back.

10.4  Why do transactions

Think about a scenario: There are many people who have your account and go to participate in the Double Eleven snap-up at the same time

10.5  The Problem of Transaction Conflicts

10.5.1 Examples

A request wants to subtract 8000 from the amount

A request wants to subtract 5000 from the amount

A request wants to subtract 1000 from the amount

10.5.2 Pessimistic locking

Pessimistic Lock (Pessimistic Lock) , as the name suggests, is very pessimistic. Every time you go to get the data, you think that others will modify it, so every time you get the data, you will lock it, so that others will block until it gets the data. to the lock. Many such locking mechanisms are used in traditional relational databases , such as row locks , table locks , etc., read locks , write locks, etc., all of which are locked

10.5.3  Optimistic locking 

Optimistic Lock (Optimistic Lock) , as the name suggests, is very optimistic. Every time I go to get the data, I think that others will not modify it, so it will not be locked, but when updating, I will judge whether others have updated it during this period. Data, you can use mechanisms such as version numbers. Optimistic locking is suitable for multi-read application types, which can improve throughput . Redis uses thischeck-and-setmechanism to implement transactions. 

10.5.4 WATCH key [key ...]

Before executing multi, first execute watch key1 [key2], you can monitor one (or more) key, if the key (or these) is changed by other commands before the transaction is executed, the transaction will be interrupted.

10.5.5 unwatch

Cancel  the monitoring of all keys by the WATCH command.

If the EXEC  command or the DISCARD  command is executed first  after executing the WATCH command, then there is no need to execute UNWATCH again  .

EXEC — Redis Command Reference

10.6  Three characteristics of Redis transaction

  • separate isolation operation
    1. All commands in a transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.
  • No concept of isolation level
    1. The commands in the queue will not be actually executed until they are committed, because any instructions will not be actually executed until the transaction is committed
  • atomicity not guaranteed
    1. If a command fails to execute in the transaction, subsequent commands will still be executed without rollback

11.  Redis_transaction_seckill case _

11.1  Solve transaction operations of counters and personnel records

11.2  Redis Transaction -- Second Kill Concurrency Simulation 

Use the tool ab to simulate the test

CentOS6 default installation

CentOS7 needs manual installation


12. RDB for Redis persistence

12.1  General introduction

Official website introduction: http://www.redis.io

Redis provides two different forms of persistence.

        RDB(Redis DataBase)

        AOF(Append Of File)

12.2 RDBRedis DataBase

12.2.1  Official Website Introduction

 

12.2.2  What is it 

Write the snapshot of the data set in the memory to the disk within the specified time interval , which is the Snapshot snapshot in jargon. It reads the snapshot file directly into the memory when it is restored.

12.2.3  How backup is performed

Redis will create (fork) a child process separately for persistence, and will first write the data into a temporary file , and after the persistence process is over, then use this temporary file to replace the last persisted file. During the whole process, the main process does not perform any IO operations, which ensures extremely high performance. If large-scale data recovery is required and the integrity of data recovery is not very sensitive, the RDB method is more efficient than the AOF method. efficient. The disadvantage of RDB is that the data after the last persistence may be lost .

12.2.4 Fork

  1. The function of Fork is to copy a process that is the same as the current process . All data (variables, environment variables, program counters, etc.) of the new process have the same value as the original process, but it is a brand new process and is used as a child process of the original process
  2. In a Linux program, fork() will generate a child process that is exactly the same as the parent process, but the child process will often call the exec system afterwards. For efficiency reasons, Linux introduces " copy-on-write technology "
  3. In general, the parent process and the child process will share the same physical memory . Only when the content of each segment of the process space changes, will the content of the parent process be copied to the child process.

12.2.5  RDB persistence process

 12.2.6  dump.rdb file

Configure the file name in redis.conf, the default is dump.rdb

12.2.7  Configuration location 

The save path of the rdb file can also be modified. The default is the directory where the command line is located when Redis starts

dir "/myredis/"

 12.2.8  How to trigger RDB snapshot; keep policy

12.2.8.1 Default snapshot configuration in the configuration file

 12.2.8.2 command save VS bgsave

save : Just save when saving, and block everything else. Save manually. Not recommended.

bgsave: Redis will perform snapshot operations asynchronously in the background, and snapshots can also respond to client requests.

The time of the last successful snapshot execution can be obtained through the lastsave command

 

Guess you like

Origin blog.csdn.net/fgwynagi/article/details/130022529