Sticky Session Best Practices
Sticky sessions maintain the same proxy IP across multiple requests. This is essential for workflows that rely on server-side state, such as login flows, shopping carts, or multi-page form submissions. Hex Proxies supports sticky sessions across its 10M+ residential and 250K+ ISP proxy pools.
How Sticky Sessions Work
Append a session identifier to your proxy username. The gateway maps that identifier to a specific exit IP and routes all matching requests through it:
Username format: YOUR_USERNAME-session-IDENTIFIER
Example: user123-session-checkout-flow-001When to Use Sticky Sessions
| Scenario | Session Type | Duration | |----------|-------------|----------| | Login + authenticated browsing | Sticky | 5-30 minutes | | Shopping cart + checkout | Sticky | 10-15 minutes | | Multi-page form submission | Sticky | 5-10 minutes | | API with rate limits per IP | Sticky | Varies | | Stateless scraping | Rotating | N/A | | Price comparison | Rotating | N/A |
Implementation Patterns
#### Python: Session-Aware Scraping
import requestsclass StickyProxyScraper: def __init__(self, base_user, password): self.base_user = base_user self.password = password self.gateway = "gate.hexproxies.com:8080"
def create_session(self, label="default"): session_id = f"{label}-{uuid.uuid4().hex[:8]}" proxy_url = f"http://{self.base_user}-session-{session_id}:{self.password}@{self.gateway}" session = requests.Session() session.proxies = {"http": proxy_url, "https": proxy_url} session.timeout = 30 return session
def login_and_scrape(self, login_url, target_url, credentials): session = self.create_session("login")
# Login (same IP) login_resp = session.post(login_url, json=credentials) if login_resp.status_code != 200: raise Exception(f"Login failed: {login_resp.status_code}")
# Scrape authenticated pages (same IP) data_resp = session.get(target_url) return data_resp.json()
scraper = StickyProxyScraper("YOUR_USERNAME", "YOUR_PASSWORD") data = scraper.login_and_scrape( "https://example.com/api/login", "https://example.com/api/data", {"user": "me", "pass": "secret"} ) ```
#### Node.js: Session Management
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';function createStickyAgent(baseUser, password, label = 'default') { const sessionId = `${label}-${crypto.randomBytes(4).toString('hex')}`; const proxyUrl = `http://${baseUser}-session-${sessionId}:${password}@gate.hexproxies.com:8080`; return new HttpsProxyAgent(proxyUrl); }
const agent = createStickyAgent('YOUR_USERNAME', 'YOUR_PASSWORD', 'checkout');
// All requests through this agent use the same IP const loginResp = await fetch('https://example.com/login', { method: 'POST', agent, body: JSON.stringify({ user: 'me', pass: 'secret' }), headers: { 'Content-Type': 'application/json' }, });
const dataResp = await fetch('https://example.com/dashboard', { agent }); console.log(await dataResp.json()); ```
Session Lifecycle Management
- **Generate unique IDs**: Use UUIDs or random hex strings. Never reuse session IDs across unrelated workflows.
- **Label sessions**: Include a human-readable label (e.g., "checkout", "login") for debugging.
- **Expire sessions**: Create new session IDs after the workflow completes. Do not hold sessions indefinitely.
- **Handle IP changes**: If the session IP changes unexpectedly, create a new session and restart the workflow.
Common Pitfalls
- **Reusing session IDs across workflows**: This can cause unrelated requests to share an IP, leading to unexpected blocks.
- **Holding sessions too long**: Extended sessions can trigger rate limits or IP reputation degradation.
- **Mixing sticky and rotating in the same flow**: The login request and subsequent authenticated requests must all use the same session ID.
- **Not checking IP consistency**: Verify the exit IP at the start and end of critical workflows.