Skip to content

Conversation

@nikhilo
Copy link

@nikhilo nikhilo commented Mar 24, 2025

I've implemented support for routing read operations to read replicas. Here's what the changes do:

  1. Added new types and definitions:
  • Added role field to the node record to track master/replica status
  • Added redis_command_type() type for read/write/admin operations
  • Added READ_COMMANDS macro with a comprehensive list of read-only Redis commands
  1. Added replica node tracking:
  • Added get_replicas_for_slot/2 to find replica nodes for a given slot
  • Added parse_node_role/1 to identify node roles from cluster output
  • Added update_node_role/2 to update node roles in the slots map
  1. Modified command routing:
  • Added is_read_command/1 to identify read operations
  • Added get_pool_for_command/3 to route commands based on operation type
  • Modified split_by_pools/5 to use the new routing logic
  1. Updated cluster node parsing:
  • Modified parse_cluster_nodes/2 to properly set node roles when parsing cluster output

The new behavior works as follows:

  1. For read operations (commands in READ_COMMANDS):
  • First tries to find a replica node for the slot
  • If replicas are available, routes the command to a replica
  • If no replicas are available, falls back to the master node
  1. For write operations (all other commands):
  • Always routes to the master node for that slot

This implementation:

  • Automatically identifies read operations
  • Load balances read operations across available replicas
  • Maintains write operations on master nodes
  • Gracefully falls back to master nodes when replicas are unavailable
  • Preserves all existing functionality for write operations
  • The changes are backward compatible and should work seamlessly with existing Redis cluster configurations.

This code was generated using Cursor

Nikhil Owalekar added 3 commits March 26, 2025 16:26
- Add export for get_replicas_for_slot/1 and get_replicas_for_slot/2
- Fix type specs to use term() instead of #state{}
- Fix undefined function error in read replica routing

This fixes the undefined function error when trying to route read
operations to replica nodes.
@nikhilo nikhilo force-pushed the nikhil/use-read-replicas branch from 17ef6cf to 28c0261 Compare March 26, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant