mitre

Learn Concurrency and Socket Programming with Go

Learn Concurrency and Socket Programming with Go by implementing a made-up protocol called Short Text Transfer Protocol (STTP). Although Go is the featured language, the project can be implemented in any language you desire.

Diving Deeper into Computer Networking
link

Imagine a computer network as a postal system with multiple layers. Just like the post office delivers letters, a network delivers data between computers. Each computer has an "address" (IP address), ensuring data goes to the right place. Data is packaged in "packets," with each layer adding information like an envelope, stamp, or address label (encapsulation). Routers, like postal workers, use this info to guide packets to their correct destinations through the network's layers.

Tip: When talking about networks we refer to a model called the OSI model and the layer we're working with or having troubleshooting.

optional
circle
The Client Server Model

The client server model is the base architecture for how one device or software application makes a request and receives a response from another device or application.

Key Points
  • Client and Server are not devices or software "things", but roles.
  • A device or program can act as both a client and a server depending on the role it is performing.
  • Example, a parent at a restaurant is a client to the waitress but a server to their child. This shows how roles can shift between client and server based on who/what is making/fulfilling the request.
optional
circle
The OSI Model

The OSI model is a conceptual model used by engineers to talk about computer networks. Software and devices each play a role within the model interacting at different "layers".

Key Points
  • We will cover IP, TCP, HTTP, and TLS in more detail in this and future sections
optional
circle
IP and Network Addressing

Watch videos 1-5 of this playlist. If you were trying to become a network engineer this is super important, but as a software engineer, just watch and absorb the information.

Key Points
  • understand what host and network addressing is
  • understand the relationship between an address, subnet mask, and default gateway
  • understand the difference between public and private Addressing
optional
circle
TCP

A reliable protocol that ensures data is delivered accurately and in order between devices over a network, like a secure, tracked delivery service.

Key Points
  • If an IP Address is the address of a host on a network (apartment building address), a port is the address(apartment unit) of a service or another protocol which resides on that host.
  • Example, HTTP relies on TCP, so it's TCP port is 80
  • The deep dive, linked above, is an excellent explanation.
optional
circle
DNS

Translates human-readable domain names (e.g., example.com) into IP addresses, acting like the internet's phonebook to route requests to the correct server.

optional
circle
Tooling

The following link includes a table breaking down the OSI Model along with which tools you can use to interact and troubleshoot each layer.

Key Points
  • Try out ping, ip, netstat, netcat/ncat for now.
  • Be aware of others, keep them in your back pocket for when needed.
optional
circle
Going Really Deep with TCP

Chris Greer is one of the best presenters of TCP/IP content I've come across, linked is one of his talks. If you ever need to really dive deep, or are curious, hard to beat his content.

Key Points
  • It's a really cool talk just in general, even if its above your head, you get a sense of how software is really communicating back and forth.
Intro to Socket Programming
link

A socket is an endpoint for network communication between two machines or processes, defined by an IP address and port number. It enables data exchange over networks using protocols like TCP or UDP. Socket programming involves creating, managing, and using sockets to build networked applications, such as web servers or chat clients, where data is sent and received between connected systems. This section will introduce you to programming with sockets.

Tip: When working with socket or network packages, they can be a bit overwhelming to work with given all the options and toggles available to any one protocol. I tend to stick to examples and tutorials as I find I need to see a complete working example to understand the big picture.

optional
circle
Follow the Go Socket Example

This is a great self contained example of building a simple client and server application in Go.

Key Points
  • Read up to - "Implementing Advanced Socket Programming", we'll get to concurrency later.
  • Although the article says "Complete Guide", from my experience, there is a LOT more that goes into socket programming. It's a great introduction nonetheless.
optional
circle
Linux Sockets and Buffers

Sockets and buffers are key to socket programming.

Key Points
  • You program binds to a socket(ip:port), listens for data.
  • As data is received, the program needs to store that data somewhere before it can be processed. A buffer serves this purpose.
  • Note, the streams and buffers video is part of a node.js series, but it's applicable in any language.
optional
circle
Start Working on a TCP Server Project in Go

You'll implement a fake protocol over TCP called Short Text Transfer Protocol (STTP). Up to and including "(F) Error Handling and Response".

Key Points
  • Just focus on the server, do not implement the client!
  • You an interface with the server using netcat, telnet, or any other tool which simply opens a socket and lets you input into it.
  • Do not implement Caching or concurrency. We'll get to it.
optional
circle
Follow the Python Socket Example

If you're interested in the Python example, here it is. I recommend sticking with Go for network programming for any serious project though.

optional
circle
Follow along with Python too

You can also implement it in Python as well. If you're having trouble switching between languages, stick with Go if you can.

Key Points
  • Feel free to continue developing the Python implementation alongside the Go implementation as we continue.
Intro to Caching and Performance Tuning
link

