top of page
  • Writer's pictureRohit Modi

System Design : Traffic Signal Control

Updated: Dec 18, 2022

Hi, a warm welcome to tech-maze, before deep diving in the topic, let me tell you one small story, recently i have met one of my friend and he was talking about his interview experience, one of the question was related to system design so i have asked what exactly the question was, then he said interviewer wanted me to design a system which can improve the traffic signal problem and provide more robust signaling system.


He gave the answer but couldn't optimize the solution, later we have discussed and found one of the design which looks quite robust, lets look at the solution.


In this topic we will consider following parameters :-

1. junction where 4 roads meet from all direction

2. where to utilize TCP connection

3. how Load balancer can help

4. where Kubernetes or any container orchestration tool can be used


Lets begin with diagram and then will break down the logic in steps.




Step 1 : Each Camera will be connected with Server and each camera mapped with an Id lets say camId which distinguishes from other camera, mapping will be stored in server.


Step 2 : Camera will be sending the snapshot of the road to server and server process this snapshot and find out how many vehicles are present in snapshot by using image processing algorithms.


Step 3 : if number of vehicle in snapshot is more than 20 then server send this info to one of the microservice via API gateway (API gateway used for authorization and parsing of request in machine instructions), lets say the service name is SnapShotProcessorService, since server is sending the request to client hence this should be peer to peer connection and for this we use TCP connection instead of http connection and to achieve peer to peer connection we have implemented WebSocket in SnapShotProcessorService.


Step 4 : When SnapShotProcessorService receives request with camera id, it saves the event in messageQueue (RabbitMQ or Kafka etc.), event contains camId and direction.

Here we have used messageQueue, the reason being is, we don't want to loose any of the request in the processing and request will be served and managed based on timestamp, these facilities are in built in messageQueue.


Step 5 : MessageQueue publish the events and services start consuming the event, lets say service which consume the request is ProcessSignalEventService.


Step 6 : ProcessSignalEventService process the request in following manner


a. ProcessSignalEventService sends the request to another service (TrafficSignalService), request contains camId and signal color as

below

service name : TrafficSignalService

request :

camId - camera4

signalColor - Red

b. if (return value is true)

ProcessSignalEventService send the request again to

TrafficSignalService, request will contains camId and signal color as

below

service name : TrafficSignalService

request :

camId - camera4

signalColor - Green

else

retry the process again.

c. if (return value is true)

ProcessSignalEventService send the request again to TrafficSignalService,

request will contains camId and signal color as

below

service name : TrafficSignalService

request :

camId - camera4

signalColor - any color

else

retry the process again.

d. processing of request ends here.



Step 7 : TrafficSignalService receives the request and it process the request in following manner


if (signalColor is Red)

- send request to server, server sends notification to Signal system to blink

Red light

- start a counter for 20 seconds

- when counter reaches 0, return true

else if (signalColor is Green)

- send request to server, server sends notification to Signal system to blink

Green light

- start a counter for 15 seconds

- when counter reaches 0, return true

else

- send request with any signal color other than red and green to server to

stop blinking any color at the Signal system

- return true


Now to make this system fault tolerant and no single point of failure, we can take following steps :


1. create multiple instance of messageQueue, ProcessSignalEventService and

TrafficSignalService using container orchestration tool such as Kubernetes

2. can have multiple servers which can process these request

3. For proper redirection of request, LoadBalancer can be used, this can be placed just

after API gateway

4. Save signal related data in database to make sure that data will be available

throughout the application for multiple purposes such as Monitoring

and analysis etc.


I did not consider authentication and few failure or over provisioning scenario to track down the error, i have tried to keep it simple and precise so that people can understand, this could be one of the way to begin with a robust traffic signal control design.


That's all folks in this article, keep visiting, feel free to share this article and share your suggestion/thoughts or any other solution in comment section.


A Big thank you for checking out my article, this really encourage me to come up with more topics.


Here are some resources you might be interested ------------------------






64 views0 comments

Recent Posts

See All
Spring Boot

©2022 by tech-maze. Proudly created with Wix.com

bottom of page