Server-side practical learning tips for front end personnel

This is a server-side learning guide for front-end personnel, whether it is web /H5 / applets or native APP developers, it is necessary to understand the back-end knowledge system dynamics. This article focuses on basic skills, practical operation and purpose introduction. The backend is a comprehensive subject, of which programming is only a very important point. Here, the front and back ends are simply defined:

  1. The front end solves the problems of data collection, data display and interaction between human and data.
  2. The backend solves the problems of data persistence, data acceleration, and interworking between data.

Back-end software architecture

no MVC

Why not talk about MVC, because with the development of technical division of labor, View display layer gradually withdrew from the stage of server-side development (there are special industry exceptions, such as large-scale data display in map industry, SSR). The backend gradually develops in depth. Distributed cluster, microservice, massive data storage and processing, containerized automatic orchestration and deployment have gradually become the standard backend configuration.

Classic backend mental model diagram

Overview of classical software data flows

Detailed data flow:

Typical deployment structure

This deployment is generic, and any practical solution is simply a matter of how much to add or subtract from the model.

Understanding the Server

Now let’s take a closer look at the server. A server can be a computer in nature, similar to a PERSONAL computer, but the server is placed in a remote room, there is no display, need to be connected through the network management. At present, the company usually does not directly contact the server hardware in the computer room, but mainly rents the cloud server to reduce the cost of manual maintenance. For personal learning, you can rent a server or install VMS, such as VirtualBox, on a PC. There are two types of servers: Windows and Linux. Windows server through the remote desktop connection with the same as personal computer operation, relatively simple and convenient; Linux is also the most widely used back-end operating system. Its advantage is that it occupies less resources and has a high utilization rate. Its disadvantage is that it has no interactive interface and can only be controlled through the command line. The most widely used version of Linux is centos. Using centos as an example, here is how to operate your server.

ssh

The first step to becoming familiar with the server is to connect to your server.

  1. For direct connection, you need to use the SSH client software, including Xshell, SecureCRT, and Putty. An IP address, port number, user name, and password are required for connecting to a server. After successful login, the CLI interface is displayed.

  2. A company usually has multiple servers. For security reasons, servers do not directly allow SSH connections to any server. Instead, a unified gateway is used as a springboard to log in to the jumpers first and then log in to other servers indirectly through the command line. Indirect jumps can jump arbitrarily between any server across multiple levels.

So there are two steps, the first step directly to the jumpers, the second step throughsshThe command line connects to other servers.

[~] > SSH to [email protected]Copy the code

To log out and return to the previous server, simply enter the exit command to exit the current server.

System File directory

centosThe operating system has its own set of directories, as shown belowIn general, you do not need to know what these directories are. You just need to know the directory where the command line resides:

  1. / : Root directory. Generally, the root directory stores only directories, not files
  2. /home: indicates the default user home directory. When a user account is added, the user’s home directory is stored in this directory. ~ indicates the home directory of the current user.
  3. /bin:/usr/bin: Directory for executing binary files, such as common commandsls,tar,mv,catEtc.
  4. /sbin:/usr/sbin:/usr/local/sbin: Places executable commands used by the system administrator, such asfdisk,mountAnd so on. Unlike /bin, these directories are for system administratorsrootCommon users can only view the command, but cannot set or use it.

If you want to find a directory for a command, use which:

    Find the ls command path[~] >which ls
Copy the code
How do I manipulate server files

Back-end development requires knowing how to manipulate files on the server, such as file transfer, creation, editing, copying, moving, deleting, and so on.

    Transfer files between servers[~] > SCP/home/test. TXT [email protected]: / home /# create file
    [~]> touch test.txt
    # copy file
    [~]> cp test.txt test2.txt
    # move file
    [~]> mv test.txt /data
    # delete file
    [~]> rm test.txt
    # Edit file
    [~]> vim test.txt
Copy the code

The key is to edit the file, the command line interface will enter the edit mode. General need to master the shortcut key function is to move the cursor position, delete a line, copy a line, jump to the specified line, jump to the end of the line, delete characters, insert characters, keyword search, forward search, reverse search, regular search and so on.

