Building A Distributed Database Step by Step…(Part 1.1.1 Servers and P2P)
Tags: pyhton, distributedsystems, computernetworking, distributeddatabase
Published: 2022-07-11 01:35:11

---
title: Building A Distributed Database Step by Step…(Part 1.1.1 Servers and P2P)
published: true
date: 2019-05-04 21:35:24 UTC
tags: pyhton,distributedsystems,computernetworking,distributeddatabase
canonical_url: https://medium.com/@rajdeepdas.india/building-a-distributed-database-step-by-step-part-1-1-1-servers-and-p2p-5c80960f76b
---

#### Peer to Peer Network and Simple Server Implementation in Python for Distributed Database Nodes.

![](https://cdn-images-1.medium.com/max/1024/0*9p6T9w_VlWHL20Rf)<figcaption>Photo by <a href="https://unsplash.com/@jeshoots?utm_source=medium&amp;utm_medium=referral">JESHOOTS.COM</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

**Previous Part of the Series** [https://medium.com/@rajdeepdas.india/building-a-distributed-database-step-by-step-part-1-abc7d944e52d](https://medium.com/@rajdeepdas.india/building-a-distributed-database-step-by-step-part-1-abc7d944e52d)

Today we will clear the basic concepts of P2P and try to build a P2P application which will help us to build our distributed database nodes in the future.

Fist I thought to build the server using Go & C/C++ but I think more important is to clear the concepts. For Easy understanding let’s try to build with something easier language like Python. later we will convert this to C/C++ or go implementation.

I choose Python because it comes with lot’s of libraries and modules, so it will be easier to understand the abstract concepts easily rather than the understanding of lower level details. In the beginning, if we go at a very lower level it will be harder to understand for beginners as well as Intermediate users or programmers.

As I intended this series for all beginner to advance programmers so if you are an intermediate or advanced programmer and have knowledge about these basic concepts I will recommend to skip this part otherwise you may fill boring. although I will also request the advance programmers to read it to rectify if I made any mistake and give me feedback.free fell to give feedback.

Too much introduction and talk let’s dive into it :)

![](https://cdn-images-1.medium.com/max/612/1*BGpQR4QqUCFqQzMhkXT-_g.png)<figcaption>First Layer(Bottom Up)</figcaption>

**Peer-to-peer** ( **P2P** ) computing or networking is a [distributed application](https://en.wikipedia.org/wiki/Distributed_application) architecture that partitions tasks or workloads between peers. Peers are equally privileged, equipotent participants in the application. They are said to form a peer-to-peer network of nodes.

Everyone has a question why this guy wanted to use peer to peer network the main point of using it the followings…

1. In peer-to-peer networks, all nodes act as a server as well as client, therefore, no need of a dedicated server.
2. As I told the previous post ([_Post Link_](https://medium.com/@rajdeepdas.india/building-a-distributed-database-step-by-step-part-1-abc7d944e52d)) we will use the **Raft** Consensus Algorithm for Sync the Databases.
3. P2P is a good choice for decentralized applications.
4. As we know Wireless Sensors Networks are mainly decentralized, so it easy for us to make any of node is master, if we follow the technique of P2P Networks.
5. Peer-to-peer gains its largest advantage from cost, as it does not require a centralized server, a network operating system, or an administrator. Peer-to-peer networks

Let’s first Build a Simple Python BareBone HTTP Server

**Simple HTTP Server(** bareBoneServer.py)

{% gist https://gist.github.com/Rajdeep-Das/e0b6383d2ac68ff93094bd4caf30bc32 %}

Create a simple HTML file in the same directory and run the programme, then go the browser 127.0.0.1:8888, you will see the server serve HTML content

![](https://cdn-images-1.medium.com/max/1024/1*NiKw8ct5sJVJAH65p0NM4A.jpeg)<figcaption>Demo 1</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*IzE5u8kNzQCJk-9TYwPT3g.jpeg)<figcaption>Server Running on Port 8888</figcaption>

**So before implementing the Peer to Peer network, we must understand basic of the network, socket programming and basic network programming in python**

### Network Fundamentals

Let’s Quicky understand visually

![](https://cdn-images-1.medium.com/max/575/1*hHcjchPGhdIxDs8JaAl8Ew.jpeg)<figcaption>Network fundamentals 1</figcaption>

![](https://cdn-images-1.medium.com/max/668/1*TK5zQOToJG5lhKQDdBT31A.jpeg)<figcaption>Network fundamentals 2</figcaption>

![](https://cdn-images-1.medium.com/max/685/1*67LVLuuDpRDFjp2vJjKoiw.jpeg)<figcaption>Network fundamentals 3</figcaption>

![](https://cdn-images-1.medium.com/max/656/1*JFMF9BFHRR02M1Ve5lD5jw.jpeg)<figcaption>Network fundamentals 4</figcaption>

![](https://cdn-images-1.medium.com/max/668/1*BayPeTom7jC1-bl3GlknHA.jpeg)<figcaption>Network fundamentals 5</figcaption>

### **Data Transport**

There are two basic types of communication

1. **Streams (TCP)**: Computers establish a connection with each other and read/write data in a continuous stream of bytes — -like a file. This is the most common.
2. **Datagrams (UDP)**: Computers send discrete packets (or messages) to each other. Each packet contains a collection of bytes, but each packet is separate and self-contained.

### **Sockets**

Programming abstraction for the network code

![](https://cdn-images-1.medium.com/max/545/1*IYM7f0OjI9i38Or4G9ku1Q.jpeg)<figcaption>Socket</figcaption>

1. **Socket** : A communication endpoint
2. Supported by socket library module
3. Allows connections to be made and data to be transmitted in either direction

### **Socket Basics**

To create socket

```
import socket
s = socket.socket(addr\_family, type)
```

Address families

```
socket.AF\_INET Internet protocol (IPv4)
socket.AF\_INET6 Internet protocol (IPv6)
```

Socket types

```
socket.SOCK\_STREAM Connection based stream (TCP)
socket.SOCK\_DGRAM Datagrams (UDP)
```

Now Let’s combine the above concept and build a **simple client** which fetch data from our made above Simple HTTP Server

_socket\_ex.py_ file looks like below

{% gist https://gist.github.com/Rajdeep-Das/43adfaec91db2bc69d5abfbcda825d69 %}

Now run our _bareboneServer.py_ to start the server, then run socket\_ex.py

![](https://cdn-images-1.medium.com/max/835/1*k5wOpgguAxPkH1IybSIGIQ.jpeg)<figcaption>Getting data from our server</figcaption>

### Next, We will try to build TCP Server

#### Server Implementation

The very basic concepts of server in a practical way are the followings…

1. Network servers are a bit more tricky
2. Must listen for incoming connections on a well-known port
3. Typically run forever in a server-loop

> May have to service multiple clients

{% gist https://gist.github.com/Rajdeep-Das/d5cf9b45bd7eaa34a5aa353372614046 %}

Now run _coolServer.py_ open telnet client and attempt to connect our server

```
telnet localhost 9999
```

![](https://cdn-images-1.medium.com/max/554/1*6l5RTq77Bv6FBrzhnsEESw.jpeg)

![](https://cdn-images-1.medium.com/max/707/1*GwZSZyy1WtuaCTIJ6PQshQ.jpeg)<figcaption>connect using telnet</figcaption>

![](https://cdn-images-1.medium.com/max/806/1*1C4TADsdRROI3fKBjmkp6Q.jpeg)<figcaption>Server print(Server log)</figcaption>

![](https://cdn-images-1.medium.com/max/576/1*xeHhDKjONoveMPrIp_Ghxw.jpeg)

![](https://cdn-images-1.medium.com/max/829/1*GCmIJOZvKiSetoCXYu6n6w.jpeg)<figcaption>Explanation</figcaption>

```
c.send("Hi buddy %s\n" %a[0]) # Send Data to Clinet
```

Use the client socket for transmitting data. The server socket is only used for accepting new connections.

![](https://cdn-images-1.medium.com/max/682/1*RW78jMjbY7J6Cf8AiIa1Aw.jpeg)

A server can keep client connection alive as long as it wants. Can repeatedly receive/send data.

![](https://cdn-images-1.medium.com/max/585/1*xvIwVQ2CnvydG5memTCtOw.jpeg)

Server waiting for the next connection. Original server socket is reused to listen for more connections. The server runs forever in a loop like above.

> **In the next part 1.1.2, we will try deal with advance socket programming in python,thread server,socket as file and many more which will help us to build our p2p network for our distributed database**

Stay tuned and have fun …  **😎**

Happy Reading 😀