Caching is the process of storing frequently accessed data in a fast, temporary storage layer (in memory or caching service) to quickly retrieve it without repeatedly accessing the slower primary storage (filesystem or a Database). It improves performance by reducing latency, and speeding up data retrieval, which can enhance the responsiveness of your apps and services, especially under high demand. This section will introduce you to basic caching.

Tip: Caching falls into the knowledge category of "Systems Design". Which looks at the overall architecture of a software system as a whole to addresses problems of performance, latency, high availability (ensuring your apps do not become unavailable), and more. It's a whole discipline in its own right, this is just scratching the surface.

optional
circle
Learn about Caching

A few resources on learning about caching

optional
circle
Review the Performance Testing Script

Engineers use performance testing tools to understand how well their applications are performing under high load. These tools can be expensive tooling, or simple Bash scripts.

Key Points
  • Review what the runPerformanceTest.sh script is and what it does.
  • Bash scripting is good to be aware of, but for the most part I find everyone including myself are not very good at it.
  • I usually use Bash scripts for small tasks, and if a task becomes too big or requires complex data types, I use a more powerful language instead.
  • Just worry about using the script provided, along with its options. But feel free to dig deeper if you want!
optional
circle
Break it!

Play with the provided script until your program breaks under load. Make note of the errors you receive.

Key Points
  • Use the runPerformanceTest.sh script to establish a baseline on your STTP server before making changes.
  • Reduce the interval between requests until your program breaks
  • Do not increase the number of concurrent requests, yet. That's what we'll look at next.
optional
circle
Implement Caching in the STTP Server

Complete the stories under the "(F) Caching System" section. Run the performance script throughout the development process to see improvement.

Key Points
  • By introducing basic caching you should be able to increase performance.
  • Do not implement concurrency, that's next!
  • Go is a very fast language, you may need to crank it up to see it really work.
Concurrency in Go
link

This is the really fun stuff and where Go really shines. Concurrency is allows your program to do multiple things at the same time. In other languages it's common to refer to concurrency as asynchronous programming. In this section we're going to learn a bit about concurrency and then implement it within the STTP server.

Tip: There are a lot of new concepts and terms when working with concurrency, just take it slow and work on understanding once concept at a time.

optional
circle
What is Concurrency

A few resources on learning about caching

optional
circle
Basics of Go Concurrency

Read the following articles to get an idea of what concurrency is about. Follow the examples yourself and play with their behavior.

Key Points
  • Goroutines allow multiple tasks to proceed simultaneously.
  • Channels facilitate communication between goroutines by allowing them to send and receive values. They help synchronize tasks.
  • Mutexes (mutual exclusions) are used to protect shared data from concurrent access by locking and unlocking critical sections of code, preventing race conditions.
  • Contexts manage the lifecycle of goroutines, allowing for cancellation, timeouts, and passing of request-scoped data.
optional
circle
Concurrency Deep Dive

This is a great series and goes deeper into concurrency. Dedicate the time to watch all videos. The presenter whiteboards along the way with examples.

optional
circle
Other Concurrency Topics

Included here are other utilities and features to be aware of when dealing with concurrency.

Key Points
  • Atomics in Go's sync/atomic package provide low-level, lock-free operations for safely updating shared variables across goroutines.
  • sync.Map is a thread-safe map in Go's sync package, allowing concurrent reads and writes without manual locking
optional
circle
Implement Concurrency in the STTP Server

Complete the stories under the "(F) Concurrency and Thread Safety" section. Ensure the performance script is making concurrent requests and run the script throughout the development process to see improvement.

Concluding Socket Programming and Concurrency
link

This is the last section dealing with socket programming but this is just the surface. We'll close out this guide with implementing the STTP Client and then provide some optional projects and ideas to go further if you desire.

optional
circle
Implement the STTP Client

Complete the final set of features starting with "(F) CLI -". This will implement a client side of the socket programming, along with introduce topics such as name resolution, url parsing etc.

optional
circle
Flex Your Skills - Rewrite the Performance Test Utility

You should be familiar with what the performance test utility does at this point, can you rewrite it using Go, Python or another language?

optional
circle
Other Ideas to Implement

Other ideas to implement if you're looking to expand this project

Key Points
  • Expand the STTP protocol to handle query parameters, allowing users to search through files
  • Swap out the filesystem for a SQLite Database
  • Swap out the memory cache for a Redis Cache, allow the server to be configured with in-memory or both (great use of interfaces!)
optional
circle
Develop your own Proxies

Another advanced topic to cover is writing you own Proxies. This is pretty advanced stuff to implement, but fun challenge for experienced engineers.

Key Points
  • Perform your own research and engineer your own solution.
  • Develop a third component that implements an STTP forward and/or reverse Proxy
  • [ client ]<--->[ proxy ]<--->[ server ]
optional
circle
Become an Expert with these Books

Material found on the web can only go so far, and in this case for a very deep dive an expert understanding, refer to these books.