How to Install software

Centos system installation software generally use the yum command.

    yum [options] [command] [package ...]
Copy the code
    Find package command
    yum search <package_name>
Copy the code
    Install the specified software command
    yum install <package_name>
Copy the code
    Delete package command
    yum remove <package_name>
Copy the code
Linux commands and shell scripts

Linux has some common commands that you can use to be more productive. It can be easily divided into several categories, one is related to file processing, which has been described above, one is related to managing software and starting and stopping services and processes, and one is related to hardware, such as monitoring CPU and memory status, monitoring network and port, etc. In actual work, there will be some requirements, such as scheduled tasks, temporary processing of log files, etc., and writing code is a bit of a big effort, a simple shell script can be very efficient to deal with many simple transactions. Shell scripts are essentially collections of Linux commands that combine multiple commands to accomplish a comprehensive task.

    #! /bin/bash
    
    echo "exec test.sh begin" #1, print the log
    chmod u+x ./test.sh  #2, give the script execution permission
    ./test.sh  #3, execute the script
    echo "exec test.sh end" #4, print the log
    
Copy the code
Understanding the server network

The network is the core knowledge of the server, and understanding the network is one of the necessary back-end skills.

  1. port

Port is the channel for each server to communicate with the outside world. After each server application service is started, it will generally start monitoring one or more specific ports to receive or send data and provide services.

    Check the listener on port 8080
    lsof -i:8080
Copy the code
  1. A firewalleachlinuxServers have their own set of firewall services, for examplecentosYou can enable or disable the service.centos7It used to beiptable.centos7And then it changed tofirewallService. A firewall is a tool for managing ports and controlling the status of each port in fine detail. If a cloud service provider is used, the firewall service on each machine does not need to be configured separately on the machine. By default, the firewall service on each machine is disabled, and the security group management mechanism provided by the cloud service provider facilitates port management.
  2. Forward and reverse proxiesForward proxy: through which clients access arbitrary websites and hide the client’s own source, such as Internet proxy,socks.VPNAnd so on.

Reverse proxy: it is used by clients to access servers behind firewalls and hide servers, for examplenginx.apacheAnd so on. 4. Port forwardingFor example, you need to access the port of a server on the cloud network, but the external users cannot directly connect to the port. In this case, you can establish the port on the springboardsshPort forwarding solves this problem by directly turning remote access into local port access. 5. Port mappingA port mapping is a simple way to access one port, but actually access another port. The classic example isdockerContainer port mapping, which can change a service fixed port to any other port specified by the deployment.

Know the database

There are many types of databases, which can be roughly divided into traditional general-purpose relational databases and domain-specific purpose databases, as well as SQL and NOSQL databases, but are not well understood. General-purpose and special-purpose examples are used to illustrate the analysis.

Traditional relational database

Traditional databases are those that have a long history, such as MySQL, Oracle, SQLServer. The advantages of these databases are stability, good performance, and ACID characteristics, transaction processing is its strength, suitable for fixed structure data storage and maintenance.

  1. ACIDTo solve the database consistency problem when multiple clients access the database at the same time, that is, data is not disorderly

Atomicity, Consistency, Isolation, Durability.

  1. Paradigm, database design specifications, database table structure design data structure is not disorderly

In practice, the design specification will weigh against performance, but the specification design will greatly improve the clarity of the data.

  1. SQLDatabase manipulation language, necessary skills
Cache database

Cached databases are mainly used to speed up data acquisition, mainly through simplified data structures such askey-value, to obtain a much faster read speed than traditional databases, typically such asredis. Caching databases are typically used to speed up other types of databases:

Search the database

Search database is mainly used to speed up the data search process, mainly through fine-grained index mechanism, to obtain much faster than the traditional database query speed, such as typicalelasticsearch. The core of searching databases isInverted index:

Document database

