SoFunction
Updated on 2025-04-04

Example of Redis implementing distributed transactions

Redis itself is not a relational database, and it does not support transactions with the ACID feature in traditional SQL databases. However, Redis provides a feature called "transactions" that allows clients to bundle multiple commands together as a separate sequence to send to the server. Although Redis's transactions cannot guarantee atomicity, consistency, isolation, and durability, it can be used to ensure that a set of commands are received and executed by the server as a whole.

The transaction characteristics of Redis mainly include the following commands:

  • MULTI: Mark the beginning of a transaction block.
  • EXEC: Execute commands within all transaction blocks.
  • DISCARD: Cancels execution of all commands within the transaction block.
  • WATCH: Monitors one or more keys. If the monitored key is changed by other commands before EXEC, the entire transaction will not be executed.

Here is an example of using Redis to perform simple transaction operations:

import ;
import ;

public class RedisTransactionExample {

    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;

    public static void main(String[] args) {
        Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT);

        try {
            // Start a transaction            ("product:p001");
            String stockStr = ("product:p001");

            if (stockStr != null && (stockStr) > 0) {
                (); // Mark the transaction start                ("product:p001"); // Reduce inventory                ("user:1001", "p001"); // Add purchase history                List<Object> results = (); // Execute transactions
                if (results != null && () == 2) {
                    ("Transaction executed successfully.");
                } else {
                    ("Transaction failed or was cancelled.");
                }
            } else {
                ("Not enough stock.");
            }

        } catch (NumberFormatException e) {
            ("Invalid stock value: " + stockStr);
        } catch (JedisException e) {
            ("Error while executing transaction: " + ());
        } finally {
            if (jedis != null) {
                ();
            }
        }
    }
}

In this example, we use WATCH to monitor changes in the product:p001 key and then check if the inventory is sufficient before the transaction begins. If stock is sufficient, we will start the transaction using MULTI and reduce inventory within the transaction and record the user's purchase behavior. Finally, use EXEC to execute the transaction.

Since Redis's transactions do not have ACID characteristics, it cannot completely replace traditional relational database transactions. In the case of high concurrency, race conditions or other consistency problems are still possible. If stricter transaction support is required, it may need to be implemented in combination with other technologies or mechanisms.

For more complex situations, such as the need for consistency operations across multiple keys or multiple Redis instances, it may be necessary to consider using external coordination services such as ZooKeeper or Etcd to manage distributed transactions, or designing business logic to tolerate some degree of inconsistency, such as using a final consistency model.

This is the end of this article about Redis’s example implementation of distributed transactions. For more related Redis distributed transaction content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!