Stateful vs Stateless API: වෙනස කුමක්ද?

Stateful vs Stateless API: වෙනස කුමක්ද?
Image source - interviewbit

Stateful සහ Stateless කියන Termsවලින් අපිට API එකේ Behavior සහ Data Handling සහ State Management වෙන්නෙ කොහොමද කියලා Describe කරන්න පුලුවන්.

Stateless APIs

Stateless API වලදි සෑම request එකක්ම කලින් ආව request වලට සම්බන්ධයක් නැති independent transactions විදියට සලකනවා. මෙහිදි client ගැන හෝ කලින් ආව request ගැන server side එකෙන් state or session information maintain වෙන්නෙ නෑ. ඒක නිසා clientගෙන් එන සෑම request එකක්ම server එක තුල request එක process වීමට අවශ්‍ය data සියල්ලම contain වෙන්න ඕන. Server එක මගින් request අතර context හෝ session data store වීමක් වෙන්නෙ නෑ. මේ stateless API වල state information maintain වෙන්නෙ නැති නිසා design කරන්න, implement කරන්න වගේම scale කරන්න පහසුයි. ඒ වගේම server එකක් fail උනොත් වෙන instance එකකට seamlessly ඊලගට එන request handle කරන්න පුලුවන් නිසා මේක ගොඩක් fault tolerance.

Stateful APIs

Stateful APIs වලදි සෑම client කෙනෙක්ටම හෝ request sequence එකකට අදාලව state or session information, server side එකේ maintain වෙනවා. කලින් එන request වල data, server එකේ store කරගෙන එම data, ඊලගට එන request process කරන්න use කරන එක මේකෙදි වෙනවා. උදාහරණයක් විදියට authentication, shopping carts, user preferences වගේ data maintain කරන්න පුලුවන්. මේ විදියට සෑම request එකක්ම identify කරගෙන specific client කෙනෙක්ට හෝ session එකකට associate කරන්න mechanism එකක් ඕන. මේකට session identifier, tokens හෝ cookies use කරන්න පුලුවන්. මෙහෙම state එකක් maintain කරනකොට server complexity එක වැඩි වෙනවා. ඒ වගේම scalability and faul tolerance issues එන්න පුලුවන්.

REST API, Stateful or Stateless ?

Generally, REST API design කරන්නෙ stateless විදියට. ඒක නිසා කිසිම state එකක් server එක මගින් handle කරන්නෙ නෑ. RESTful API වලදි application එකේ state එක manage කරන එක සහ necessary data සියල්ලම සෑම request එකක් සමගම එවන එක client ගෙ responsibility එකක්. Stateless RESTful API එකක key characteristics ටිකක් බලමු.

1. Stateless Server : මුලින් කිව්ව විදියට කිසිම state එකක් server එක මගින් store කරන්නේ නෑ. ඒක නිසා client ගෙන එන සෑම request එකකම necessary data සේරම තියෙන්න ඕන.

2. Scalability : සෑම server එකක්ම requests, independently handle කරන නිසා Stateless APIs වලට තව servers add කරලා horizontally scale කරන්න ලේසි.

3. Reliability : එක server එකක් fail උනොත් තව server instance එකකට seamlessly ඊලගට එන request handle කරන්න පුලුවන්. ඒක නිසා fault tolerance එක improve වෙලා තියෙනවා.

4. Caching : Stateless API වලින් එන response, effectively cache කරන්න පුලුවන්. Performance එක වැඩි වෙනවා වගේම server load එක අඩු කරන්න පුලුවන්.

Generally RESTful APIs stateless උනාට අපිට අවශ්‍ය නම් stateful behavior එක එන විදියට design කරන්න පුලුවන්. මේකෙදි tokens, cookies or local storage use කරලා state information client side එකේ store කරලා හෝ session data store සහ retrieve කරන්න database හෝ caching system එකක් වගේ separate stateful component use කරන්න පුලුවන්. මේකෙදි API එක stateless විදියට තිබුණත් application එකට stateful advantages ගන්න පුලුවන්.

Read more

Vo1d නම් Malware එකක් මඟින් Android TV මිලියනයකට අධික ප්‍රමාණයකට හානි

Vo1d නම් Malware එකක් මඟින් Android TV මිලියනයකට අධික ප්‍රමාණයකට හානි

Android TV Box එකක් මේ වන විට බොහෝ නිවෙස්වල ඇති බව නිසකයි. කෙසේ වෙතත් මේ පුවත නම් Android TV Box එකක් භාවිත කරන ඔබට නම් එතරම් සුභ දායක වන්නේ නැ.

By Bhasura Nuwan Kularathna
Game Design කරන්න ආසාවෙන් ඉන්නවා නම් ඔයා මේ මුලික කරුණු ගැන අනිවාර්යෙන්ම දැන ගන්න ඕනේ - 2

Game Design කරන්න ආසාවෙන් ඉන්නවා නම් ඔයා මේ මුලික කරුණු ගැන අනිවාර්යෙන්ම දැන ගන්න ඕනේ - 2

ඔයාගේ game එක play කරන්න ඕන කොහොමද කියලා player ට කියලා දෙන rules එහෙමත් නැත්නම් systems වලට තමයි අපි මේ game mechanics කියලා කියන්නේ. මේ දෙවනි mini article එකෙන් අපි කතා කරමු කොහොමද

By Ranga Pushpakumara