0
# Clustering and High Availability
1
2
This document covers Redis Cluster support, high availability through Redis Sentinel, multi-cluster failover scenarios, and advanced deployment patterns for fault tolerance.
3
4
## Redis Cluster
5
6
### ClusterCommands
7
8
Interface for Redis Cluster management and administration commands.
9
10
```java { .api }
11
public interface ClusterCommands {
12
/**
13
* Get cluster nodes information
14
* @return Cluster nodes configuration and status
15
*/
16
String clusterNodes();
17
18
/**
19
* Get cluster information and statistics
20
* @return Cluster info including state, slots, and metrics
21
*/
22
String clusterInfo();
23
24
/**
25
* Get cluster slots mapping
26
* @return List of slot ranges and responsible nodes
27
*/
28
List<Object> clusterSlots();
29
30
/**
31
* Get keys in specific cluster slot
32
* @param slot Cluster slot number (0-16383)
33
* @param count Maximum number of keys to return
34
* @return List of keys in the slot
35
*/
36
List<String> clusterGetKeysInSlot(int slot, int count);
37
38
/**
39
* Count keys in cluster slot
40
* @param slot Cluster slot number
41
* @return Number of keys in the slot
42
*/
43
Long clusterCountKeysInSlot(int slot);
44
45
/**
46
* Get slot for key
47
* @param key Redis key
48
* @return Cluster slot number for the key
49
*/
50
Long clusterKeySlot(String key);
51
52
/**
53
* Initiate cluster failover
54
* @return Status code reply
55
*/
56
String clusterFailover();
57
58
/**
59
* Initiate cluster failover with options
60
* @param failoverOption Failover type (FORCE or TAKEOVER)
61
* @return Status code reply
62
*/
63
String clusterFailover(ClusterFailoverOption failoverOption);
64
65
/**
66
* Forget cluster node
67
* @param nodeId Node ID to forget
68
* @return Status code reply
69
*/
70
String clusterForget(String nodeId);
71
72
/**
73
* Meet new cluster node
74
* @param ip Node IP address
75
* @param port Node port
76
* @return Status code reply
77
*/
78
String clusterMeet(String ip, int port);
79
80
/**
81
* Reset cluster node
82
* @param resetType Reset type (HARD or SOFT)
83
* @return Status code reply
84
*/
85
String clusterReset(ClusterResetType resetType);
86
87
/**
88
* Save cluster configuration
89
* @return Status code reply
90
*/
91
String clusterSaveConfig();
92
93
/**
94
* Set cluster slot state
95
* @param slot Slot number
96
* @param nodeId Node ID to assign slot
97
* @return Status code reply
98
*/
99
String clusterSetSlotNode(int slot, String nodeId);
100
101
/**
102
* Set slot as migrating
103
* @param slot Slot number
104
* @param nodeId Target node ID
105
* @return Status code reply
106
*/
107
String clusterSetSlotMigrating(int slot, String nodeId);
108
109
/**
110
* Set slot as importing
111
* @param slot Slot number
112
* @param nodeId Source node ID
113
* @return Status code reply
114
*/
115
String clusterSetSlotImporting(int slot, String nodeId);
116
117
/**
118
* Clear slot state
119
* @param slot Slot number
120
* @return Status code reply
121
*/
122
String clusterSetSlotStable(int slot);
123
124
/**
125
* Replicate from master node
126
* @param nodeId Master node ID
127
* @return Status code reply
128
*/
129
String clusterReplicate(String nodeId);
130
131
/**
132
* Get cluster node ID
133
* @return Current node ID
134
*/
135
String clusterMyId();
136
}
137
```
138
139
### JedisCluster Usage
140
141
Redis Cluster client with automatic key routing and failover.
142
143
```java { .api }
144
public class JedisCluster extends UnifiedJedis {
145
/**
146
* Creates cluster client with single node discovery
147
* @param jedisClusterNode Any cluster node endpoint
148
*/
149
public JedisCluster(HostAndPort jedisClusterNode);
150
151
/**
152
* Creates cluster client with multiple discovery nodes
153
* @param clusterNodes Set of cluster node endpoints for discovery
154
*/
155
public JedisCluster(Set<HostAndPort> clusterNodes);
156
157
/**
158
* Creates cluster client with configuration
159
* @param clusterNodes Cluster discovery nodes
160
* @param config Client configuration
161
*/
162
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig config);
163
164
/**
165
* Creates cluster client with pool configuration
166
* @param clusterNodes Cluster discovery nodes
167
* @param config Client configuration
168
* @param poolConfig Pool configuration for node connections
169
*/
170
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig config,
171
JedisPoolConfig poolConfig);
172
173
/**
174
* Creates cluster client with timeout settings
175
* @param clusterNodes Cluster discovery nodes
176
* @param connectionTimeout Connection timeout in milliseconds
177
* @param soTimeout Socket timeout in milliseconds
178
*/
179
public JedisCluster(Set<HostAndPort> clusterNodes, int connectionTimeout, int soTimeout);
180
181
/**
182
* Creates cluster client with retry settings
183
* @param clusterNodes Cluster discovery nodes
184
* @param connectionTimeout Connection timeout
185
* @param soTimeout Socket timeout
186
* @param maxAttempts Maximum retry attempts for commands
187
*/
188
public JedisCluster(Set<HostAndPort> clusterNodes, int connectionTimeout,
189
int soTimeout, int maxAttempts);
190
191
/**
192
* Gets connection pool for specific cluster node
193
* @param node Cluster node endpoint
194
* @return Connection pool for the node
195
*/
196
public JedisPool getConnectionPoolFromSlot(int slot);
197
198
/**
199
* Gets all cluster node pools
200
* @return Map of node endpoints to connection pools
201
*/
202
public Map<String, JedisPool> getClusterNodes();
203
204
/**
205
* Refreshes cluster topology information
206
*/
207
public void refreshClusterSlots();
208
209
/**
210
* Gets cluster slot cache
211
* @return Current slot-to-node mapping
212
*/
213
public Map<Integer, JedisPool> getSlotPoolMap();
214
215
/**
216
* Closes cluster client and all node connections
217
*/
218
public void close();
219
}
220
```
221
222
#### Usage Example
223
224
```java
225
Set<HostAndPort> clusterNodes = new HashSet<>();
226
clusterNodes.add(new HostAndPort("redis-node1.example.com", 7000));
227
clusterNodes.add(new HostAndPort("redis-node2.example.com", 7001));
228
clusterNodes.add(new HostAndPort("redis-node3.example.com", 7002));
229
230
JedisPoolConfig poolConfig = new JedisPoolConfig();
231
poolConfig.setMaxTotal(20);
232
poolConfig.setMaxIdle(10);
233
poolConfig.setTestOnBorrow(true);
234
235
JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
236
.connectionTimeoutMillis(2000)
237
.socketTimeoutMillis(2000)
238
.password("cluster-password")
239
.build();
240
241
JedisCluster cluster = new JedisCluster(clusterNodes, clientConfig, poolConfig);
242
243
try {
244
// Use like regular Redis client - clustering is transparent
245
cluster.set("user:1000", "John Doe");
246
cluster.hset("user:1000:profile", "email", "john@example.com");
247
248
// Keys are automatically routed to correct cluster nodes
249
String user = cluster.get("user:1000");
250
Map<String, String> profile = cluster.hgetAll("user:1000:profile");
251
252
// Cluster-specific operations
253
String clusterInfo = cluster.clusterInfo();
254
String nodes = cluster.clusterNodes();
255
256
// Get slot information
257
Long slot = cluster.clusterKeySlot("user:1000");
258
List<String> keysInSlot = cluster.clusterGetKeysInSlot(slot.intValue(), 10);
259
260
} finally {
261
cluster.close();
262
}
263
```
264
265
### Cluster Pipeline
266
267
Pipeline operations for Redis Cluster with automatic node routing.
268
269
```java { .api }
270
public class ClusterPipeline implements AutoCloseable {
271
/**
272
* Creates cluster pipeline
273
* @param clusterNodes Cluster node endpoints
274
* @param clientConfig Client configuration
275
*/
276
public ClusterPipeline(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig);
277
278
/**
279
* Queues SET command
280
* @param key Redis key
281
* @param value String value
282
* @return Response object for the command
283
*/
284
public Response<String> set(String key, String value);
285
286
/**
287
* Queues GET command
288
* @param key Redis key
289
* @return Response object for the command
290
*/
291
public Response<String> get(String key);
292
293
/**
294
* Queues hash operations
295
* @param key Hash key
296
* @param field Hash field
297
* @param value Field value
298
* @return Response object for the command
299
*/
300
public Response<Long> hset(String key, String field, String value);
301
302
/**
303
* Synchronizes all queued commands
304
* Sends commands to appropriate cluster nodes and retrieves responses
305
*/
306
public void sync();
307
308
/**
309
* Synchronizes and returns all responses
310
* @return List of command responses
311
*/
312
public List<Object> syncAndReturnAll();
313
314
/**
315
* Closes the cluster pipeline
316
*/
317
public void close();
318
}
319
```
320
321
#### Usage Example
322
323
```java
324
Set<HostAndPort> clusterNodes = Set.of(
325
new HostAndPort("node1", 7000),
326
new HostAndPort("node2", 7001),
327
new HostAndPort("node3", 7002)
328
);
329
330
try (ClusterPipeline pipeline = new ClusterPipeline(clusterNodes, clientConfig)) {
331
// Queue multiple commands - they'll be routed to appropriate nodes
332
Response<String> r1 = pipeline.set("key1", "value1");
333
Response<String> r2 = pipeline.set("key2", "value2");
334
Response<Long> r3 = pipeline.hset("hash1", "field1", "value1");
335
336
// Execute all commands
337
pipeline.sync();
338
339
// Get results
340
String result1 = r1.get(); // "OK"
341
String result2 = r2.get(); // "OK"
342
Long result3 = r3.get(); // 1
343
}
344
```
345
346
## Redis Sentinel (High Availability)
347
348
### JedisSentinelPool
349
350
Connection pool with Redis Sentinel integration for automatic failover.
351
352
```java { .api }
353
public class JedisSentinelPool extends Pool<Jedis> {
354
/**
355
* Creates sentinel pool with default configuration
356
* @param masterName Master name in Sentinel configuration
357
* @param sentinels Set of Sentinel endpoints ("host:port")
358
*/
359
public JedisSentinelPool(String masterName, Set<String> sentinels);
360
361
/**
362
* Creates sentinel pool with pool configuration
363
* @param masterName Master name in Sentinel configuration
364
* @param sentinels Set of Sentinel endpoints
365
* @param poolConfig Pool configuration
366
*/
367
public JedisSentinelPool(String masterName, Set<String> sentinels,
368
JedisPoolConfig poolConfig);
369
370
/**
371
* Creates sentinel pool with client configuration
372
* @param masterName Master name
373
* @param sentinels Sentinel endpoints
374
* @param clientConfig Client configuration
375
*/
376
public JedisSentinelPool(String masterName, Set<String> sentinels,
377
JedisClientConfig clientConfig);
378
379
/**
380
* Creates sentinel pool with full configuration
381
* @param masterName Master name
382
* @param sentinels Sentinel endpoints
383
* @param poolConfig Pool configuration
384
* @param clientConfig Client configuration
385
*/
386
public JedisSentinelPool(String masterName, Set<String> sentinels,
387
JedisPoolConfig poolConfig, JedisClientConfig clientConfig);
388
389
/**
390
* Creates sentinel pool with sentinel authentication
391
* @param masterName Master name
392
* @param sentinels Sentinel endpoints
393
* @param poolConfig Pool configuration
394
* @param clientConfig Master client configuration
395
* @param sentinelClientConfig Sentinel client configuration
396
*/
397
public JedisSentinelPool(String masterName, Set<String> sentinels,
398
JedisPoolConfig poolConfig, JedisClientConfig clientConfig,
399
JedisClientConfig sentinelClientConfig);
400
401
/**
402
* Gets current master endpoint
403
* @return Current Redis master host and port
404
*/
405
public HostAndPort getCurrentHostMaster();
406
407
/**
408
* Gets Jedis connection from pool
409
* @return Jedis instance connected to current master
410
*/
411
public Jedis getResource();
412
413
/**
414
* Destroys the sentinel pool
415
*/
416
public void destroy();
417
}
418
```
419
420
### SentinelCommands
421
422
Commands for interacting with Redis Sentinel.
423
424
```java { .api }
425
public interface SentinelCommands {
426
/**
427
* Get master address by name
428
* @param masterName Master name
429
* @return Master host and port
430
*/
431
List<String> sentinelGetMasterAddrByName(String masterName);
432
433
/**
434
* Get master information
435
* @param masterName Master name
436
* @return Master configuration and status
437
*/
438
List<Map<String, String>> sentinelMasters();
439
440
/**
441
* Get specific master information
442
* @param masterName Master name
443
* @return Master details
444
*/
445
Map<String, String> sentinelMaster(String masterName);
446
447
/**
448
* Get sentinel information about slaves
449
* @param masterName Master name
450
* @return List of slave configurations
451
*/
452
List<Map<String, String>> sentinelSlaves(String masterName);
453
454
/**
455
* Get sentinel information about other sentinels
456
* @param masterName Master name
457
* @return List of sentinel configurations
458
*/
459
List<Map<String, String>> sentinelSentinels(String masterName);
460
461
/**
462
* Force failover of master
463
* @param masterName Master name
464
* @return Status code reply
465
*/
466
String sentinelFailover(String masterName);
467
468
/**
469
* Monitor new master
470
* @param masterName Master name
471
* @param ip Master IP address
472
* @param port Master port
473
* @param quorum Quorum for failover decisions
474
* @return Status code reply
475
*/
476
String sentinelMonitor(String masterName, String ip, int port, int quorum);
477
478
/**
479
* Remove master from monitoring
480
* @param masterName Master name
481
* @return Status code reply
482
*/
483
String sentinelRemove(String masterName);
484
485
/**
486
* Set master configuration
487
* @param masterName Master name
488
* @param parameterName Parameter name
489
* @param parameterValue Parameter value
490
* @return Status code reply
491
*/
492
String sentinelSet(String masterName, String parameterName, String parameterValue);
493
}
494
```
495
496
#### Usage Example
497
498
```java
499
String masterName = "mymaster";
500
Set<String> sentinels = new HashSet<>();
501
sentinels.add("sentinel1.example.com:26379");
502
sentinels.add("sentinel2.example.com:26379");
503
sentinels.add("sentinel3.example.com:26379");
504
505
JedisPoolConfig poolConfig = new JedisPoolConfig();
506
poolConfig.setMaxTotal(20);
507
poolConfig.setTestOnBorrow(true);
508
509
JedisClientConfig masterConfig = DefaultJedisClientConfig.builder()
510
.password("redis-password")
511
.database(0)
512
.build();
513
514
JedisClientConfig sentinelConfig = DefaultJedisClientConfig.builder()
515
.password("sentinel-password")
516
.build();
517
518
JedisSentinelPool sentinelPool = new JedisSentinelPool(
519
masterName, sentinels, poolConfig, masterConfig, sentinelConfig
520
);
521
522
try {
523
// Use pool normally - failover is automatic
524
try (Jedis jedis = sentinelPool.getResource()) {
525
jedis.set("key", "value");
526
String value = jedis.get("key");
527
}
528
529
// Monitor current master
530
HostAndPort currentMaster = sentinelPool.getCurrentHostMaster();
531
System.out.println("Current master: " + currentMaster);
532
533
} finally {
534
sentinelPool.destroy();
535
}
536
```
537
538
## Multi-Cluster Failover
539
540
### Multi-Cluster Configuration
541
542
Configuration for multi-cluster failover scenarios with circuit breaker patterns.
543
544
```java { .api }
545
public class MultiClusterClientConfig {
546
public static class Builder {
547
/**
548
* Sets exceptions that trigger fallback to next cluster
549
* @param fallbackExceptionList List of exception classes
550
* @return Builder instance
551
*/
552
public Builder fallbackExceptionList(List<Class<? extends Exception>> fallbackExceptionList);
553
554
/**
555
* Sets circuit breaker sliding window size
556
* @param circuitBreakerSlidingWindowSize Window size for failure tracking
557
* @return Builder instance
558
*/
559
public Builder circuitBreakerSlidingWindowSize(int circuitBreakerSlidingWindowSize);
560
561
/**
562
* Sets circuit breaker failure rate threshold
563
* @param circuitBreakerFailureRateThreshold Failure rate (0.0-1.0) to open circuit
564
* @return Builder instance
565
*/
566
public Builder circuitBreakerFailureRateThreshold(float circuitBreakerFailureRateThreshold);
567
568
/**
569
* Sets minimum calls before circuit breaker evaluation
570
* @param circuitBreakerMinimumNumberOfCalls Minimum calls in window
571
* @return Builder instance
572
*/
573
public Builder circuitBreakerMinimumNumberOfCalls(int circuitBreakerMinimumNumberOfCalls);
574
575
/**
576
* Sets circuit breaker wait duration in open state
577
* @param circuitBreakerWaitDurationInOpenState Wait duration in milliseconds
578
* @return Builder instance
579
*/
580
public Builder circuitBreakerWaitDurationInOpenState(long circuitBreakerWaitDurationInOpenState);
581
582
/**
583
* Builds multi-cluster configuration
584
* @return Configuration instance
585
*/
586
public MultiClusterClientConfig build();
587
}
588
589
public List<Class<? extends Exception>> getFallbackExceptionList();
590
public int getCircuitBreakerSlidingWindowSize();
591
public float getCircuitBreakerFailureRateThreshold();
592
public int getCircuitBreakerMinimumNumberOfCalls();
593
public long getCircuitBreakerWaitDurationInOpenState();
594
}
595
```
596
597
### Multi-Cluster Connection Provider
598
599
Connection provider supporting multiple Redis clusters with automatic failover.
600
601
```java { .api }
602
public class MultiClusterPooledConnectionProvider implements ConnectionProvider {
603
/**
604
* Creates multi-cluster connection provider
605
* @param clusters List of cluster connection providers in priority order
606
* @param multiClusterClientConfig Multi-cluster configuration
607
*/
608
public MultiClusterPooledConnectionProvider(List<ClusterConnectionProvider> clusters,
609
MultiClusterClientConfig multiClusterClientConfig);
610
611
@Override
612
public Connection getConnection();
613
614
@Override
615
public Connection getConnection(CommandArguments commandArguments);
616
617
/**
618
* Gets active cluster index
619
* @return Index of currently active cluster
620
*/
621
public int getActiveMultiClusterIndex();
622
623
/**
624
* Gets cluster providers
625
* @return List of cluster connection providers
626
*/
627
public List<ClusterConnectionProvider> getClusterConnectionProviders();
628
629
/**
630
* Manually trigger failover to next cluster
631
*/
632
public void incrementActiveMultiClusterIndex();
633
634
@Override
635
public void close();
636
}
637
```
638
639
### Circuit Breaker Failover
640
641
Circuit breaker implementation for multi-cluster failover.
642
643
```java { .api }
644
public class CircuitBreakerFailoverBase {
645
/**
646
* Creates circuit breaker failover
647
* @param connectionProvider Primary connection provider
648
* @param config Multi-cluster configuration
649
*/
650
public CircuitBreakerFailoverBase(ConnectionProvider connectionProvider,
651
MultiClusterClientConfig config);
652
653
/**
654
* Executes command with circuit breaker protection
655
* @param commandObject Command to execute
656
* @return Command result
657
*/
658
public <T> T execute(CommandObject<T> commandObject);
659
660
/**
661
* Gets circuit breaker state
662
* @return Current circuit breaker state (CLOSED, OPEN, HALF_OPEN)
663
*/
664
public String getCircuitBreakerState();
665
666
/**
667
* Gets failure rate
668
* @return Current failure rate (0.0-1.0)
669
*/
670
public float getFailureRate();
671
672
/**
673
* Resets circuit breaker statistics
674
*/
675
public void resetCircuitBreakerStats();
676
}
677
```
678
679
#### Usage Example
680
681
```java
682
// Configure multiple clusters
683
List<ClusterConnectionProvider> clusters = Arrays.asList(
684
// Primary cluster
685
new ClusterConnectionProvider(
686
Set.of(new HostAndPort("primary-node1", 7000),
687
new HostAndPort("primary-node2", 7001)),
688
primaryConfig, poolConfig
689
),
690
// Fallback cluster
691
new ClusterConnectionProvider(
692
Set.of(new HostAndPort("fallback-node1", 7000),
693
new HostAndPort("fallback-node2", 7001)),
694
fallbackConfig, poolConfig
695
)
696
);
697
698
// Configure circuit breaker
699
MultiClusterClientConfig multiConfig = MultiClusterClientConfig.builder()
700
.fallbackExceptionList(Arrays.asList(
701
JedisConnectionException.class,
702
JedisClusterException.class
703
))
704
.circuitBreakerFailureRateThreshold(0.5f) // 50% failure rate
705
.circuitBreakerSlidingWindowSize(100) // 100 call window
706
.circuitBreakerMinimumNumberOfCalls(10) // Min 10 calls
707
.circuitBreakerWaitDurationInOpenState(60000) // 1 minute wait
708
.build();
709
710
MultiClusterPooledConnectionProvider mcProvider =
711
new MultiClusterPooledConnectionProvider(clusters, multiConfig);
712
713
UnifiedJedis multiClusterJedis = new UnifiedJedis(mcProvider);
714
715
try {
716
// Operations automatically failover if primary cluster fails
717
multiClusterJedis.set("key", "value");
718
String value = multiClusterJedis.get("key");
719
720
// Monitor cluster state
721
int activeCluster = mcProvider.getActiveMultiClusterIndex();
722
System.out.println("Active cluster: " + activeCluster);
723
724
} finally {
725
multiClusterJedis.close();
726
}
727
```
728
729
## Sharded Deployments
730
731
### JedisSharding
732
733
Client for consistent hashing across multiple Redis instances.
734
735
```java { .api }
736
public class JedisSharding implements JedisCommands, AutoCloseable {
737
/**
738
* Creates sharding client with default hashing
739
* @param shards List of Redis shard information
740
*/
741
public JedisSharding(List<JedisShardInfo> shards);
742
743
/**
744
* Creates sharding client with custom hashing algorithm
745
* @param shards List of Redis shard information
746
* @param algo Hashing algorithm implementation
747
*/
748
public JedisSharding(List<JedisShardInfo> shards, Hashing algo);
749
750
/**
751
* Creates sharding client with key tag pattern
752
* @param shards List of Redis shard information
753
* @param keyTagPattern Pattern for extracting hash tags from keys
754
*/
755
public JedisSharding(List<JedisShardInfo> shards, Pattern keyTagPattern);
756
757
/**
758
* Gets shard information for a key
759
* @param key Redis key
760
* @return Shard info for the key
761
*/
762
public JedisShardInfo getShardInfo(String key);
763
764
/**
765
* Gets all shard information
766
* @return Collection of all shards
767
*/
768
public Collection<JedisShardInfo> getAllShardInfo();
769
770
/**
771
* Gets Jedis connection for specific key
772
* @param key Redis key
773
* @return Jedis instance for the shard containing the key
774
*/
775
public Jedis getShard(String key);
776
777
/**
778
* Gets Jedis connection for specific shard info
779
* @param shardInfo Shard information
780
* @return Jedis instance for the shard
781
*/
782
public Jedis getShard(JedisShardInfo shardInfo);
783
784
/**
785
* Closes all shard connections
786
*/
787
public void close();
788
}
789
```
790
791
### JedisShardInfo
792
793
Information about individual Redis shards in a sharded deployment.
794
795
```java { .api }
796
public class JedisShardInfo {
797
/**
798
* Creates shard info with equal weight
799
* @param host Redis host
800
* @param port Redis port
801
*/
802
public JedisShardInfo(String host, int port);
803
804
/**
805
* Creates shard info with custom weight
806
* @param host Redis host
807
* @param port Redis port
808
* @param weight Shard weight for consistent hashing
809
*/
810
public JedisShardInfo(String host, int port, int weight);
811
812
/**
813
* Creates shard info with name and weight
814
* @param host Redis host
815
* @param port Redis port
816
* @param name Shard name
817
* @param weight Shard weight
818
*/
819
public JedisShardInfo(String host, int port, String name, int weight);
820
821
/**
822
* Creates shard info from URI
823
* @param uri Redis connection URI
824
*/
825
public JedisShardInfo(URI uri);
826
827
public String getHost();
828
public int getPort();
829
public String getName();
830
public int getWeight();
831
public int getTimeout();
832
public String getPassword();
833
834
/**
835
* Sets connection timeout
836
* @param timeout Timeout in milliseconds
837
*/
838
public void setTimeout(int timeout);
839
840
/**
841
* Sets authentication password
842
* @param auth Redis password
843
*/
844
public void setPassword(String auth);
845
}
846
```
847
848
#### Usage Example
849
850
```java
851
// Create shards with different weights
852
List<JedisShardInfo> shards = Arrays.asList(
853
new JedisShardInfo("redis1.example.com", 6379, "shard1", 1),
854
new JedisShardInfo("redis2.example.com", 6379, "shard2", 1),
855
new JedisShardInfo("redis3.example.com", 6379, "shard3", 2) // Double weight
856
);
857
858
JedisSharding sharding = new JedisSharding(shards);
859
860
try {
861
// Keys automatically distributed based on consistent hashing
862
sharding.set("user:1000", "John"); // Routed to appropriate shard
863
sharding.set("user:2000", "Jane"); // May go to different shard
864
865
// Retrieve data - client finds correct shard automatically
866
String user1 = sharding.get("user:1000");
867
String user2 = sharding.get("user:2000");
868
869
// Get shard information
870
JedisShardInfo shardInfo = sharding.getShardInfo("user:1000");
871
System.out.println("Key user:1000 on shard: " + shardInfo.getName());
872
873
} finally {
874
sharding.close();
875
}
876
```
877
878
## Connection Routing and Load Balancing
879
880
### Broadcast and Round Robin
881
882
Configuration for operations that need to run on multiple Redis instances.
883
884
```java { .api }
885
public class JedisBroadcastAndRoundRobinConfig {
886
/**
887
* Creates broadcast and round robin configuration
888
* @param pools List of connection pools for different Redis instances
889
*/
890
public JedisBroadcastAndRoundRobinConfig(List<Pool<Connection>> pools);
891
892
/**
893
* Gets connection for broadcast operation (all pools)
894
* @return List of connections from all pools
895
*/
896
public List<Connection> getBroadcastConnections();
897
898
/**
899
* Gets connection for round-robin operation (next pool in rotation)
900
* @return Connection from next pool in round-robin
901
*/
902
public Connection getRoundRobinConnection();
903
904
/**
905
* Gets all connection pools
906
* @return List of connection pools
907
*/
908
public List<Pool<Connection>> getPools();
909
}
910
```
911
912
This documentation covers Redis clustering, high availability, and advanced deployment patterns available in Jedis. The library provides comprehensive support for production Redis deployments with automatic failover, load balancing, and fault tolerance.