Compared with traditional database, document database has the following characteristics:

  1. Unstructured data storage and query, loose data structure
  2. Data records resolve concurrent access issues by version, not by versionACIDData management
  3. It is easier to scale with large data volume

Typical examples are MongoDB, couchBase

Sequential database

Time series database is mainly used to process data with time label (change according to the order of time, namely time serialization), such as monitoring data, APP location coordinate reporting data, log data and so on. Time series database adopts special data storage method, which greatly improves the processing ability of time related data. Compared with relational database, its storage space is halved, and query performance is far better than relational database. Typical example: TimescaleDB, InfluxDB

Service deployment

When we call any software we use as a service, the service actually needs to be deployed, how to deploy the service is a knowledge. Deployment has changed dramatically from traditional manual deployment, to scripted deployment, to containerized deployment, and then to clustered deployment.

The server runs the service manually

Generally run the program locally

    java -jar XXX.jar # Java program
    node XXX.js # the node program
Copy the code

But the server does not run in this way, Linux programs must be specified to run in the background, otherwise exit the remote server, the current program immediately exit. Nohup and & are required to run properly in the background.

    nohup [command] &
Copy the code

Add the output log:

     nohup [command] >> output.log 2>&1 & 
Copy the code
The command function
nohup Immune closedsession
& Immune to useCtrl + C
>> Log redirection, append
2 > &1 Error level logs are also written to the normal log

The Linux log input/output convention represents numbers:

The command function java node
1 The standard output System.out.print console.log
2 Error output System.err.print console.error
Docker runs the service
    # Run open source public services like mysqlDocker run - name mysql5.7 - p 3306-3306 - d - v/data/mysql/data: / var/lib/mysql - v / data/mysql/my. CNF: / etc/mysql/mysql. Conf., d/mysqld. CNF MYSQL_ROOT_PASSWORD = 123456 - e mysql: 5.7Copy the code
    # service shutdownDocker stop mysql5.7Copy the code
    # delete service instanceDocker rm mysql5.7Copy the code
    # Delete a service mirrorDocker rmi mysql: 5.7Copy the code

Docker run:

The command function
– the name mysql5.7 Run the program and give it a nice name
-p 3306:3306 You can change the fixed port number of the service itself
-d Background running, similar to the above “nohup &” function
-v /data/mysql/data:/var/lib/mysql The /var/lib/mysql directory inside the container actually uses the external /data/mysql/data directory
-e MYSQL_ROOT_PASSWORD=123456 Pass in some initialization parameters
Cluster Running service

What problems clustering solves:

  1. The resource waste server automatically expands and contracts based on usage
  2. Complex deployment Automatic allocation Nodes are automatically deployed and multiple environments are switched

Here I put a lot of kinds of deployment of a unified call cluster deployment, including swarm, compose, k8s, and PAAS, BaaS, FaaS, Serverless, etc. Cluster if not specialized in this company, do not recommend to build their own, especially K8S cluster, directly with service providers can, simple page operation.

What is Serverless?

‘If your PaaS can be efficiently started instances in 20ms that run for half a second, Then call it serverless. ‘Adrian Cockcroft

  1. For the user,Serverless=httpThe interface is encapsulated as a function + back-end service, that is, in the future the front end will directly use functions to deal with the back end.
  2. ServerlessIs the future trend, has a very mature application environment. Thanks to thenodejsCold start capability,nodejsthanjavaThe present is more suitable for this scenario.

Server-side development

Language requirements

Beginners may think that a language is free and open, that they can write whatever they want, or that they just need to understand it. The truth may be disappointing, but language has never been a free-spirited tool of expression. It is an assembly-line tool. Why is that? Any language ultimately serves business interests, and back-end languages are no exception. What is the essence of business? Sell the most products at the least cost. What products make the most money? Standardized pipelined products, so development is essentially a game of turning complex requirements and ideas into standardized, pipelined processes. So what language is appropriate for this:

  1. Less prone to error

Strongly typed, checked at compile time

  1. Runtime error management is simple and fault isolation is possible

Stack trace, multithreading, run snapshot

  1. powerful

