Advanced applications of Redis-transaction processing, persistence, message publishing and subscription, virtual memory usage, and redis Transaction Processing

Source: Internet
Author: User
Tags redis server

Advanced applications of Redis-transaction processing, persistence, message publishing and subscription, virtual memory usage, and redis Transaction Processing

Iii. Transaction Processing

Redis transaction processing is relatively simple. You can only ensure that the commands in the transaction initiated by the client can be executed continuously without inserting other client commands. When a client issues multi commands in the connection, the connection enters the context of a transaction. Subsequent commands of the connection are not executed, but stored in a queue. When the exec command is executed, redis will execute all the commands in the queue in sequence. If an error occurs during execution, the correct execution will not be rolled back, but the transaction is different from that of the relational database.

The age value has changed after the rollback.

Cancels a transaction with discard, cancels the transaction queue, and all data is rolled back.

Redis optimistic lock: watch is used to monitor the specified key. When exec, if the monitored key changes from the watch call, the entire transaction will fail. You can call watch to monitor multiple keys multiple times. Watch and transactions are valid for the entire connection. If the connection is disconnected, the monitoring and transactions are cleared (exec, discard, and unwatch are no exception ).


Iv. Persistence Mechanism

Redis is a persistent memory database. Redis often needs to synchronize data in memory to the disk to ensure persistence.

Redis supports two persistence methods:

1. snapshotting stores data in a file. The default method is.

Is to write the data in the memory to the binary file by taking snapshots. By default, the file dump. rdb can be configured to enable automatic snapshot persistence. Configurable Redis in n seconds if more than m

When the key is modified, the snapshot is automatically saved.


Save 900 1 # if more than one key is modified within 900 seconds, the snapshot is saved.

Save 300 10 # if more than 10 keys are modified within 300 seconds, the snapshot is saved

Save 60 10000 # If more than 10000 data records are modified within 60 seconds, the snapshot is saved.


2. Append-only file (aof), which stores read/write operations in files.

Because the snapshot method is performed once at a certain interval, if Redis is accidentally down, all modifications after the last snapshot will be lost.

Aof is more persistent than snapshot, because when aof is used, redis will store every write command received in the file through the write function. When redis Qidong City will save it by executing the file again

To re-create the entire database content in the memory.

As the OS caches write modifications in the kernel, it may not be immediately written to the disk. In this way, the aof method may still lose part of the data.

You can use the configuration file to tell redis when we want to force the OS to be written to the disk through the fsync function.


Appendonly yes # enable aof persistence

# Appendfsync always // writes data to the disk immediately after receiving the write command, which is the slowest, but ensures full data persistence.

Appendfsync everysec // writes data to the disk once per second, which compromises performance and persistence.

# Appendfsync no // fully dependent on OS, with the best performance and no guarantee of persistence


After it is enabled, two more files are stored in the bin folder, as shown below:


V. Publishing and subscription information

Pub/sub is a message communication mode. It aims to remove the coupling between message publishers and subscribers. Redis is used as a pub/sub server, the message routing function is provided between the subscriber and the publisher. The subscriber subscribes to the Message Type of interest to redis through the subscribe and psubscribe commands. redis converts the information type into a channel ). When the publisher sends a specific type of information to the redis server by running the publish command, all clients that subscribe to this type of message will receive this message.

For example, to enable three clients, one is to publish messages and the other two are to subscribe to messages, as shown below:


Vi. Use of virtual memory

Redis's virtual memory is temporary. The infrequently accessed data is exchanged from memory to disk, freeing up the memory space for other data access, especially for memory databases such as redis, memory is always insufficient. In addition to separating multiple redis servers, the method to increase the database capacity is to use virtual memory to exchange infrequently accessed data to the disk.

Configure redis. config related to the vm:

Vm-enable yes # enable the vm Function

Vm-swap-file/tmp/redis. swap # path of the file for storing the exchanged values

Vm-max-memory 10000000 # redis maximum memory available

Vm-page-size 32 # size of each page 32 bytes

Vm-pages 123217729 # use up to several small pages

Vm-max-threads 4 # Number of worker threads used to execute value object swapping

The Virtual Memory Management version 2.6 and above is canceled.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.