Redis Clone Part 3: Concurrent TCP Server Loop
Redis achieves blistering throughput using a non-blocking event loop on a single execution thread. `asyncio.start_server` enables thousands of concurrent TCP socket connections without thread lock contention 🚀.
import asyncio
async def handle_client(reader, writer):
request = await reader.read(256)
if b"PING" in request:
writer.write(b"+PONG\r\n")
await writer.drain()
writer.close()
await writer.wait_closed()
async def demo():
server = await asyncio.start_server(handle_client, "127.0.0.1", 8985)
r, w = await asyncio.open_connection("127.0.0.1", 8985)
w.write(b"PING\r\n")
await w.drain()
reply = await r.read(64)
print(f"Server replied: {reply.decode().strip()} 🚀")
w.close()
await w.wait_closed()
server.close()
await server.wait_closed()
asyncio.run(demo())
# 109. Redis Clone Part 3: Concurrent TCP Server Loop Redis achieves blistering throughput using a non-blocking event loop on a single execution thread. `asyncio.start_server` enables thousands of concurrent TCP socket connections without thread lock contention 🚀. ```python import asyncio async def handle_client(reader, writer): request = await reader.read(256) if b"PING" in request: writer.write(b"+PONG\r\n") await writer.drain() writer.close() await writer.wait_closed() async def demo(): server = await asyncio.start_server(handle_client, "127.0.0.1", 8985) r, w = await asyncio.open_connection("127.0.0.1", 8985) w.write(b"PING\r\n") await w.drain() reply = await r.read(64) print(f"Server replied: {reply.decode().strip()} 🚀") w.close() await w.wait_closed() server.close() await server.wait_closed() asyncio.run(demo()) ``` Because Redis operations run in-memory in nanoseconds, CPU execution is rarely the bottleneck; network I/O dominates. Modern Redis 6+ offloads socket reads and writes to I/O threads while keeping data execution single-threaded ⚡! --- ### Question Why does Redis utilize a single-threaded event loop for core command execution? 🧠- [ ] a. It eliminates lock synchronization overhead, context switches, and race conditions while maximizing I/O concurrency 🚀 - [ ] b. Linux does not permit multi-threaded programs to bind TCP ports - [ ] c. It reduces TCP packet header sizes by 50% - [ ] d. Memory allocation is prohibited on secondary CPU threads --- ### Challenge Build an `asyncio.start_server` on `('127.0.0.1', 8981)` responding `b'+PONG\r\n'` to `b'PING'`. Connect, send PING, read response, clean up, and print the decoded response stripped (`'+PONG'`). 🎯 <details> <summary>Solution</summary> ```python import asyncio async def handle(reader, writer): data = await reader.read(256) if b"PING" in data: writer.write(b"+PONG\r\n") await writer.drain() writer.close() await writer.wait_closed() async def run(): srv = await asyncio.start_server(handle, "127.0.0.1", 8981) r, w = await asyncio.open_connection("127.0.0.1", 8981) w.write(b"PING\r\n") await w.drain() resp = await r.read(64) w.close() await w.wait_closed() srv.close() await srv.wait_closed() print(resp.decode().strip()) asyncio.run(run()) ``` </details>