Java手写Redis事务管理:带你掌握缓存事务的内部秘密_Redis
Redis事务提供了一种将多个命令打包执行的能力,确保这些命令要么全部成功执行,要么在出现错误时全部不做。这种机制对于需要保证操作原子性的场景非常有用,如金融交易、库存扣减等。Redis事务的设计背景是为了在内存数据库中实现类似于传统关系型数据库的事务特性,同时保持Redis的高性能和简单性。然而,与关系型数据库不同的是,Redis事务不支持回滚操作,如果在事务中的某个命令失败,整个事务将被放弃,但不会撤销已经执行的命令。这种设计简化了系统的复杂性,并利用Redis的单线程特性来避免锁的竞争,从而实现快速的数据处理。

肖哥弹架构 跟大家“弹弹” 代码设计技巧,需要代码关注

欢迎 点赞,点赞,点赞。

关注公号Solomon肖哥弹架构获取更多精彩内容

历史热点文章

1、Redis的事务特定
  1. 性能:Redis是一个基于内存的数据库,其设计目标之一是提供极高的性能。使用单线程模型可以避免多线程并发执行时的锁竞争和上下文切换开销,从而实现快速的操作处理。
  2. 简单性:Redis的事务模型相对简单,它通过队列来收集多个命令,并在EXEC命令时一次性执行。这种设计简化了内部实现,并使得命令执行顺序清晰,易于理解和维护。
  3. 一致性:虽然Redis的事务不支持内部回滚,但它确保了在没有错误的情况下,事务中的所有命令都将被顺序执行,从而保证了操作的一致性。
  4. 隔离性:由于Redis是单线程执行命令的,它天然具有隔离性。在执行事务的过程中,不会有其他命令插入执行,这简化了并发控制的复杂性。
  5. 可靠性:Redis的事务设计允许在执行过程中不进行回滚,这减少了因为事务失败而导致的数据不一致的风险。如果事务中的某个命令失败,Redis可以简单地记录错误并继续执行后续命令。
  6. 使用场景:Redis通常用于缓存、消息队列、排行榜等场景,这些场景下对事务的原子性和隔离性要求不如传统数据库严格。Redis的事务设计更符合其主要使用场景的需求。
  7. 乐观锁:通过WATCH命令,Redis实现了一种乐观锁机制,允许在事务执行前检查数据是否被修改。这种方式适用于冲突较少的环境,可以提高性能。
  8. 持久化:Redis提供了持久化机制,但事务的持久化与事务的原子性是分开的。这意味着即使事务中的命令失败了,已经持久化的数据也不会丢失,但事务不会回滚。
2、Redis事务实现的关键特性:
  1. MULTI/EXEC:通过multi()方法开始事务,并通过exec()方法执行所有排队的命令。
  2. WATCH:通过watch(String key)方法监视一个或多个键,如果在执行exec()之前这些键被修改,则事务中止。
  3. DISCARD:通过discard()方法可以放弃当前事务,清除命令队列。
3、Java手写Reids事务功能

