Posts

Class Decorator practical uses - Part 2

Last part we have seen about function decorator and how to use it to add extensions to existing functions. We have seen how decorator are useful, today we can see about how to use class based decorator which will provide extension functionalities to existing functions. How we can configure a class decorator during creation so that we can configure a decorator for our need. Lets get started with code. def func ( msg ):     print ( f "Hai programmer we got this message { msg } for you " ) if __name__ == "__main__" :     func ( "Hello Developer" ) Now for adding a class decorator we need to understand two things one is how the constructor is invoked for a class and how functors is used while invoking an object of a class. While creating a instance of the  class like below. class MyDecorator :     def __init__ ( self , func ): obj = MyDecorator ( func ) Now we created an instance passing function as a parameter object. Since function is a first clas...

Decorators in python - Part 1

Decorators in python is very useful in data hiding and performing pre and post operation using simple function object. Below is a example of decorator and how its working in python interpretor. def sample_decorator ( func ):     def wrapper ( val ):         print ( "calling a function for pre-processing" )         return_value = func ( val )         print ( "calling post-processing function" )         return return_value     return wrapper def sample_function ( val ):     print ( f "hai from sample function { val } " ) if __name__ == "__main__" :     sample_function = sample_decorator ( sample_function )     sample_function ( 10 ) In python everything is object so what we are doing above is adding a syntatical sugar using a decorator that replaces the sample_function object with newly created sample_function object. We have created sample_function ...

Reverse proxy

  Reverse proxy: Reverse proxy is used for proxing backend servers where the IP address and protocol used by the backend server will not be exposed to internet. Reverse proxy handles the request for transport medium like HTTP or TCP serves the request on behalf of the actual server. Here is IP address and protocol will be masked hence we can have a public IP configured for reverse proxy exposed to internet and serves the request on behalf of the actual Web server having private IPs. Protocol conversion will also be supported for example client browser can transport the data using HTTP which will hit the proxy and it will inturn convert to RPC or any properiatery protocol which backend server accepts. Uses of reverse proxy is huge it serves as the following layers before connecting to actual server. Caching - Want to some of the data to be cached for example some webpages, js script or any frequently used data can be cached in Reverse proxy. Authentication and authorization - It ca...

System design - Design a system that provides cricket score in real-time

Image
 Problem: Design a system that broadcast cricket system score to millions of users online.  Requirement: Consider 10 millions of users online accessing the server at the same time. Each want to get the live update of cricket score. Latency requirement is 100 millisec not more than that. There can be n no of matches happening at the same time.  Functional: 1. Each user can login to the system using his credentials. 2. User can go to dashboard showing multiple matches going on with basic details like run, wicket, current playing teams. 3. Once user selects a match going on it will show the details of who played runs, who is currently playing, who is serving balls etc. Non-Functional: 1. Througput of the system should be high handling at least 1 million of connections. For this use a high end machine having more RAM and CPU. 2. Need a LB for balancing the load. Since we are going to use Server send events in HTTP as transport medium we need a L7 LB. Use least connection as a...

How to validate if the software has been corrupted or started by a malicious software

Code signing certificate: If we downloading a software from internet for example Adobereader.exe from internet how can you validate if this from a trusted source and its not tampered during transit. For this reason we use code signing certificate and sign it in our build system using signtool.exe passing .pfx file containing certificate and private key (if private key not present pass it separately).  Who will provide the code signing certificate: A Intermediatery CA or CA will provide you code signing certificate which can be organization validated or domain validated or Extended validated. EV is a special way of approving a certificate its so stringent that CA will validate the organization financial statement and go in person to validate the presence of office. For all other validation just personal identification, legal documents and domain/site details will do, This is needed so a hacker can not impersonate as other valid domain. How it is generated: First a organization or de...

Public key cryptogrphy - How certificate validation works using certificate chain

Application of certificate- Its widely used as a mean for identity, passing the asymmetric public key used for key exchange, also provides integrity of certificate. In case if the client or server in TLS or SSL handshake want to trust the client/server it will validate the certificate of server/client by verfying the certificate signature.  What is digitial certificate - When I want a server using HTTPS communication I need to purchase a SSL certificate for my domain. The reason is when a browser does ssl handshake it need to validate the authenticity of the website also need to make sure this authenticity is not tampered during the transit. For this problem we use certificate. Certificate contains mainly two parts like attributes and signature.  Attribute contains - Issuer, Subject name like google.com, public key algorithm, public key etc. Signature - This is crucial part of having integrity and authenticy of the certificate. For eg if I am getting an end entity ceritificate...

System Design - Upload files in Server farm

Image
 Requirement: There are server farms with multiple servers each having different volume size of storage you need to write a service that can perfrom CRUD operation in files upload, update, get and delete. While uploading you have to split the files at random size and upload in server farm. Also there should be an operation for search using grep command that can search the files in a particular folder or files.  Assume we have 10,000 users are uploading an average of 500MB file size per second. Resource calculation: Storage - 10,000*500*60*60*24 = 432 PB per day * 365 days = 157680 PB file storage needed for year DB - 10,000 * 175 bytes *60*60*24 = 1.51 PB per day * 365 days = 551.15 PB per year storage Architecture: Have a microservices placed after the LB, the microservice can expose the REST endpoints (/operate/file and /operate/search) which can take operations.  POST - Upload the file name for the folder path. While uploading we need to split the files into multi...