OOP, functional programming, generics, reflection

  1. Easy to cooperate with multiple people

Mature standardization, process standardization, engineering

Code specification
  1. Language specification itself

Like line breaks, indentation, punctuation, good naming, good structure. Such specifications can generally be programmed, such as the JSLint tool for JavaScript or the CheckStyle tool for Java.

  1. Logic specification

These types of norms are not routinable, but they are the key to differentiating personal skills. Good coding skills come from good coding habits, and good habits come from good thinking. It’s hard to program, but there are plenty of classic books to learn. For example, Effective XX series of books, Effective Java in Java and Effective JavaScript in JavaScript. It doesn’t matter if you don’t know them at the beginning. The ideas in these books will have a far-reaching influence and can be outdated even in today’s rapid technological development.

  1. The design specification

Design patterns look at software engineering practices from a higher perspective. They emphasize what are commonly known as patterns of behavior, which cross languages and are universal principles. Design patterns regulate three things:

  • How do I create a data-creation schema

  • How do YOU organize data structured schemas

  • How do data communicate behavioral patterns

    Here apply a famous management saying: build a team, set a strategy, lead the team – Liu Chuanzhi

Write code trilogy

Code is written for other people, and writing code is essentially determining how your code communicates with the outside world.

  1. Identify coding objectives and communicate with product customers
  2. Determine the interface, pre-define the interface and its parameters, and communicate with the front end and back end
  3. How to validate parameters, how to handle exceptions and known unknown errors, how to communicate with other modules and the larger framework
Server framework

Pick a back-end framework and read in depthThe official documentation.Then choose a requirement, use the framework to implement it, and solve the problem yourself.

Server Log Management
  1. Log print
    console.log("I am logging"); // Native log
    logger.info("I am logging"); // Frame log
Copy the code

Here to log4X (log4j.log4js) as an example to illustrate common log management frameworks. Common log output, for exampleSystem.out.print.console.logOutput directly to the console window. What the log management framework does is decompose this output process, separating the printing log code from the actual output logic, which is generally divided into three parts. The benefits of separation are the freedom to define output targets and the ability to debug log output levels dynamically.

Application structure:

  1. Log collection

After printing logs, they are finished. They can be used for statistics or troubleshooting. Because servers are generally clustered, logs are scattered across multiple servers and need to be collected, collated and merged for quick retrieval. Common distributed collection frameworks such asELK.Log search query interface:

The test code

We all want our code to be perfect, but unfortunately, buggy code is the norm, getting slapped in the face is the norm, and people who are too confident are often not good code writers.

  1. How do I test my code

Simple services can use unit tests, such as JEST,spring-boot-starter-test. If pre-test data initialization and post-test data rollback are also involved, this requires the introduction of a transactional testing framework. Some non-transactional databases, such as ElasticSearch, need to write their own initialization and clear testing code.

  1. How to tell someone a bug

Sometimes you find someone else’s bug, whether it’s a colleague in another department or submitted to the open source community, and need to prepare a basic use-case specification, which is a way to improve communication efficiency. MCVE (Minimal, Complete, and Verifiable example) : When a problem is being solved, it can be used to minimize, Complete, and Verifiable examples.

On how to learn

Unfortunately, even after reading this, back-end development may still be in the dark. To understand that the really valuable things need to hit a pain point, such as crushing performance, you need to understand the memory model, IO model; For example, when a common framework does not fully meet business requirements, specific business requirements require rewriting and refactoring the source code of an open source framework. A journey of a thousand miles begins with a single step.

About route Selection

In this society of rapid technological progress, technologists present different development routes. Some people are good at and like to study the bottom layer, and will specialize in a particular field deeply. Some are keen to discover and build new technologies and expand their knowledge boundaries, eventually moving towards the all-stack all-powerful direction. No matter which route, some people are very successful, others are confused. No matter how to choose, as long as you find the direction of your inner interest and persevere, over time, when you break through several cognitive bottlenecks, you will eventually reap huge gains.