Step 4 - Challenge: Handling Concurrent Clients

Step 4 - Challenge: Handling Concurrent Clients

In this step your goal is to handle multiple concurrent clients. If you haven’t already done so, now is the time to make your Redis server handle concurrent clients. You have two basic options here; have one thread per client or use the asynchronous programming support offered by your chosen programming language. If you have time, give both a go, both have pros and cons.

You can use redis-benchmark as a client to check how you handle concurrency:

% redis-benchmark -t SET,GET -q
SET: 108225.10 requests per second, p50=0.223 msec
GET: 115606.94 requests per second, p50=0.215 msec

Note that by default this will create 50 client connections to your server. Check the full documentation for Redis Benchmark to see how else you can use it.

You should move on to Step 5 when your server can handle the above command without crashing. If the command is taking too long to run add -n 1000 onto the end to reduce the number of requests. Play around with the number if needs be.

Don’t focus on performance, just check your server can handle concurrency without any errors.

Build Your Own Redis Server (Go Edition)

Buy nowLearn more

Introduction

  • Introduction
  • How To Use This Course
  • Learn About Redis

Step 0 - Setting Up

  • Step Goals
  • Installing Redis
  • Setting Up Go
  • Learn To Use Redis, Redis CLI And Redis Benchmark
  • Create A GitHub Repo For Your Project.

Step 1 - Introduction to RESP, Building a Protocol Handler Using Test-Driven Development

  • Introduction to RESP2
  • Introduction to Test Driven Development
  • Using Test Driven Development to Build a Protocol Handler
  • Step 1 - Challenge: Protocol Handling2
  • Step 1 - Solution: Protocol Handling

Step 2 - Creating a Network Client and Server

  • Introduction To Creating A Network Client And Server
  • Serialising the RESP Data Types1
  • Building a Simple Redis CLI
  • Testing the CLI Against Redis
  • Sample Solution for a Simple Redis CLI
  • Sample Solution for a Simple Redis CLI
  • Step 2 - Challenge: Handling PING and ECHO Commands
  • Building a Simple Server To Handle Incoming Connections
  • Responding To The PING And ECHO Commands
  • Step 2 - Solution: Handling PING And ECHO Commands2

Step 3 - Data Structures, Set and Get

  • Set Up Continuous integration
  • Step 3 - Challenge: SET and GET
  • Adding The Core Data Structure
  • Handling The Set Command
  • Handling the Get command
  • Using the Redis CLI to test Set and Get
  • Step 3 - Solution: SET and GET1

Step 4 - Handling Concurrency

  • Step 4 - Challenge: Handling Concurrent Clients
  • Making The Data Store Thread-Safe
  • Handling Concurrent Clients With Goroutines
  • Using The Redis CLI And Redis Benchmark To Test Concurrency
  • Step 4 - Solution: Handling Concurrent Clients

Step 5 - Implementing Expiry

  • Introducing The Two Methods Of Expiry
  • Step 5 - Challenge: Implementing Key Expiry
  • Updating The Data Store And Set Command Handler Ready For Expiry
  • Implementing Expire On Read
  • Implementing Background Expiry
  • Step 5 - Solution: Implementing Key Expiry

Step 6 - Implementing More Commands

  • Step 6 - Challenge: Adding More Commands
  • Implementing EXISTS and DEL
  • Implementing INCR and DECR
  • Implementing LPUSH, RPUSH And LRANGE
  • Step 6 - Solution: Adding More Commands1

Step 7 - Performance

  • Step 7 - Challenge: Performance Optimisation and Benchmarking
  • Benchmarking Your Server
  • How To Optimise For Performance
  • Preparing To Profile The Server
  • Profiling The Go Code
  • Performance Tuning The Server

Step 8 - Persistence

  • Step 8 - Challenge: Persistence
  • Persisting The Database
  • Loading The Database
  • Step 8 - Solution: Persistence

Closing Remarks

  • The Complete Final Solution
  • Going Further
  • Wrapping Up