实现了SET、GET、DEL、WATCH、DISCARD、MUTIL、EXEC和INCR等基本Redis命令来。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisTransactionSimulator {
    private final Map<String, String> database; // Redis的内存存储
    private final List<String> commands; // 存储事务中的命令
    private boolean isDiscarded; // 标记事务是否已被丢弃
    private boolean isExecuted; // 标记事务是否已执行
    private final List<String> watchedKeys; // 存储被WATCH命令监视的键

    public RedisTransactionSimulator() {
        this.database = new HashMap<>();
        this.commands = new ArrayList<>();
        this.isDiscarded = false;
        this.isExecuted = false;
        this.watchedKeys = new ArrayList<>();
    }

    public void multi() {
        if (isExecuted || isDiscarded) {
            throw new IllegalStateException("Transaction has already been executed or discarded");
        }
        commands.clear();
        watchedKeys.clear();
    }

    public void watch(String key) {
        if (isExecuted || isDiscarded) {
            throw new IllegalStateException("Cannot watch in an executed or discarded transaction");
        }
        watchedKeys.add(key);
    }

    public void set(String key, String value) {
        addCommand("SET " + key + " " + value);
    }

    public void get(String key) {
        addCommand("GET " + key);
    }

    public void del(String key) {
        addCommand("DEL " + key);
    }

    public void incr(String key) {
        addCommand("INCR " + key);
    }

    public void discard() {
        if (isExecuted) {
            throw new IllegalStateException("Cannot discard an executed transaction");
        }
        isDiscarded = true;
        commands.clear();
    }

    public List<String> exec() {
        if (isDiscarded) {
            return null;
        }
        if (isExecuted) {
            throw new IllegalStateException("Transaction has already been executed");
        }

        List<String> results = new ArrayList<>();
        for (String command : commands) {
            String[] parts = command.split(" ");
            switch (parts[0]) {
                case "SET":
                    database.put(parts[1], parts[2]);
                    results.add("OK");
                    break;
                case "GET":
                    String value = database.get(parts[1]);
                    results.add(value != null ? value : "nil");
                    break;
                case "DEL":
                    boolean isDeleted = database.remove(parts[1]) != null;
                    results.add(isDeleted ? "1" : "0");
                    break;
                case "INCR":
                    String currentValue = database.get(parts[1]);
                    if (currentValue == null) {
                        database.put(parts[1], "1");
                    } else {
                        int newValue = Integer.parseInt(currentValue) + 1;
                        database.put(parts[1], Integer.toString(newValue));
                    }
                    results.add(database.get(parts[1]));
                    break;
                default:
                    throw new UnsupportedOperationException("Command not supported: " + parts[0]);
            }
        }

        isExecuted = true;
        return results;
    }

    private void addCommand(String command) {
        if (!isExecuted && !isDiscarded) {
            commands.add(command);
        } else {
            throw new IllegalStateException("Cannot add command to an executed or discarded transaction");
        }
    }

    public static void main(String[] args) {
        RedisTransactionSimulator simulator = new RedisTransactionSimulator();

        simulator.multi();
        simulator.watch("foo");
        simulator.set("foo", "bar");
        simulator.incr("counter"); // 假设counter初始值为0
        simulator.del("someKey");
        List<String> results = simulator.exec();

        if (results != null) {
            for (String result : results) {
                System.out.println(result);
            }
        } else {
            System.out.println("Transaction aborted due to watched key change.");
        }

        System.out.println("Final value of foo: " + simulator.get("foo"));
    }

    public String get(String key) {
        return database.get(key);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
代码说明
  1. RedisTransactionSimulator 类:Redis事务的行为。
  2. multi() 方法:开始一个新的事务,清除之前的命令和监视键。
  3. watch(String key) 方法:添加一个键到监视列表中,用于乐观锁。
  4. set(String key, String value) 方法:SET命令,将键值对添加到事务队列中。
  5. get(String key) 方法:GET命令,从事务队列中获取键的值。
  6. del(String key) 方法:DEL命令,从数据库中删除键。
  7. incr(String key) 方法:INCR命令,增加键的整数值。
  8. discard() 方法:放弃当前事务,清除命令队列。
  9. exec() 方法:执行事务中的所有命令,返回命令结果。
  10. addCommand(String command) 方法:将命令添加到事务队列中。
  11. main 方法:演示如何使用RedisTransactionSimulator类来操作Redis事务。
4、Redis事务使用业务场景
秒杀活动库存扣减
  • 业务说明:在秒杀活动中,需要确保库存扣减和订单创建是原子性的,以避免超卖现象。
  • 事务说明:保证库存扣减和订单创建操作要么同时成功,要么同时失败。
  • 无事务结果:如果库存扣减和订单创建不是原子性的,可能会出现库存扣减后订单创建失败的情况,导致库存和订单数据不一致。
Jedis jedis = new Jedis("localhost", 6379);

String productKey = "product:1:stock";
String orderKey = "order:1";

// 开始事务
Transaction transaction = jedis.multi();
transaction.decr(productKey); // 扣减库存
transaction.set(orderKey, "Order details"); // 创建订单

// 执行事务
List<Object> results = transaction.exec();

if (results != null) {
    System.out.println("秒杀成功,订单创建:" + results.get(1));
} else {
    System.out.println("秒杀失败,库存不足或订单创建失败");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.