Repository navigation
Expand file tree
/
Copy pathinfo.txt
More file actions
218 lines (167 loc) · 5.88 KB
/
Copy pathinfo.txt
File metadata and controls
218 lines (167 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
Language : C++17
Build System : CMake
Testing : GoogleTest
Networking : POSIX/WinSock sockets
Storage : unordered_map
Concurrency : thread + shared_mutex
Persistence : fstream
rom the repo root, use these commands:
Configure the project, if needed:
cmake -S . -B build
Build it:
cmake --build build
Run the executable on Windows:
.\build\cpp_multithreaded_cache_engine.exe
If you want a Debug build with this MinGW setup, configure with:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
redis-lite/
│
├── CMakeLists.txt
│
├── src/
│ ├── main.cpp
│ │
│ ├── cache/
│ │ ├── CacheEngine.h
│ │ └── CacheEngine.cpp
│ │
│ ├── storage/
│ │ ├── Entry.h
│ │ └── StorageEngine.h
│ │
│ ├── ttl/
│ │
│ ├── eviction/
│ │
│ ├── metrics/
│ │
│ ├── persistence/
│ │
│ ├── replication/
│ │
│ └── server/
│
└── tests/
RESP (Redis Serialization Protocol ) : Redis client (your application) and the Redis server use to talk to each other.
DAEMON : A Daemon is simply a background process that runs silently to manage
these two tasks without freezing the main database engine.
- The background daemon quietly writes all that data to an .rdb file on the solid-state drive (SSD).
- use the fork() mechanism of os to run in background
Snapshots (RDB) - Single file binary representation of entire dataset
- OS concept called Copy-on-Write (CoW), this child process gets a point-in-time duplicate of
the memory without actually copying the data in RAM.
- Pros: The file size is very small and optimized. It is incredibly fast to load back into memory if the server restarts.
- Cons: You can lose data. If you configure snapshots to happen every 5 minutes,
and the server crashes at minute 4, you lose 4 minutes of data.
WAL(Write-Ahead Log (AOF)) - To solve the data-loss problem of snapshots,
Redis uses a Write-Ahead Log (WAL), which Redis calls the Append Only File (AOF).
RAII - stands for Resource Acquisition Is Initialization. It is a core programming technique in C++ where the
lifecycle of a resource is strictly tied to the lifetime of a local object.
Scenario Command
Changed a .cpp or .h file : cmake --build .\build
Added a new file (update CMakeLists.txt too!): cmake -S . -B build then cmake --build .\build
Something is broken, start fresh Delete build\ folder, then reconfigure + build
If you add a new .cpp file, you must also add it to
CMakeLists.txt
under add_executable(...) before rebuilding.
✅ Storage
✅ Command Parser
✅ Thread Safety
✅ Thread Test
⬜ Thread Pool
⬜ TCP Server
⬜ TTL
⬜ LRU Eviction
⬜ Memory Limits
⬜ Persistence
⬜ Replication
⬜ Metrics
CLI
│
▼
Command Parser
│
▼
Cache Engine
│
▼
Storage Engine
│
+--------+--------+
| |
▼ ▼
unordered_map shared_mutex
-- TCP server steps ---
Start
↓
Create socket
↓
Bind socket to a port
↓
Listen
↓
Accept client
↓
Read command
↓
Execute
↓
Send response
↓
Repeat forever
2. WSAStartup(MAKEWORD(2, 2), &wsaData)
WSAStartup(...): The function that initiates the WS2_32.dll library for the process.
MAKEWORD(2, 2): A macro that constructs a WORD specifying the requested version of Winsock.
2, 2 requests Winsock Version 2.2 (the standard modern version for Windows network programming).
&wsaData: Passes the address of the WSADATA structure to receive configuration details populated by Windows.
3. Error Handling (if (...) != 0)
WSAStartup returns 0 on success.
If it returns any non-zero value, it indicates an initialization failure (e.g., system network subsystem not ready, requested version unsupported).
/*************************************************************/
cmake -B build
cmake --build build --target cpp_multithreaded_cache_engine
cmake --build build --target cache_client
cpp_multithreaded_cache_engine.exe (The Server)
cache_client.exe (The Client)
/*************************************************************/
Code Explanation
htons(8080) Host To Network Short: Converts the port number from host byte order (Little Endian on x86) to network byte order (Big Endian).
inet_pton(...) Presentation to Network: Converts "127.0.0.1" (text string) into binary in serverAddress.sin_addr.
connect(...) Initiates the TCP 3-way handshake to the server listening on 127.0.0.1:8080.
#pragma comment(lib, "Ws2_32.lib") : this is a Microsoft Visual C++ (MSVC) preprocessor directive that tells the linker to
automatically link the Windows Sockets 2 library (Ws2_32.lib) into your executable.
CacheEngine
+-------------+
| Storage |
+-------------+
▲
│
+-----------------------+
| CommandProcessor |
| |
| parser_ |
| cache_ (reference) ---+
+-----------------------+
-> How "Non-Copyable" Propagates
This restriction cascades up through any class that owns a non-copyable member by value:
When a class contains a non-copyable member, the compiler implicitly deletes its own copy constructor too.
You never wrote = delete on StorageEngine or CacheEngine, but the compiler did it for you.
Client
while(true)
{
Read Command
send()
recv()
Print}
================ TCP ================
Server
while(true)
{
accept()
while(client connected)
{
recv()
execute()
send()
}
}