DEV Community

Rajdeep Das
Rajdeep Das

Posted on • Originally published at Medium on

Building A Distributed Database Step by Step…(Part 1.1.1 Servers and P2P)

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

Photo by JESHOOTS.COM on Unsplash

Previous Part of the Series 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 :)

First Layer(Bottom Up)

Peer-to-peer ( P2P ) computing or networking is a 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) 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)

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

Demo 1

Server Running on Port 8888

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

Network fundamentals 1

Network fundamentals 2

Network fundamentals 3

Network fundamentals 4

Network fundamentals 5

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

Socket

  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)
Enter fullscreen mode Exit fullscreen mode

Address families

socket.AF\_INET Internet protocol (IPv4)
socket.AF\_INET6 Internet protocol (IPv6)
Enter fullscreen mode Exit fullscreen mode

Socket types

socket.SOCK\_STREAM Connection based stream (TCP)
socket.SOCK\_DGRAM Datagrams (UDP)
Enter fullscreen mode Exit fullscreen mode

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

Now run our bareboneServer.py to start the server, then run socket_ex.py

Getting data from our server

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

Now run coolServer.py open telnet client and attempt to connect our server

telnet localhost 9999
Enter fullscreen mode Exit fullscreen mode

connect using telnet

Server print(Server log)

Explanation

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

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

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

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 😀

Top comments (0)