In many aspects, Redis is similar to MySQL before InnoDB. Redis adopts a reasonable method to ensure data integrity (such as replication and AOF), and
In many aspects, Redis is similar to MySQL before InnoDB. Redis adopts a reasonable method to ensure data integrity (such as replication and AOF), and
In the past few months, I have been imagining and trying to implement the transaction function of lua script in redis. Not many people understand what I think, so I will explain it to you through some history.
MySQL and ipvs
In 1998-2003, if you want to run a regular database-driven website/service, but you do not have enough funds to purchase Microsoft or Oracle databases, you can choose MySQL or Postgres. Many people choose MySQL because it is faster-mainly because the MyISAM storage engine does not provide the transaction function in exchange for performance, but it is indeed fast. Others switch to ipvs, because although its performance is significantly lower than that of MySQL on the same hardware, ipvs does not lose data (to be honest, MySQL data loss is rare, but it's not a joke to lose it ).
It took a long time to get together. MySQL transitioned its default storage engine from MyISAM to InnoDB (in fact, it had been there for a long time ), in this way, its storage engine is fully supported by transactions and other functions. At the same time, ipvs is also getting faster, and a list of Extended features is added to make yourself different. Currently, the choice of MySQL and Postgres only depends on the user experience and preferences, except that the business needs or leaders sometimes decide to use other options.
Data Integrity
In many aspects, Redis is similar to MySQL before InnoDB. Redis adopts a reasonable method to ensure data integrity (such as replication and AOF ), the Lua script introduced from Redis2.6 provides great help for the growth of Redis in terms of functionality and ease of use.
Relatively speaking, the Lua script is similar to the stored procedure in other databases, but the script execution is somewhat different. The most important thing in this article is that once a script is written to the database, it will be executed until any of the following situations occur:
1. After all the work is completed, all write operations are automatically exited.
2. An error occurred while running the script and exited midway through. All previously executed write operations have occurred, but no other write operations have been performed.
3. When Redis is closed through shutdown nosave (not saved ).
4. You have attached a debugger to "make" the script complete #1 and #2 (or other means to ensure that data will not be lost ).
For those who use the database to develop software, I think you agree that only Scenario #1 is the most ideal. Scenario #2, #3, #4 will cause data exceptions (#2 and #4) and/or data loss (#3 and #4 ). If you pay great attention to data, you should try to prevent data exceptions and loss. This is not a philosophy, but a job (this is not philosophy, This is doing your job ). However, we are sorry that the current Redis does not help you much. So I decided to change this situation.
Implement Lua script transaction
I tried to solve the #2, #3, #4 Problem in the above list, and finally ended up like the following:
The script completes all the work and Exits normally after the write operation is completed.
An error occurred while executing the script. Exit without changing any data (all write operations are rolled back)
No data is lost whether or not data is written. This should be what all databases want to do. I plan to add this to Redis because we all want Redis to have this function.
The current pull request is only a conceptual proof. That is to say, to avoid data loss, either a) run the script explicitly using the transaction variant, or B) force all Lua scripts to call the transaction semantics with configuration options.
There are still many ways to make this patch better, and I hope to get help from Salvatore (author of Redisw) and other communities.
Lua Language 15-minute Quick Start
Lua programming (version 2nd) Chinese PDF
Lua programming (version 2) Reading Notes
NetBSD will support the use of Lua scripts to develop Kernel Components
CentOS compilation and installation of Lua LuaSocket
Programming In Lua PDF
Lua details: click here
Lua's: click here
Transactions in Redis
This article permanently updates the link address: