Backend Essentials: TCP/UDP, HTTP, Linux, MySQL Indexes & C++ Basics
This article compiles essential backend knowledge, covering TCP vs UDP differences, why HTTP uses TCP, HTTP/2 and HTTP/3 improvements, status codes, long vs short connections, key Linux process commands, MySQL B+Tree indexing, C++ struct/class nuances, STL container types, map thread‑safety, and queue versus stack distinctions.
Working in internet companies often involves high intensity; a comfort ranking of Chinese internet firms shows Tencent as the most comfortable, followed by Baidu and JD, with ByteDance, Pinduoduo and Huawei in the highly competitive tier.
Tencent (Backend Interview)
1. What are the differences between TCP and UDP?
Connection: TCP is connection‑oriented, UDP is connectionless.
Service model: TCP is one‑to‑one, UDP supports one‑to‑many and many‑to‑many.
Reliability: TCP guarantees ordered, loss‑free delivery; UDP provides best‑effort delivery.
Congestion & flow control: TCP has mechanisms, UDP does not.
Header overhead: TCP header is 20+ bytes, UDP header is fixed 8 bytes.
Transmission mode: TCP streams data, UDP sends packets.
2. Which transport protocol does HTTP use and why?
HTTP/1.1 is built on TCP because it requires reliable, ordered delivery of web resources. HTTP/2 also uses TCP but adds multiplexing, binary framing, and header compression (HPACK) to mitigate head‑of‑line blocking.
HTTP/3 switches to QUIC (based on UDP) to eliminate TCP’s head‑of‑line blocking, achieve faster connection establishment (1‑2 RTT vs 3‑4 RTT for TCP+TLS), and support connection migration via a connection ID.
3. What HTTP status codes should you know?
HTTP status codes are divided into five classes:
1xx – Informational (rarely used).
2xx – Success (e.g., 200 OK).
3xx – Redirection (e.g., 301, 302).
4xx – Client error (e.g., 404 Not Found, 405 Method Not Allowed).
5xx – Server error (e.g., 500 Internal Server Error).
4. Is HTTP a long‑connection or short‑connection?
Traditional HTTP/1.0 uses a short connection: each request opens a TCP connection, exchanges data, then closes it. HTTP/1.1 introduced persistent (keep‑alive) connections, allowing multiple requests/responses over a single TCP connection, effectively making it a long connection by default.
5. How to view processes in Linux?
Use the ps command.
6. What does the second column of ps -aux represent and how does it differ from ps -elf ?
The second column shows the PID. ps -aux emphasizes user and resource usage, while ps -elf focuses on process hierarchy (PPID), priority, and system flags.
7. How does MySQL implement indexes?
MySQL InnoDB uses a B+Tree structure. Only leaf nodes store actual row data; internal nodes store index keys, keeping the tree shallow (typically 3‑4 levels) and enabling fast disk I/O (3‑4 reads per lookup). select * from product where id = 5; The lookup proceeds top‑down: compare with root keys, descend to the appropriate child, and finally locate the leaf containing the target row.
8. Why use B+Tree instead of B‑Tree?
Non‑leaf nodes store only keys, allowing more entries per node and reducing height.
All data resides in leaf nodes linked sequentially, making range queries efficient.
In pure memory scenarios B‑Tree may have a slight random‑access advantage, but B+Tree’s benefits generally outweigh it.
9. What are the differences between struct and class in C++?
Default access: struct members are public, class members are private.
Default inheritance: struct inherits public by default, class inherits private by default.
struct A { int x; void f() {} };
class B { int y; void g() {} };
int main() { A a; a.x = 10; a.f(); B b; // b.y = 20; // error
return 0; }10. Overview of C++ STL containers
Containers are grouped into three categories:
Sequence containers (vector, deque, list, array).
Associative containers (set, multiset, map, multimap) – typically implemented with red‑black trees.
Unordered associative containers (unordered_set, unordered_map, etc.) – hash‑table based.
Container adapters (stack, queue, priority_queue) provide limited interfaces built on underlying containers.
11. Is std::map thread‑safe?
No. Concurrent reads/writes can corrupt the internal red‑black tree, cause iterator invalidation, or crash the program. To make it safe, protect accesses with a mutex or use a thread‑safe concurrent map implementation.
#include <map>
#include <mutex>
std::map<int,int> my_map;
std::mutex mtx;
void safe_insert(int key,int value){
std::lock_guard<std::mutex> lock(mtx);
my_map[key]=value;
}
int safe_find(int key){
std::lock_guard<std::mutex> lock(mtx);
auto it=my_map.find(key);
return (it!=my_map.end())?it->second:-1;
}12. What are the differences between a queue and a stack?
Queue follows FIFO (first‑in‑first‑out); stack follows LIFO (last‑in‑first‑out).
Queues are suited for ordered processing (e.g., task scheduling); stacks are ideal for managing recent state (e.g., function calls).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
