Skip to content

Commit 1a436d4

Browse files
Adds docstrings for sync redis single client
Signed-off-by: Elena Kolevska <[email protected]>
1 parent a757bad commit 1a436d4

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

redis/client.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,133 @@ def __init__(
248248
"""
249249
Initialize a new Redis client.
250250
251+
PARAMETER DOCUMENTATION
252+
=======================
253+
254+
host (str, default="localhost"):
255+
Main description: Redis server hostname or IP address.
256+
Recommended values:
257+
- Production: Use FQDN (e.g., "redis.example.com")
258+
- Development: "localhost" or "127.0.0.1"
259+
- Docker: Service name (e.g., "redis-service")
260+
Related parameters: port, unix_socket_path (mutually exclusive)
261+
Common issues:
262+
- IPv6 resolution delays: Use "127.0.0.1" instead of "localhost"
263+
- Firewall blocks: Ensure Redis port is accessible
264+
265+
port (int, default=6379):
266+
Main description: Redis server port number.
267+
Recommended values:
268+
- Standard: 6379 (default Redis port)
269+
Related parameters: host, ssl (for secure connections)
270+
Common issues:
271+
- Port conflicts: Check if port is already in use
272+
- Firewall rules: Ensure port is open in security groups
273+
274+
db (int, default=0):
275+
Main description: Redis logical database number.
276+
Possible values: 0 - 15
277+
Recommended values:
278+
- Single app: 0 (default)
279+
- Namespaced keys: Different db per namespace (0-15)
280+
- Testing: Separate logical db for tests (e.g., 15)
281+
Trade-offs:
282+
- Multiple DBs: Logical separation but shared memory/CPU and configuration
283+
- Single DB: Better performance but requires key prefixing for namespacing
284+
Related parameters: None (isolated per database)
285+
Use cases:
286+
- Logical separation: db=0 (cache), db=1 (sessions), db=2 (temp data)
287+
- Testing: Ex. use db=15 for test data isolation
288+
Common issues:
289+
- DB limit: Redis supports 16 DBs by default (configurable)
290+
- Cross-DB operations: FLUSHALL affects ALL databases (https://redis.io/docs/latest/commands/flushall/)
291+
- MOVE command: Moves keys between databases (https://redis.io/docs/latest/commands/move/)
292+
- Cluster mode: Only db=0 supported in Redis Cluster
293+
- NOT true multi-tenancy: All DBs share CPU, memory, ACL, and configuration
294+
Security considerations:
295+
- Redis databases provide logical separation only, NOT security isolation
296+
- All databases share the same authentication and ACL rules
297+
- Memory and CPU resources are shared across all databases
298+
- Use separate Redis instances for true tenant isolation
299+
300+
password (Optional[str], default=None):
301+
Main description: Redis server authentication password.
302+
Recommended values:
303+
- Production: Strong password (32+ chars, mixed case, symbols)
304+
- Development: Simple password or None for local dev
305+
Related parameters: username (for ACL), credential_provider
306+
Security considerations:
307+
- Never commit passwords to version control
308+
- Use Redis ACL for fine-grained access control
309+
- Rotate passwords regularly
310+
311+
socket_timeout (Optional[float], default=None):
312+
Main description: Timeout for socket read/write operations in seconds.
313+
Recommended values:
314+
- Same datacenter: 0.3-0.5 seconds
315+
- Same region: 0.5-1.5 seconds
316+
- Cross-region (same continent): 1.0-2.0 seconds
317+
- Intercontinental: 1.0-3.0 seconds
318+
- Slow networks/unreliable connections: 3.0-10.0 seconds
319+
- Real-time systems: 0.05-0.2 seconds
320+
- None: No timeout (blocks indefinitely)
321+
Trade-offs:
322+
- Low timeout: Fast failure detection but may cause false timeouts
323+
- High timeout: More resilient but slower error detection
324+
- None: Never times out but may hang indefinitely
325+
Related parameters: socket_connect_timeout, retry, health_check_interval, ssl
326+
Use cases:
327+
- API endpoints: 0.5-2.0s to prevent request hanging
328+
- Slow/unreliable networks: 3-10s for poor connectivity
329+
- Real-time trading: 0.05-0.2s for predictable latency
330+
Common issues:
331+
- Timeout too low: Legitimate operations fail
332+
- No timeout: Application hangs on network issues
333+
- Inconsistent timeouts: Different behavior across environments
334+
Performance implications:
335+
- Affects all Redis operations (GET, SET, etc.)
336+
- Should be tuned based on network latency and operation complexity
337+
Distance and latency considerations:
338+
- Same datacenter: ~0.1-1ms RTT, Redis responds <1ms, use 0.1-0.5s timeout
339+
- Same region (e.g., us-east-1a to us-east-1b): ~1-5ms RTT, use 0.2-1.0s timeout
340+
- Cross-region same continent (e.g., us-east-1 to us-west-2): ~70-100ms RTT, use 0.5-2.0s timeout
341+
- Cross-continent (e.g., us-east-1 to eu-west-1): ~100-150ms RTT, use 1.0-3.0s timeout
342+
- Intercontinental (e.g., us-east-1 to ap-southeast-1): ~180-250ms RTT, use 1.5-4.0s timeout
343+
- Note: Redis operations typically complete in <1ms; timeout accounts for network RTT + safety margin
344+
TLS/SSL impact:
345+
- TLS handshake adds 1-3 additional round trips during connection establishment
346+
- Ongoing operations have minimal TLS overhead (~5-10% latency increase)
347+
- Consider higher socket_timeout when using SSL, especially for distant Redis instances
348+
349+
socket_connect_timeout (Optional[float], default=None):
350+
Main description: Timeout for initial socket connection in seconds.
351+
Recommended values:
352+
- Local Redis (no SSL): 1.0-3.0 seconds
353+
- Local Redis (with SSL): 2.0-5.0 seconds
354+
- Remote Redis (no SSL): 3.0-8.0 seconds
355+
- Remote Redis (with SSL): 5.0-15.0 seconds
356+
- Unreliable networks: 10.0-30.0 seconds
357+
- None: Uses system default (usually 60-120s)
358+
Trade-offs:
359+
- Short timeout: Fast failure on connection issues
360+
- Long timeout: More resilient to network delays
361+
- None: May wait too long for failed connections
362+
Related parameters: socket_timeout, ssl, retry
363+
Common issues:
364+
- Too short: Fails on slow networks or high load
365+
- Too long: Slow startup and poor user experience in case of network issues
366+
- Network partitions: Connection hangs without timeout
367+
Performance implications:
368+
- Only affects initial connection establishment
369+
- Important for application startup time
370+
- Should account for network latency and server load
371+
SSL/TLS handshake considerations:
372+
- TLS handshake requires 1-3 additional round trips (2-6 RTT total)
373+
- Certificate validation adds processing time
374+
- Distant Redis with SSL: add 2-3x the base RTT to timeout
375+
- Example: 100ms RTT + SSL = ~300-500ms handshake time
376+
- Consider TLS session resumption to reduce reconnection overhead
377+
251378
To specify a retry policy for specific errors, you have two options:
252379
253380
1. Set the `retry_on_error` to a list of the error/s to retry on, and

0 commit comments

Comments
 (0)